|
1 | 1 | package inu.codin.codinticketingsse.common.exception; |
2 | 2 |
|
3 | 3 | import inu.codin.codinticketingsse.common.response.ExceptionResponse; |
| 4 | +import inu.codin.codinticketingsse.security.exception.SecurityErrorCode; |
| 5 | +import inu.codin.codinticketingsse.security.exception.SecurityException; |
| 6 | +import inu.codin.codinticketingsse.sse.exception.SseErrorCode; |
| 7 | +import inu.codin.codinticketingsse.sse.exception.SseException; |
| 8 | +import jakarta.validation.ConstraintViolationException; |
4 | 9 | import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.springframework.core.convert.ConversionFailedException; |
5 | 11 | import org.springframework.http.HttpStatus; |
6 | | -import org.springframework.http.MediaType; |
7 | 12 | import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.security.access.AccessDeniedException; |
| 14 | +import org.springframework.web.bind.MethodArgumentNotValidException; |
8 | 15 | import org.springframework.web.bind.annotation.ControllerAdvice; |
9 | 16 | import org.springframework.web.bind.annotation.ExceptionHandler; |
| 17 | +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; |
| 18 | +import org.springframework.web.multipart.support.MissingServletRequestPartException; |
10 | 19 |
|
11 | 20 | @ControllerAdvice |
12 | 21 | @Slf4j |
13 | 22 | public class GlobalExceptionHandler { |
14 | 23 |
|
15 | 24 | @ExceptionHandler(Exception.class) |
16 | 25 | protected ResponseEntity<ExceptionResponse> handleException(Exception e) { |
17 | | - return ResponseEntity.status(HttpStatus.NOT_FOUND) |
18 | | - .contentType(MediaType.APPLICATION_JSON) |
19 | | - .body(new ExceptionResponse( e.getMessage(), HttpStatus.NOT_FOUND.value())); |
| 26 | + log.warn("[Exception] Class: {}, Error Message : {}, Stack Trace: {}", |
| 27 | + e.getClass().getSimpleName(), |
| 28 | + e.getMessage(), |
| 29 | + e.getStackTrace()[0].toString()); |
| 30 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 31 | + .body(new ExceptionResponse(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR.value())); |
20 | 32 | } |
21 | 33 |
|
22 | 34 | @ExceptionHandler(GlobalException.class) |
23 | 35 | protected ResponseEntity<ExceptionResponse> handleGlobalException(GlobalException e) { |
| 36 | + log.warn("[GlobalException] Class: {}, Error Message : {}", e.getClass().getSimpleName(), e.getMessage()); |
24 | 37 | GlobalErrorCode code = e.getErrorCode(); |
25 | 38 | return ResponseEntity.status(code.httpStatus()) |
26 | 39 | .body(new ExceptionResponse(e.getMessage(), code.httpStatus().value())); |
27 | 40 | } |
| 41 | + |
| 42 | + @ExceptionHandler(SseException.class) |
| 43 | + public ResponseEntity<ExceptionResponse> handleSseException(SseException e) { |
| 44 | + log.warn("[SseException] Class: {}, Error Message : {}", e.getClass().getSimpleName(), e.getMessage()); |
| 45 | + SseErrorCode code = e.getErrorCode(); |
| 46 | + return ResponseEntity.status(code.httpStatus()) |
| 47 | + .body(new ExceptionResponse(e.getMessage(), code.httpStatus().value())); |
| 48 | + } |
| 49 | + |
| 50 | + @ExceptionHandler(SecurityException.class) |
| 51 | + public ResponseEntity<ExceptionResponse> handleSecurityException(SecurityException e) { |
| 52 | + log.warn("[SecurityException] Class: {}, Error Message : {}", e.getClass().getSimpleName(), e.getMessage()); |
| 53 | + SecurityErrorCode code = e.getSecurityErrorCode(); |
| 54 | + return ResponseEntity.status(code.httpStatus()) |
| 55 | + .body(new ExceptionResponse(e.getMessage(), code.httpStatus().value())); |
| 56 | + } |
| 57 | + |
| 58 | + @ExceptionHandler(MethodArgumentNotValidException.class) |
| 59 | + public ResponseEntity<ExceptionResponse> handleValidationException(MethodArgumentNotValidException e) { |
| 60 | + log.warn("[MethodArgumentNotValidException] Error Message : {}", e.getMessage()); |
| 61 | + return ResponseEntity.status(HttpStatus.BAD_REQUEST) |
| 62 | + .body(new ExceptionResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value())); |
| 63 | + } |
| 64 | + |
| 65 | + @ExceptionHandler(ConstraintViolationException.class) |
| 66 | + public ResponseEntity<ExceptionResponse> handleConstraintViolationException(ConstraintViolationException e) { |
| 67 | + log.warn("[ConstraintViolationException] Error Message : {}", e.getMessage()); |
| 68 | + return ResponseEntity.status(HttpStatus.BAD_REQUEST) |
| 69 | + .body(new ExceptionResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value())); |
| 70 | + } |
| 71 | + |
| 72 | + @ExceptionHandler(AccessDeniedException.class) |
| 73 | + public ResponseEntity<ExceptionResponse> handleAccessDeniedException(AccessDeniedException e) { |
| 74 | + log.warn("[AccessDeniedException] Error Message : {}", e.getMessage()); |
| 75 | + return ResponseEntity.status(HttpStatus.FORBIDDEN) |
| 76 | + .body(new ExceptionResponse(e.getMessage(), HttpStatus.FORBIDDEN.value())); |
| 77 | + } |
| 78 | + |
| 79 | + @ExceptionHandler(MissingServletRequestPartException.class) |
| 80 | + public ResponseEntity<ExceptionResponse> handleMissingServletRequestPartException(MissingServletRequestPartException e) { |
| 81 | + log.warn("[MissingServletRequestPartException] Error Message : {}", e.getMessage()); |
| 82 | + return ResponseEntity.status(HttpStatus.BAD_REQUEST) |
| 83 | + .body(new ExceptionResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value())); |
| 84 | + } |
| 85 | + |
| 86 | + @ExceptionHandler(MethodArgumentTypeMismatchException.class) |
| 87 | + public ResponseEntity<ExceptionResponse> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) { |
| 88 | + log.warn("[MethodArgumentTypeMismatchException] Error Message : {}", e.getMessage()); |
| 89 | + return ResponseEntity.status(HttpStatus.BAD_REQUEST) |
| 90 | + .body(new ExceptionResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value())); |
| 91 | + } |
| 92 | + |
| 93 | + @ExceptionHandler(ConversionFailedException.class) |
| 94 | + public ResponseEntity<ExceptionResponse> handleConversionFailedException(ConversionFailedException e) { |
| 95 | + log.warn("[ConversionFailedException] Error Message : {}", e.getMessage()); |
| 96 | + return ResponseEntity.status(HttpStatus.BAD_REQUEST) |
| 97 | + .body(new ExceptionResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value())); |
| 98 | + } |
28 | 99 | } |
0 commit comments