Skip to content
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

Return Bad Request for Jackson client exceptions #717

Open
darioseidl opened this issue Dec 15, 2021 · 2 comments
Open

Return Bad Request for Jackson client exceptions #717

darioseidl opened this issue Dec 15, 2021 · 2 comments
Labels

Comments

@darioseidl
Copy link

darioseidl commented Dec 15, 2021

The problem-spring-web library already handles many Java and Spring exceptions out-of-the-box, which is great. In any project where we're using it, we added some additional advice traits for common exceptions. Maybe it would make sense to handle them directly in the library?

Jackson parsing and input exceptions:

  • MismatchedInputException

General exception type used as the base class for all {@link JsonMappingException}s
that are due to input not mapping to target definition; these are typically
considered "client errors" since target type definition itself is not the root cause
but mismatching input. This is in contrast to {@link InvalidDefinitionException} which
signals a problem with target type definition and not input.

so this exception should clearly return a 400 status code.

  • JsonParseException

Exception type for parsing problems, used when non-well-formed content
(content that does not conform to JSON syntax as per specification)
is encountered.

this happens on invalid JSON, so that should also return a 400 status code.

We're handling this as such (in Kotlin),

/**
 * Return [Status.BAD_REQUEST] for exceptions indicating bad requests.
 */
@ControllerAdvice
@Order(0)
class BadRequestAdvice : AdviceTrait {

    /**
     * Handle [JsonParseException]s, thrown when JSON cannot be parsed because of invalid syntax.
     */
    @ExceptionHandler
    fun handle(exception: JsonParseException, request: NativeWebRequest): ResponseEntity<Problem> =
        create(Status.BAD_REQUEST, exception, request)
    
    /**
     * Handle [MismatchedInputException]s, thrown when JSON input cannot be mapped to the target type.
     */
    @ExceptionHandler
    fun handle(exception: MismatchedInputException, request: NativeWebRequest): ResponseEntity<Problem> =
        create(Status.BAD_REQUEST, exception, request)

}

I'm not quite sure if there are other Jackson exceptions that indicate client errors, but I believe those two cover most cases.

If it makes sense to add that, I could provide a pull request.

@whiskeysierra
Copy link
Collaborator

Yeah, that sounds useful. Do we already have a dependency on Jackson?
If not, then I would add an optional dependency and offer an advice trait à la carte, i.e. one that isn't part of e.g. ProblemHandling.

@darioseidl
Copy link
Author

There's a dependency on jackson-databind already (not optional, as far as I can see).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants