From 800e7730d60c6c64914cd39b3fa7d286f548dc8a Mon Sep 17 00:00:00 2001 From: Trond Magnus Sevre Date: Mon, 2 Dec 2024 14:37:33 +0100 Subject: [PATCH] throw exception if no handler found --- .../customer/controller/GlobalExceptionHandler.java | 9 +++++++++ src/main/resources/application.yml | 1 + 2 files changed, 10 insertions(+) diff --git a/src/main/java/no/fint/portal/customer/controller/GlobalExceptionHandler.java b/src/main/java/no/fint/portal/customer/controller/GlobalExceptionHandler.java index 12ba1ff..5fa48f2 100644 --- a/src/main/java/no/fint/portal/customer/controller/GlobalExceptionHandler.java +++ b/src/main/java/no/fint/portal/customer/controller/GlobalExceptionHandler.java @@ -7,6 +7,7 @@ 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.NoHandlerFoundException; import javax.servlet.http.HttpServletRequest; @@ -23,4 +24,12 @@ public ResponseEntity handleGlobalException(Exception ex, WebRequ ErrorResponse errorResponse = new ErrorResponse("Internal Server Error! An unexpected error occurred. Please try again later."); return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); } + + @ExceptionHandler(NoHandlerFoundException.class) + public ResponseEntity handleNoHandlerFoundException(NoHandlerFoundException ex) { + log.error("No handler found", ex); + + ErrorResponse errorResponse = new ErrorResponse("Internal Server Error! An unexpected error occurred. Please try again later. (2)"); + return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 1db6e4c..489617d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -85,6 +85,7 @@ spring: mvc: servlet: path: /api + throw-exception-if-no-handler-found: true cache: cache-names: - users