1+ /*
2+ * ============LICENSE_START=======================================================
3+ * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
4+ * ================================================================================
5+ * Licensed under the Apache License, Version 2.0 (the "License");
6+ * you may not use this file except in compliance with the License.
7+ * You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ *
17+ * SPDX-License-Identifier: Apache-2.0
18+ * ============LICENSE_END=========================================================
19+ */
20+
21+ package org .onap .cps .ncmp .dmi .rest .stub .exception ;
22+
23+ import java .util .HashMap ;
24+ import java .util .Map ;
25+ import lombok .extern .slf4j .Slf4j ;
26+ import org .springframework .http .HttpHeaders ;
27+ import org .springframework .http .HttpStatus ;
28+ import org .springframework .http .HttpStatusCode ;
29+ import org .springframework .http .ResponseEntity ;
30+ import org .springframework .lang .NonNull ;
31+ import org .springframework .web .bind .MethodArgumentNotValidException ;
32+ import org .springframework .web .bind .MissingServletRequestParameterException ;
33+ import org .springframework .web .bind .annotation .ControllerAdvice ;
34+ import org .springframework .web .bind .annotation .ExceptionHandler ;
35+ import org .springframework .web .context .request .WebRequest ;
36+ import org .springframework .web .servlet .mvc .method .annotation .ResponseEntityExceptionHandler ;
37+
38+ @ ControllerAdvice
39+ @ Slf4j
40+ public class DmiRestStubExceptionHandler extends ResponseEntityExceptionHandler {
41+
42+ // Handles validation errors on @RequestBody and @Valid
43+ @ Override
44+ protected ResponseEntity <Object > handleMethodArgumentNotValid (@ NonNull final MethodArgumentNotValidException
45+ methodArgumentNotValidException ,
46+ @ NonNull final HttpHeaders httpHeaders ,
47+ @ NonNull final HttpStatusCode httpStatusCode ,
48+ @ NonNull final WebRequest webRequest ) {
49+ final Map <String , String > errors = new HashMap <>();
50+ methodArgumentNotValidException .getBindingResult ().getFieldErrors ().forEach (error ->
51+ errors .put (error .getField (), error .getDefaultMessage ()));
52+
53+ log .warn ("Validation failed: {}" , errors );
54+ return new ResponseEntity <>(errors , HttpStatus .BAD_REQUEST );
55+ }
56+
57+ // Handles missing @RequestParam
58+ @ Override
59+ protected ResponseEntity <Object > handleMissingServletRequestParameter (
60+ @ NonNull final MissingServletRequestParameterException missingServletRequestParameterException ,
61+ @ NonNull final HttpHeaders httpHeaders ,
62+ @ NonNull final HttpStatusCode httpStatusCode ,
63+ @ NonNull final WebRequest webRequest ) {
64+ final String error = "Missing required query parameter: "
65+ + missingServletRequestParameterException .getParameterName ();
66+ log .warn (error );
67+ return new ResponseEntity <>(error , HttpStatus .BAD_REQUEST );
68+ }
69+
70+ /**
71+ * Handles all uncaught exceptions in the application.
72+ * This method acts as a global fallback exception handler using Spring's
73+ * {@link ExceptionHandler} mechanism. It captures any exceptions that are not
74+ * explicitly handled by other more specific exception handlers.
75+ *
76+ * @param exception the exception that was thrown
77+ * @param webRequest the current web request during which the exception occurred
78+ * @return a {@link ResponseEntity} containing a generic error message and an
79+ * {@link HttpStatus#INTERNAL_SERVER_ERROR} status code
80+ */
81+ @ ExceptionHandler (Exception .class )
82+ public ResponseEntity <Object > handleAllExceptions (final Exception exception , final WebRequest webRequest ) {
83+ log .error ("Unexpected server error: {}" , exception .getLocalizedMessage ());
84+ return new ResponseEntity <>("Internal server error. Please contact support." + webRequest .getContextPath (),
85+ HttpStatus .INTERNAL_SERVER_ERROR );
86+ }
87+ }
0 commit comments