Fix SQL logging and add catch-all exception handler (continuation w #11988)#12004
Fix SQL logging and add catch-all exception handler (continuation w #11988)#12004nishantharkut wants to merge 3 commits intocBioPortal:masterfrom
Conversation
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
There was a problem hiding this comment.
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()inhandleBadSqlGrammarwithLOG.error(...). - Add
@ExceptionHandler(Exception.class)to return a generic 500ErrorResponsefor otherwise-unhandled exceptions. - Add
GlobalExceptionHandlerTestsforhandleBadSqlGrammarand 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.
| @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); |
There was a problem hiding this comment.
@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.
|
@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.
|
Addressed Copilot's feedback on the catch-all handler.
Fixed by extending This also resolves the TODO that was already in the file ( |
Fix #11987
Describe changes proposed in this pull request:
ex.printStackTrace()inhandleBadSqlGrammarwithLOG.error("SQL grammar exception", ex)so SQL exceptions route through the SLF4J/Logback pipeline instead of writing directly toSystem.err@ExceptionHandler(Exception.class)catch-all so all unhandled exceptions return a structured JSONErrorResponse(HTTP 500) instead of leaking raw stack traces to API consumersGlobalExceptionHandlerTestscovering both changed methodsNote: the parameter type mismatch in
handleCacheOperationExceptionis 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
handleBadSqlGrammarwas introduced by @LukeSikina, cache eviction handling by @pvannierop, and the most recent change to this file was by @gblaih.