Skip to content

Fix SQL logging and add catch-all exception handler (continuation w #11988)#12004

Open
nishantharkut wants to merge 3 commits intocBioPortal:masterfrom
nishantharkut:fix/11987-logging-and-catch-all
Open

Fix SQL logging and add catch-all exception handler (continuation w #11988)#12004
nishantharkut wants to merge 3 commits intocBioPortal:masterfrom
nishantharkut:fix/11987-logging-and-catch-all

Conversation

@nishantharkut
Copy link

@nishantharkut nishantharkut commented Mar 3, 2026

Fix #11987

Describe changes proposed in this pull request:

  • Replace ex.printStackTrace() in handleBadSqlGrammar with LOG.error("SQL grammar exception", ex) so SQL exceptions route through the SLF4J/Logback pipeline instead of writing directly to System.err
  • Add @ExceptionHandler(Exception.class) catch-all so all unhandled exceptions return a structured JSON ErrorResponse (HTTP 500) instead of leaking raw stack traces to API consumers
  • Add GlobalExceptionHandlerTests covering both changed methods

Note: the parameter type mismatch in handleCacheOperationException is already addressed by #11988 and is excluded from this PR intentionally.

Checks

Any screenshots or GIFs?

N/A — backend-only change, no visual output.

Notify reviewers

handleBadSqlGrammar was introduced by @LukeSikina, cache eviction handling by @pvannierop, and the most recent change to this file was by @gblaih.

Replace ex.printStackTrace() with LOG.error() in handleBadSqlGrammar
so SQL exceptions route through the SLF4J/Logback pipeline instead
of writing directly to System.err.

Add @ExceptionHandler(Exception.class) catch-all to ensure all
unhandled exceptions return a structured JSON ErrorResponse (HTTP 500)
instead of leaking raw stack traces to API consumers.

Add GlobalExceptionHandlerTests covering both changed methods.

Fixes cBioPortal#11987
Copilot AI review requested due to automatic review settings March 3, 2026 09:25
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the REST GlobalExceptionHandler to route SQL grammar exceptions through SLF4J logging and introduces a catch-all exception handler intended to ensure unhandled errors return a structured JSON ErrorResponse (500), alongside new unit tests.

Changes:

  • Replace printStackTrace() in handleBadSqlGrammar with LOG.error(...).
  • Add @ExceptionHandler(Exception.class) to return a generic 500 ErrorResponse for otherwise-unhandled exceptions.
  • Add GlobalExceptionHandlerTests for handleBadSqlGrammar and the new catch-all handler.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/main/java/org/cbioportal/application/rest/error/GlobalExceptionHandler.java Adds SLF4J logger usage, improves SQL exception logging, and adds a catch-all exception handler.
src/test/java/org/cbioportal/application/rest/error/GlobalExceptionHandlerTests.java Adds unit tests covering the updated SQL handler and the new catch-all handler.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +261 to +266
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleUncaughtException(Exception ex) {
LOG.error("Unhandled exception", ex);
return new ResponseEntity<>(
new ErrorResponse("An unexpected error occurred. Please contact your administrator."),
HttpStatus.INTERNAL_SERVER_ERROR);
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

@ExceptionHandler(Exception.class) will also intercept Spring MVC framework exceptions (e.g., HttpRequestMethodNotSupportedException, HttpMediaTypeNotSupportedException, etc.) that would otherwise be translated to correct 4xx/405 responses by Spring’s default resolvers. With this catch-all in place, those cases may start returning a generic 500 ErrorResponse instead. Consider narrowing the handler to RuntimeException (to avoid overriding framework 4xx handling) and/or adding explicit handlers for common Spring MVC exceptions with appropriate HTTP statuses, or extending ResponseEntityExceptionHandler to preserve the default mappings while still returning JSON.

Copilot uses AI. Check for mistakes.
@nishantharkut
Copy link
Author

@inodb @dippindots could you please add the bug label to this PR? The label-check CI will fail until a valid label from the release-drafter categories is applied. Thank you!

…ling

@ExceptionHandler(Exception.class) on its own intercepts Spring MVC
framework exceptions (HttpRequestMethodNotSupportedException,
HttpMediaTypeNotSupportedException, etc.) before they can be mapped
to correct 4xx responses. Extending ResponseEntityExceptionHandler
registers specific handlers for those exception types in the class
hierarchy; Spring picks the most specific type match, so framework
exceptions are handled by the parent while the catch-all still fires
for genuinely unhandled application exceptions.

Also removes the now-resolved TODO item about extending
ResponseEntityExceptionHandler.
@nishantharkut
Copy link
Author

Addressed Copilot's feedback on the catch-all handler.

@ExceptionHandler(Exception.class) on its own intercepts Spring MVC framework exceptions (HttpRequestMethodNotSupportedException → 405, HttpMediaTypeNotSupportedException → 415, etc.) before they can be translated to their correct 4xx responses — turning them all into 500s instead.

Fixed by extending ResponseEntityExceptionHandler. The parent class registers specific handlers for all those framework exception types in the class hierarchy. Spring's ExceptionHandlerMethodResolver picks the most specific type match, so the parent's handlers continue to fire for Spring MVC framework exceptions, while the Exception.class catch-all only fires for genuinely unhandled application exceptions.

This also resolves the TODO that was already in the file (// - consider extending extends ResponseEntityExceptionHandler).

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.

[BUG] GlobalExceptionHandler has type mismatch and uses printStackTrace()

2 participants