|
| 1 | +package com.amigoscode.exception; |
| 2 | + |
| 3 | +import jakarta.servlet.http.HttpServletRequest; |
| 4 | +import org.springframework.http.HttpStatus; |
| 5 | +import org.springframework.http.ResponseEntity; |
| 6 | +import org.springframework.validation.FieldError; |
| 7 | +import org.springframework.web.bind.MethodArgumentNotValidException; |
| 8 | +import org.springframework.web.bind.annotation.ControllerAdvice; |
| 9 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 10 | + |
| 11 | +import java.time.Instant; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Objects; |
| 14 | +import java.util.stream.Collectors; |
| 15 | + |
| 16 | +@ControllerAdvice |
| 17 | +public class GlobalExceptionHandler { |
| 18 | + |
| 19 | + @ExceptionHandler(ResourceNotFound.class) |
| 20 | + public ResponseEntity<ErrorResponse> handleResourceNotFound( |
| 21 | + ResourceNotFound ex, |
| 22 | + HttpServletRequest request |
| 23 | + ) { |
| 24 | + ErrorResponse errorResponse = new ErrorResponse( |
| 25 | + ex.getMessage(), |
| 26 | + HttpStatus.NOT_FOUND.getReasonPhrase(), |
| 27 | + HttpStatus.NOT_FOUND.value(), |
| 28 | + request.getRequestURI(), |
| 29 | + Instant.now(), |
| 30 | + null |
| 31 | + ); |
| 32 | + return new ResponseEntity<>( |
| 33 | + errorResponse, |
| 34 | + HttpStatus.NOT_FOUND |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + @ExceptionHandler(MethodArgumentNotValidException.class) |
| 39 | + public ResponseEntity<ErrorResponse> handleValidationException( |
| 40 | + MethodArgumentNotValidException ex, |
| 41 | + HttpServletRequest request |
| 42 | + ) { |
| 43 | + |
| 44 | + Map<String, String> errors = ex.getBindingResult() |
| 45 | + .getFieldErrors() |
| 46 | + .stream() |
| 47 | + .collect(Collectors.toMap( |
| 48 | + FieldError::getField, |
| 49 | + fieldError -> |
| 50 | + Objects.requireNonNullElse( |
| 51 | + fieldError.getDefaultMessage(), |
| 52 | + "no error available" |
| 53 | + ) |
| 54 | + )); |
| 55 | + ErrorResponse errorResponse = new ErrorResponse( |
| 56 | + ex.getMessage(), |
| 57 | + HttpStatus.BAD_REQUEST.getReasonPhrase(), |
| 58 | + HttpStatus.BAD_REQUEST.value(), |
| 59 | + request.getRequestURI(), |
| 60 | + Instant.now(), |
| 61 | + errors |
| 62 | + ); |
| 63 | + return new ResponseEntity<>( |
| 64 | + errorResponse, |
| 65 | + HttpStatus.BAD_REQUEST |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + @ExceptionHandler(Exception.class) |
| 70 | + public ResponseEntity<ErrorResponse> handleGenericException( |
| 71 | + Exception ex, |
| 72 | + HttpServletRequest request |
| 73 | + ) { |
| 74 | + ErrorResponse errorResponse = new ErrorResponse( |
| 75 | + ex.getMessage(), |
| 76 | + HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), |
| 77 | + HttpStatus.INTERNAL_SERVER_ERROR.value(), |
| 78 | + request.getRequestURI(), |
| 79 | + Instant.now(), |
| 80 | + null |
| 81 | + ); |
| 82 | + return new ResponseEntity<>( |
| 83 | + errorResponse, |
| 84 | + HttpStatus.INTERNAL_SERVER_ERROR |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments