UNOMI-928: Improve REST API error handling with dedicated exception mappers#771
Merged
Conversation
…appers Add JsonMappingExceptionMapper and InternalServerErrorExceptionMapper so Jackson deserialization failures return 400 badRequest instead of 500. Enrich RuntimeExceptionMapper with sanitized request-context logging. Extract shared logic into AbstractRestExceptionMapper and LogSanitizer. Add unit tests. Backported from unomi-3-dev.
Contributor
There was a problem hiding this comment.
Pull request overview
Backports improved REST exception mapping and logging so Jackson deserialization failures are treated as client errors (HTTP 400) and REST error responses are more consistent and safer to log.
Changes:
- Added dedicated exception mappers for
JsonMappingExceptionandInternalServerErrorException(with JSON-cause downgrade to 400). - Refactored shared behavior into
AbstractRestExceptionMapperand introducedLogSanitizerfor request-context logging sanitization. - Added JUnit 5 unit tests covering mapper status/body contracts and sanitizer behavior, plus a test dependency in
rest/pom.xml.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| rest/src/main/java/org/apache/unomi/rest/exception/RuntimeExceptionMapper.java | Refactors to shared base mapper and enhances request-context/root-cause logging. |
| rest/src/main/java/org/apache/unomi/rest/exception/AbstractRestExceptionMapper.java | Adds shared helpers for request context, root-cause handling, JSON detection, and standard JSON error responses. |
| rest/src/main/java/org/apache/unomi/rest/exception/LogSanitizer.java | Centralizes sanitization/truncation for request-derived log fields to mitigate log-injection/verbosity risks. |
| rest/src/main/java/org/apache/unomi/rest/exception/JsonMappingExceptionMapper.java | New mapper: JsonMappingException → 400 with standard JSON error body. |
| rest/src/main/java/org/apache/unomi/rest/exception/InternalServerErrorExceptionMapper.java | New mapper: InternalServerErrorException → 400 when JSON root-cause, else 500 with sanitized logging. |
| rest/src/test/java/org/apache/unomi/rest/exception/RestExceptionMapperTest.java | New unit tests validating mapper status codes and JSON error bodies. |
| rest/src/test/java/org/apache/unomi/rest/exception/LogSanitizerTest.java | New unit tests validating sanitizer behavior (control chars, truncation, whitelisting). |
| rest/pom.xml | Adds JUnit Jupiter dependency for the new unit tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
The & separator was appended before the overflow guard, producing a spurious & immediately before ...[more params]. Reorder the check so the sentinel is appended directly after the last real parameter. Add tests for queryString() and queryParameters() — the latter covers the boundary at exactly 10 params and the truncation case at 11.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
JIRA: UNOMI-928 — Improve REST API error handling with dedicated exception mappers and enhanced logging
Base:
master(standalone backport — not stacked)Summary
Backport REST error-handling improvements from
unomi-3-devso Jackson deserialization failures are reported as client errors (HTTP 400) instead of leaking as HTTP 500, with sanitized request-context logging and consistent JSON error bodies.What changed
JsonMappingExceptionMapper— mapsJsonMappingException→400 {"errorMessage":"badRequest"}InternalServerErrorExceptionMapper— mapsInternalServerErrorException; downgrades to400 badRequestwhen root cause is JSON deserialization (JsonMappingException/JsonParseException), otherwise500 internalServerErrorwith detailed sanitized loggingRuntimeExceptionMapper— enriched logging (request method/URI/query, root cause, WARN vs ERROR by cause type); response remains500(JSON causes only affect log level, not status)AbstractRestExceptionMapper(request context, root cause, standard JSON responses) andLogSanitizer(log-injection defenses, length limits)RestExceptionMapperTest(6) andLogSanitizerTest(7); addsjunit-jupitertest dependency torest/pom.xmlAll mappers register via existing
@Provider+@Component(service = ExceptionMapper.class)— no blueprint/feature wiring changes.Behavior notes for reviewers
JsonMappingException(direct){"errorMessage":"badRequest"}InternalServerErrorExceptionwith JSON deserialization root cause{"errorMessage":"badRequest"}InternalServerErrorExceptionwith non-JSON root cause{"errorMessage":"internalServerError"}RuntimeException(including JSON root cause){"errorMessage":"internalServerError"}Existing validation paths (
InvalidRequestExceptionMapper, schema validation on custom deserializers) are unchanged and may still return different 400 bodies (plain text) before these mappers apply.Out of scope / follow-up
ItemDeserializerrobustness gap: malformed non-objectItemJSON (e.g."source":"string") still throwsClassCastException→ 500 viaRuntimeExceptionMapper. Present inunomi-3-devtoo; candidate for a separate JIRA (not part of this backport).Test plan
mvn -pl rest -am test -Dtest='RestExceptionMapperTest,LogSanitizerTest' -Dsurefire.failIfNoSpecifiedTests=false./build.sh/ integration test suite (reviewer)badRequestwhere applicable