Skip to content

Commit

Permalink
Log PreAuthenticatedCredentialsNotFoundException
Browse files Browse the repository at this point in the history
  • Loading branch information
trondsevre committed Dec 2, 2024
1 parent 399eadc commit f6694ca
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import no.fint.portal.model.ErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedCredentialsNotFoundException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
Expand All @@ -15,14 +16,10 @@
@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleGlobalException(Exception ex, WebRequest request, HttpServletRequest httpRequest) {

log.error("Unhandled exception on call to: {}", httpRequest.getRequestURL());
log.error("Unhandled exception", ex);

ErrorResponse errorResponse = new ErrorResponse("Internal Server Error! An unexpected error occurred. Please try again later.");
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
@ExceptionHandler(PreAuthenticatedCredentialsNotFoundException.class)
public ResponseEntity<ErrorResponse> handlePreAuthenticatedCredentialsNotFoundException(PreAuthenticatedCredentialsNotFoundException ex, HttpServletRequest httpRequest) {
log.error("Missing pre-authenticated credentials: {} on request to: {}", ex.getMessage(), httpRequest.getRequestURL());
return new ResponseEntity<>(new ErrorResponse("Authentication credentials were not found."), HttpStatus.UNAUTHORIZED);
}

@ExceptionHandler(NoHandlerFoundException.class)
Expand All @@ -32,4 +29,14 @@ public ResponseEntity<ErrorResponse> handleNoHandlerFoundException(NoHandlerFoun
ErrorResponse errorResponse = new ErrorResponse("Internal Server Error! An unexpected error occurred. Please try again later. (2)");
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleGlobalException(Exception ex, WebRequest request, HttpServletRequest httpRequest) {

log.error("Unhandled exception on call to: {}", httpRequest.getRequestURL());
log.error("Unhandled exception", ex);

ErrorResponse errorResponse = new ErrorResponse("Internal Server Error! An unexpected error occurred. Please try again later.");
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

0 comments on commit f6694ca

Please sign in to comment.