Skip to content

Commit

Permalink
throw exception if no handler found
Browse files Browse the repository at this point in the history
  • Loading branch information
trondsevre committed Dec 2, 2024
1 parent feccbc5 commit 800e773
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,4 +24,12 @@ public ResponseEntity<ErrorResponse> 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<ErrorResponse> 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);
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ spring:
mvc:
servlet:
path: /api
throw-exception-if-no-handler-found: true
cache:
cache-names:
- users
Expand Down

0 comments on commit 800e773

Please sign in to comment.