Implemented a Global Exception Handler using @ControllerAdvice to manage common excep... 83/140983/1
authorsourabh_sourabh <sourabh.sourabh@est.tech>
Wed, 28 May 2025 13:55:30 +0000 (14:55 +0100)
committersourabh_sourabh <sourabh.sourabh@est.tech>
Wed, 28 May 2025 13:55:30 +0000 (14:55 +0100)
- Handles validation errors (MethodArgumentNotValidException) with detailed field error responses.
- Handles missing request parameters (MissingServletRequestParameterException) with clear error messages.
- Catches all other uncaught exceptions and returns a generic internal server error response.
- This improves error consistency, observability, and simplifies controller logic.

Issue-ID: CPS-2827
Change-Id: I702ca917d71af997a5743288732dbc1c91ee2798
Signed-off-by: sourabh_sourabh <sourabh.sourabh@est.tech>
dmi-stub/dmi-stub-service/src/main/java/org/onap/cps/ncmp/dmi/rest/stub/exception/DmiRestStubExceptionHandler.java [new file with mode: 0644]

diff --git a/dmi-stub/dmi-stub-service/src/main/java/org/onap/cps/ncmp/dmi/rest/stub/exception/DmiRestStubExceptionHandler.java b/dmi-stub/dmi-stub-service/src/main/java/org/onap/cps/ncmp/dmi/rest/stub/exception/DmiRestStubExceptionHandler.java
new file mode 100644 (file)
index 0000000..daf01bd
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.dmi.rest.stub.exception;
+
+import java.util.HashMap;
+import java.util.Map;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.HttpStatusCode;
+import org.springframework.http.ResponseEntity;
+import org.springframework.lang.NonNull;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.bind.MissingServletRequestParameterException;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.context.request.WebRequest;
+import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
+
+@ControllerAdvice
+@Slf4j
+public class DmiRestStubExceptionHandler extends ResponseEntityExceptionHandler {
+
+    // Handles validation errors on @RequestBody and @Valid
+    @Override
+    protected ResponseEntity<Object> handleMethodArgumentNotValid(@NonNull final MethodArgumentNotValidException
+                                                                          methodArgumentNotValidException,
+                                                                  @NonNull final HttpHeaders httpHeaders,
+                                                                  @NonNull final HttpStatusCode httpStatusCode,
+                                                                  @NonNull final WebRequest webRequest) {
+        final Map<String, String> errors = new HashMap<>();
+        methodArgumentNotValidException.getBindingResult().getFieldErrors().forEach(error ->
+                errors.put(error.getField(), error.getDefaultMessage()));
+
+        log.warn("Validation failed: {}", errors);
+        return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
+    }
+
+    // Handles missing @RequestParam
+    @Override
+    protected ResponseEntity<Object> handleMissingServletRequestParameter(
+            @NonNull final MissingServletRequestParameterException missingServletRequestParameterException,
+            @NonNull final HttpHeaders httpHeaders,
+            @NonNull final HttpStatusCode httpStatusCode,
+            @NonNull final WebRequest webRequest) {
+        final String error = "Missing required query parameter: "
+                + missingServletRequestParameterException.getParameterName();
+        log.warn(error);
+        return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
+    }
+
+    /**
+     * Handles all uncaught exceptions in the application.
+     * This method acts as a global fallback exception handler using Spring's
+     * {@link ExceptionHandler} mechanism. It captures any exceptions that are not
+     * explicitly handled by other more specific exception handlers.
+     *
+     * @param exception  the exception that was thrown
+     * @param webRequest the current web request during which the exception occurred
+     * @return a {@link ResponseEntity} containing a generic error message and an
+     *     {@link HttpStatus#INTERNAL_SERVER_ERROR} status code
+     */
+    @ExceptionHandler(Exception.class)
+    public ResponseEntity<Object> handleAllExceptions(final Exception exception, final WebRequest webRequest) {
+        log.error("Unexpected server error: {}", exception.getLocalizedMessage());
+        return new ResponseEntity<>("Internal server error. Please contact support." + webRequest.getContextPath(),
+                HttpStatus.INTERNAL_SERVER_ERROR);
+    }
+}
\ No newline at end of file