22
33import com .RDS .skilltree .utils .GenericResponse ;
44import jakarta .validation .ConstraintViolationException ;
5- import java .time .LocalDateTime ;
6- import java .util .HashMap ;
75import java .util .List ;
8- import java .util .Map ;
96import lombok .extern .slf4j .Slf4j ;
107import org .apache .tomcat .websocket .AuthenticationException ;
118import org .springframework .http .HttpStatus ;
1613import org .springframework .web .bind .MethodArgumentNotValidException ;
1714import org .springframework .web .bind .annotation .ControllerAdvice ;
1815import org .springframework .web .bind .annotation .ExceptionHandler ;
19- import org .springframework .web .context .request .WebRequest ;
2016
2117@ Slf4j
2218@ ControllerAdvice
2319public class GlobalExceptionHandler {
20+
2421 @ ExceptionHandler ({NoEntityException .class })
2522 public ResponseEntity <GenericResponse <Object >> handleNoEntityException (NoEntityException ex ) {
26- log .error ("NoEntityException - Error : {}" , ex .getMessage (), ex );
27- return ResponseEntity .status (HttpStatus .NOT_FOUND )
28- .body (new GenericResponse <>(null , ex .getMessage ()));
23+ log .error ("NoEntityException - Error : {}" , ex .getMessage ());
24+ return ResponseEntity .status (HttpStatus .NOT_FOUND ).body (new GenericResponse <>(ex .getMessage ()));
2925 }
3026
3127 @ ExceptionHandler ({AuthenticationException .class , InsufficientAuthenticationException .class })
@@ -40,25 +36,21 @@ public ResponseEntity<GenericResponse<Object>> handleInvalidBearerTokenException
4036 @ ExceptionHandler ({AccessDeniedException .class })
4137 public ResponseEntity <GenericResponse <Object >> handleAccessDeniedException (
4238 AccessDeniedException ex ) {
43- return ResponseEntity .status (HttpStatus .FORBIDDEN )
44- .body (new GenericResponse <>(null , ex .getMessage ()));
39+ return ResponseEntity .status (HttpStatus .FORBIDDEN ).body (new GenericResponse <>(ex .getMessage ()));
4540 }
4641
4742 @ ExceptionHandler ({EntityAlreadyExistsException .class })
4843 public ResponseEntity <GenericResponse <Object >> handleEntityAlreadyExistsException (
4944 EntityAlreadyExistsException ex ) {
50- log .error ("EntityAlreadyExistsException - Error : {}" , ex .getMessage (), ex );
51- return ResponseEntity .status (HttpStatus .CONFLICT )
52- .body (new GenericResponse <>(null , ex .getMessage ()));
45+ log .error ("EntityAlreadyExistsException - Error : {}" , ex .getMessage ());
46+ return ResponseEntity .status (HttpStatus .CONFLICT ).body (new GenericResponse <>(ex .getMessage ()));
5347 }
5448
5549 @ ExceptionHandler ({RuntimeException .class })
5650 public ResponseEntity <GenericResponse <Object >> handleRuntimeException (RuntimeException ex ) {
57- log .error ("Runtime Exception - Error : {}" , ex .getMessage (), ex );
51+ log .error ("Runtime Exception - Error : {}" , ex .getMessage ());
5852 return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR )
59- .body (
60- new GenericResponse <>(
61- null , "Runtime Exception - Something went wrong, please try again." ));
53+ .body (new GenericResponse <>("Runtime Exception - Something went wrong, please try again." ));
6254 }
6355
6456 @ ExceptionHandler ({MethodArgumentNotValidException .class })
@@ -73,85 +65,81 @@ public ResponseEntity<GenericResponse<Object>> handleMethodArgumentNotValidExcep
7365 }
7466 log .error ("MethodArgumentNotValidException Exception - Error : {}" , ex .getMessage (), ex );
7567 return ResponseEntity .status (HttpStatus .BAD_REQUEST )
76- .body (new GenericResponse <>(null , errorString .toString ().trim ()));
68+ .body (new GenericResponse <>(errorString .toString ().trim ()));
7769 }
7870
7971 @ ExceptionHandler ({Exception .class })
8072 public ResponseEntity <GenericResponse <Object >> handleException (Exception ex ) {
81- log .error ("Exception - Error : {}" , ex .getMessage (), ex );
73+ log .error ("Exception - Error : {}" , ex .getMessage ());
8274 return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR )
83- .body (new GenericResponse <>(null , "Something unexpected happened, please try again." ));
75+ .body (new GenericResponse <>("Something unexpected happened, please try again." ));
8476 }
8577
8678 @ ExceptionHandler ({InvalidParameterException .class })
8779 public ResponseEntity <GenericResponse <Object >> handleException (InvalidParameterException ex ) {
88- log .error ("Exception - Error : {}" , ex .getMessage (), ex );
80+ log .error ("InvalidParameterException - Error : {}" , ex .getMessage ());
8981 return ResponseEntity .status (HttpStatus .BAD_REQUEST )
90- .body (new GenericResponse <>(null , ex .getMessage ()));
82+ .body (new GenericResponse <>(ex .getMessage ()));
9183 }
9284
9385 @ ExceptionHandler ({ConstraintViolationException .class })
9486 public ResponseEntity <GenericResponse <Object >> handleException (ConstraintViolationException ex ) {
95- log .error ("Exception - Error : {}" , ex .getMessage (), ex );
87+ log .error ("ConstraintViolationException - Error : {}" , ex .getMessage ());
9688 return ResponseEntity .status (HttpStatus .BAD_REQUEST )
97- .body (new GenericResponse <>(null , ex .getMessage ()));
89+ .body (new GenericResponse <>(ex .getMessage ()));
9890 }
9991
10092 @ ExceptionHandler (UserNotFoundException .class )
101- public ResponseEntity <?> handleUserNotFoundException (
102- UserNotFoundException ex , WebRequest request ) {
103- log .error ("Exception - Error : {}" , ex .getMessage (), ex );
104- return new ResponseEntity <>(new GenericResponse <>(null , ex .getMessage ()), HttpStatus .NOT_FOUND );
93+ public ResponseEntity <?> handleUserNotFoundException (UserNotFoundException ex ) {
94+ log .error ("UserNotFoundException - Error : {}" , ex .getMessage ());
95+ return new ResponseEntity <>(new GenericResponse <>(ex .getMessage ()), HttpStatus .NOT_FOUND );
10596 }
10697
10798 @ ExceptionHandler (SkillAlreadyExistsException .class )
108- public ResponseEntity <?> handleSkillAlreadyExistsException (
109- SkillAlreadyExistsException ex , WebRequest request ) {
110- log .error ("Exception - Error : {}" , ex .getMessage (), ex );
111- return new ResponseEntity <>(new GenericResponse <>(null , ex .getMessage ()), HttpStatus .CONFLICT );
99+ public ResponseEntity <?> handleSkillAlreadyExistsException (SkillAlreadyExistsException ex ) {
100+ log .error ("SkillAlreadyExistsException - Error : {}" , ex .getMessage ());
101+ return new ResponseEntity <>(new GenericResponse <>(ex .getMessage ()), HttpStatus .CONFLICT );
112102 }
113103
114104 @ ExceptionHandler (SelfEndorsementNotAllowedException .class )
115105 public ResponseEntity <?> handleSelfEndorsementNotAllowedException (
116- SelfEndorsementNotAllowedException ex , WebRequest request ) {
117- log .error ("Exception - Error : {}" , ex .getMessage (), ex );
106+ SelfEndorsementNotAllowedException ex ) {
107+ log .error ("SelfEndorsementNotAllowedException - Error : {}" , ex .getMessage ());
118108 return new ResponseEntity <>(
119- new GenericResponse <>(null , ex .getMessage ()), HttpStatus .METHOD_NOT_ALLOWED );
109+ new GenericResponse <>(ex .getMessage ()), HttpStatus .METHOD_NOT_ALLOWED );
120110 }
121111
122112 @ ExceptionHandler (SkillNotFoundException .class )
123- public ResponseEntity <?> handleSkillNotFoundException (
124- SkillNotFoundException ex , WebRequest request ) {
125- log .error ("Exception - Error : {}" , ex .getMessage (), ex );
126- return new ResponseEntity <>(new GenericResponse <>(null , ex .getMessage ()), HttpStatus .NOT_FOUND );
113+ public ResponseEntity <?> handleSkillNotFoundException (SkillNotFoundException ex ) {
114+ log .error ("SkillNotFoundException - Error : {}" , ex .getMessage ());
115+ return new ResponseEntity <>(new GenericResponse <>(ex .getMessage ()), HttpStatus .NOT_FOUND );
127116 }
128117
129118 @ ExceptionHandler (EndorsementNotFoundException .class )
130- public ResponseEntity <?> handleEndorsementNotException (
131- EndorsementNotFoundException ex , WebRequest request ) {
132- log .error ("Exception - Error : {}" , ex .getMessage (), ex );
133- return new ResponseEntity <>(new GenericResponse <>(null , ex .getMessage ()), HttpStatus .NOT_FOUND );
119+ public ResponseEntity <?> handleEndorsementNotFoundException (EndorsementNotFoundException ex ) {
120+ log .error ("EndorsementNotFoundException - Error : {}" , ex .getMessage ());
121+ return new ResponseEntity <>(new GenericResponse <>(ex .getMessage ()), HttpStatus .NOT_FOUND );
134122 }
135123
136124 @ ExceptionHandler (ForbiddenException .class )
137- public ResponseEntity <?> handleForbiddenException (ForbiddenException ex , WebRequest request ) {
138- log .error ("Exception - Error : {}" , ex .getMessage (), ex );
139- return new ResponseEntity <>(new GenericResponse <>(null , ex .getMessage ()), HttpStatus .FORBIDDEN );
125+ public ResponseEntity <?> handleForbiddenException (ForbiddenException ex ) {
126+ log .error ("ForbiddenException - Error : {}" , ex .getMessage ());
127+ return new ResponseEntity <>(new GenericResponse <>(ex .getMessage ()), HttpStatus .FORBIDDEN );
140128 }
141129
142130 @ ExceptionHandler (InternalServerErrorException .class )
143- public ResponseEntity <?> handleInternalServerErrorException (
144- InternalServerErrorException ex , WebRequest request ) {
131+ public ResponseEntity <?> handleInternalServerErrorException (InternalServerErrorException ex ) {
145132 log .error ("Internal Server Error" , ex );
146- // Create a more specific error message based on the exception type or cause
147133 String errorMessage = "An unexpected error occurred." ;
134+ return new ResponseEntity <>(
135+ new GenericResponse <>(errorMessage ), HttpStatus .INTERNAL_SERVER_ERROR );
136+ }
148137
149- // Consider adding more details to the response for debugging
150- Map <String , Object > errorDetails = new HashMap <>();
151- errorDetails .put ("timestamp" , LocalDateTime .now ());
152- errorDetails .put ("message" , errorMessage );
153- errorDetails .put ("details" , ex .getMessage ()); // Include exception details for debugging
154-
155- return new ResponseEntity <>(errorDetails , HttpStatus .INTERNAL_SERVER_ERROR );
138+ @ ExceptionHandler (EndorsementAlreadyExistsException .class )
139+ public ResponseEntity <?> handleEndorsementAlreadyExistsException (
140+ EndorsementAlreadyExistsException ex ) {
141+ log .error ("EndorsementAlreadyExistsException - Error : {}" , ex .getMessage ());
142+ return new ResponseEntity <>(
143+ new GenericResponse <>(ex .getMessage ()), HttpStatus .METHOD_NOT_ALLOWED );
156144 }
157145}
0 commit comments