11package app .common .infrastructure .exception ;
22
3+ import org .slf4j .Logger ;
4+ import org .slf4j .LoggerFactory ;
5+ import org .springframework .http .HttpStatus ;
6+ import org .springframework .http .ResponseEntity ;
37import org .springframework .web .bind .annotation .ExceptionHandler ;
48import org .springframework .web .bind .annotation .RestControllerAdvice ;
9+ import org .springframework .web .servlet .NoHandlerFoundException ;
10+ import org .springframework .web .HttpRequestMethodNotSupportedException ;
11+ import org .springframework .web .bind .MethodArgumentNotValidException ;
12+ import org .springframework .dao .DataAccessException ;
13+ import org .springframework .security .access .AccessDeniedException ;
14+
15+ import javax .validation .ConstraintViolationException ;
516
617import app .common .domain .model .common .BaseResponse ;
718
819@ RestControllerAdvice
920public class CustomExceptionHandler {
10- @ ExceptionHandler (Exception .class )
11- protected BaseResponse handleException (Exception e ) {
12- return BaseResponse .failResponse (e );
13- }
21+
22+ private static final Logger logger = LoggerFactory .getLogger (CustomExceptionHandler .class );
1423
1524 @ ExceptionHandler (CustomException .class )
16- protected BaseResponse handleCustomException (CustomException e ) {
17- return BaseResponse .failResponse (e .getResponseCode ());
25+ protected ResponseEntity <BaseResponse > handleCustomException (CustomException e ) {
26+ logger .error ("CustomException occurred: " , e );
27+ BaseResponse response = BaseResponse .failResponse (e .getResponseCode ());
28+ return new ResponseEntity <>(response , HttpStatus .BAD_REQUEST );
29+ }
30+
31+ @ ExceptionHandler (IllegalArgumentException .class )
32+ protected ResponseEntity <BaseResponse > handleIllegalArgumentException (IllegalArgumentException e ) {
33+ logger .error ("IllegalArgumentException occurred: " , e );
34+ BaseResponse response = BaseResponse .failResponse ("INVALID_ARGUMENT" );
35+ return new ResponseEntity <>(response , HttpStatus .BAD_REQUEST );
36+ }
37+
38+ @ ExceptionHandler (NoHandlerFoundException .class )
39+ protected ResponseEntity <BaseResponse > handleNoHandlerFoundException (NoHandlerFoundException e ) {
40+ logger .error ("NoHandlerFoundException occurred: " , e );
41+ BaseResponse response = BaseResponse .failResponse ("RESOURCE_NOT_FOUND" );
42+ return new ResponseEntity <>(response , HttpStatus .NOT_FOUND );
43+ }
44+
45+ @ ExceptionHandler (HttpRequestMethodNotSupportedException .class )
46+ protected ResponseEntity <BaseResponse > handleHttpRequestMethodNotSupportedException (HttpRequestMethodNotSupportedException e ) {
47+ logger .error ("HttpRequestMethodNotSupportedException occurred: " , e );
48+ BaseResponse response = BaseResponse .failResponse ("METHOD_NOT_ALLOWED" );
49+ return new ResponseEntity <>(response , HttpStatus .METHOD_NOT_ALLOWED );
1850 }
1951
52+ @ ExceptionHandler (MethodArgumentNotValidException .class )
53+ protected ResponseEntity <BaseResponse > handleMethodArgumentNotValidException (MethodArgumentNotValidException e ) {
54+ logger .error ("MethodArgumentNotValidException occurred: " , e );
55+ BaseResponse response = BaseResponse .failResponse ("INVALID_INPUT" );
56+ return new ResponseEntity <>(response , HttpStatus .BAD_REQUEST );
57+ }
58+
59+ @ ExceptionHandler (ConstraintViolationException .class )
60+ protected ResponseEntity <BaseResponse > handleConstraintViolationException (ConstraintViolationException e ) {
61+ logger .error ("ConstraintViolationException occurred: " , e );
62+ BaseResponse response = BaseResponse .failResponse ("CONSTRAINT_VIOLATION" );
63+ return new ResponseEntity <>(response , HttpStatus .BAD_REQUEST );
64+ }
65+
66+ @ ExceptionHandler (DataAccessException .class )
67+ protected ResponseEntity <BaseResponse > handleDataAccessException (DataAccessException e ) {
68+ logger .error ("DataAccessException occurred: " , e );
69+ BaseResponse response = BaseResponse .failResponse ("DATABASE_ERROR" );
70+ return new ResponseEntity <>(response , HttpStatus .INTERNAL_SERVER_ERROR );
71+ }
72+
73+ @ ExceptionHandler (AccessDeniedException .class )
74+ protected ResponseEntity <BaseResponse > handleAccessDeniedException (AccessDeniedException e ) {
75+ logger .error ("AccessDeniedException occurred: " , e );
76+ BaseResponse response = BaseResponse .failResponse ("ACCESS_DENIED" );
77+ return new ResponseEntity <>(response , HttpStatus .FORBIDDEN );
78+ }
79+
80+ @ ExceptionHandler (Exception .class )
81+ protected ResponseEntity <BaseResponse > handleException (Exception e ) {
82+ logger .error ("Unexpected exception occurred: " , e );
83+ BaseResponse response = BaseResponse .failResponse ("INTERNAL_SERVER_ERROR" );
84+ return new ResponseEntity <>(response , HttpStatus .INTERNAL_SERVER_ERROR );
85+ }
2086}
0 commit comments