Skip to content

After handler to turn null into 404 #3566

Description

@kliushnichenko

There is a typical use-case for get/find/fetch API like below

get("/users/{userId}", ctx -> {
  var userId = ctx.path("userId").value();
  return userRepository.findUserById(userId); // can return null, and this is OK for repo
}

where null can be returned from a route handler, some time ago the encoder was simply failing on this. Right now it returns just null as text. But it is still not a legal JSON response. The desired behavior is, typically, to return a 404 response in this case.

It is a bit boilerplate to add a null check in every handler like above, so, what I usually do in this situation, is just add a dead simple after() handler that does the null-check and throws 404.

Would it be OK if I add a similar after handler to the project?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions