From 15b74d92614bbbf8b3badd27867c4e3d45d45f51 Mon Sep 17 00:00:00 2001 From: sourabh_sourabh Date: Wed, 28 May 2025 14:55:30 +0100 Subject: [PATCH] Implemented a Global Exception Handler using @ControllerAdvice to manage common exceptions across the application. - 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 --- .../exception/DmiRestStubExceptionHandler.java | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 dmi-stub/dmi-stub-service/src/main/java/org/onap/cps/ncmp/dmi/rest/stub/exception/DmiRestStubExceptionHandler.java 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 index 00000000..daf01bd1 --- /dev/null +++ b/dmi-stub/dmi-stub-service/src/main/java/org/onap/cps/ncmp/dmi/rest/stub/exception/DmiRestStubExceptionHandler.java @@ -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 handleMethodArgumentNotValid(@NonNull final MethodArgumentNotValidException + methodArgumentNotValidException, + @NonNull final HttpHeaders httpHeaders, + @NonNull final HttpStatusCode httpStatusCode, + @NonNull final WebRequest webRequest) { + final Map 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 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 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 -- 2.16.6