Skip to content

Enhance global exception handler with additional exception types#525

Open
devin-ai-integration[bot] wants to merge 1 commit intomasterfrom
devin/1775707443-exception-handler
Open

Enhance global exception handler with additional exception types#525
devin-ai-integration[bot] wants to merge 1 commit intomasterfrom
devin/1775707443-exception-handler

Conversation

@devin-ai-integration
Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration bot commented Apr 9, 2026

Summary

Centralizes exception handling in CustomizeExceptionHandler by adding 4 new handler methods and removing @ResponseStatus annotations from exception classes that are now handled explicitly.

New handlers added to CustomizeExceptionHandler.java:

  • ResourceNotFoundException → 404 {"errors": {"resource": ["not found"]}}
  • NoAuthorizationException → 403 {"errors": {"authorization": ["forbidden"]}}
  • HttpMessageNotReadable (override) → 422 {"errors": {"body": ["malformed request body"]}}
  • Generic Exception fallback → 500 {"errors": {"server": ["internal server error"]}}

Annotation cleanup:

  • Removed @ResponseStatus(HttpStatus.NOT_FOUND) from ResourceNotFoundException.java
  • Removed @ResponseStatus(HttpStatus.FORBIDDEN) from NoAuthorizationException.java

Status codes are now controlled solely by the handler methods, and all error responses follow a consistent {"errors": {...}} structure.

All 68 existing tests pass. Formatted with ./gradlew spotlessJavaApply.

Review & Testing Checklist for Human

  • Generic Exception.class handler swallows all details — the catch-all handler returns a generic 500 body but does not log the original exception. Unexpected errors will be silently lost. Consider whether at minimum a logger.error(ex.getMessage(), ex) should be added.
  • No new tests for the 4 new handlers — existing tests pass but don't explicitly verify the new structured response bodies. Recommend adding integration tests that trigger each new handler and assert on status code + body shape (especially the generic fallback and malformed JSON cases).
  • Verify ResourceNotFoundException / NoAuthorizationException callers — with @ResponseStatus removed, these exceptions rely entirely on the @RestControllerAdvice handler to set the HTTP status. Confirm there are no code paths (e.g., non-controller contexts, async handlers) where the advice might not apply.

Notes

  • The handleHttpMessageNotReadable override signature matches Spring Boot 2.6.x (HttpStatus param, not HttpStatusCode). If the project upgrades to Spring Boot 3.x, this signature will need updating.
  • The anonymous HashMap subclass pattern used in the new handlers is consistent with the existing handleInvalidAuthentication method.

Link to Devin session: https://app.devin.ai/sessions/05dc1548e50f478697cfd9c84fd27149
Requested by: @tobydrinkall


Open with Devin

- Add ResourceNotFoundException handler (404 with structured error body)
- Add NoAuthorizationException handler (403 with structured error body)
- Override handleHttpMessageNotReadable for malformed JSON (422)
- Add generic Exception fallback handler (500)
- Remove @ResponseStatus annotations from ResourceNotFoundException and NoAuthorizationException
- Add HttpMessageNotReadableException import

Co-Authored-By: Toby Drinkall <toby.drinkall@cognition.ai>
@devin-ai-integration
Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Copy link
Copy Markdown
Author

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant