-
Notifications
You must be signed in to change notification settings - Fork 0
Implement global exception handling with custom error responses and e… #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
173f0f7
Implement global exception handling with custom error responses and e…
codebyNorthsteep 56862b8
Enhance global exception handling with structured error responses and…
codebyNorthsteep 05412f6
Enhance global exception handling with themed error messages for time…
codebyNorthsteep d95e6b5
Enhance global exception handling with themed error messages for time…
codebyNorthsteep 6e8f163
Enhance global exception handling by removing ResponseEntityException…
codebyNorthsteep 267f682
Enhance global exception handling by improving error message handling…
codebyNorthsteep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/org/example/projectbifrost/exception/ApiErrorResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package org.example.projectbifrost.exception; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public record ApiErrorResponse(LocalDateTime timestamp, | ||
| int status, | ||
| String message) // ← Detailed message for error | ||
|
|
||
| { | ||
| } |
81 changes: 81 additions & 0 deletions
81
src/main/java/org/example/projectbifrost/exception/GlobalExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package org.example.projectbifrost.exception; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.ErrorResponse; | ||
| import org.springframework.web.bind.MethodArgumentNotValidException; | ||
| import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.client.ResourceAccessException; | ||
| import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @ControllerAdvice | ||
| @Slf4j //Structured logging | ||
| public class GlobalExceptionHandler { | ||
|
|
||
| @ExceptionHandler(LLMException.class) | ||
| public ResponseEntity<ApiErrorResponse> handleLLMException(LLMException ex) { | ||
| HttpStatus status; | ||
| try { | ||
| status = HttpStatus.valueOf(ex.getStatusCode()); //Get the error from OpenRouter | ||
| } catch (IllegalArgumentException e) { | ||
| status = HttpStatus.INTERNAL_SERVER_ERROR; | ||
| } | ||
|
|
||
| log.warn("LLM Error [{}]: {}", status.value(), ex.getMessage()); | ||
|
|
||
| return new ResponseEntity<>( | ||
| new ApiErrorResponse(LocalDateTime.now(), status.value(), ex.getMessage()), | ||
| status | ||
| ); | ||
| } | ||
|
|
||
| @ExceptionHandler(ResourceAccessException.class) | ||
| public ResponseEntity<ApiErrorResponse> handleTimeoutException(ResourceAccessException ex) { | ||
| log.warn("Timeout connecting to LLM: {}", ex.getMessage()); | ||
| return new ResponseEntity<>( | ||
| new ApiErrorResponse(LocalDateTime.now(), 504, "Connection timeout. The Gods are taking too long to respond."), | ||
| HttpStatus.GATEWAY_TIMEOUT | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| @ExceptionHandler(MethodArgumentNotValidException.class) | ||
| public ResponseEntity<ApiErrorResponse> handleValidationException(MethodArgumentNotValidException ex) { | ||
| log.warn("Validation error - {} field(s) invalid", ex.getBindingResult().getErrorCount()); | ||
| return new ResponseEntity<>( | ||
| new ApiErrorResponse(LocalDateTime.now(), 400, "Invalid request - check your input"), | ||
| HttpStatus.BAD_REQUEST | ||
| ); | ||
| } | ||
|
|
||
| @ExceptionHandler(org.springframework.http.converter.HttpMessageNotReadableException.class) | ||
| public ResponseEntity<ApiErrorResponse> handleUnreadableMessage(org.springframework.http.converter.HttpMessageNotReadableException ex) { | ||
| log.warn("Malformed request body: {}", ex.getMostSpecificCause().getMessage()); | ||
| return new ResponseEntity<>( | ||
| new ApiErrorResponse(LocalDateTime.now(), HttpStatus.BAD_REQUEST.value(), "Malformed request body"), | ||
| HttpStatus.BAD_REQUEST | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| @ExceptionHandler(Exception.class) | ||
| public ResponseEntity<ApiErrorResponse> handleGeneralException(Exception ex) { | ||
| //If Spring has something to say about an error(e.g 404), use it! | ||
| if (ex instanceof ErrorResponse er) { | ||
| var status = er.getStatusCode(); | ||
| String message = ex.getMessage() != null ? ex.getMessage() : "Request failed"; | ||
| return ResponseEntity.status(status).body( | ||
| new ApiErrorResponse(LocalDateTime.now(), status.value(), message) | ||
| ); | ||
| } | ||
| log.error("Unexpected error: ", ex); | ||
| return new ResponseEntity<>( | ||
| new ApiErrorResponse(LocalDateTime.now(), 500, "An unexpected error occurred"), | ||
| HttpStatus.INTERNAL_SERVER_ERROR | ||
| ); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
15 changes: 15 additions & 0 deletions
15
src/main/java/org/example/projectbifrost/exception/LLMException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package org.example.projectbifrost.exception; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class LLMException extends RuntimeException { | ||
| private final int statusCode; | ||
| private final String model; | ||
|
|
||
| public LLMException(String message, String model, int statusCode) { | ||
| super(message); | ||
| this.statusCode = statusCode; | ||
| this.model = model; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.