Skip to content

Commit

Permalink
improve the internal server error exception handler by adding time st…
Browse files Browse the repository at this point in the history
…amp, error message and details
  • Loading branch information
yesyash committed Jul 27, 2024
1 parent 53173b7 commit 44a1356
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.RDS.skilltree.utils.GenericResponse;
import jakarta.validation.ConstraintViolationException;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.websocket.AuthenticationException;
import org.springframework.http.HttpStatus;
Expand All @@ -15,6 +14,11 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
Expand Down Expand Up @@ -139,8 +143,16 @@ public ResponseEntity<?> handleForbiddenException(ForbiddenException ex, WebRequ
@ExceptionHandler(InternalServerErrorException.class)
public ResponseEntity<?> handleInternalServerErrorException(
InternalServerErrorException ex, WebRequest request) {
log.error("Exception - Error : {}", ex.getMessage(), ex);
return new ResponseEntity<>(
new GenericResponse<>(null, ex.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
log.error("Internal Server Error", ex);
// Create a more specific error message based on the exception type or cause
String errorMessage = "An unexpected error occurred.";

// Consider adding more details to the response for debugging
Map<String, Object> errorDetails = new HashMap<>();
errorDetails.put("timestamp", LocalDateTime.now());
errorDetails.put("message", errorMessage);
errorDetails.put("details", ex.getMessage()); // Include exception details for debugging

return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

0 comments on commit 44a1356

Please sign in to comment.