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

Ability to convert IllegalArgumentException as an HTTP 400 (Bad Request) response #126

Closed
picaron opened this issue Jul 26, 2017 · 7 comments
Assignees
Labels
Milestone

Comments

@picaron
Copy link

picaron commented Jul 26, 2017

Currently, if we return null from a controller, the response is automatically translated to a 404 not found which is very convenient. However for any thrown errors, the response is converted to a 500 internal server error.

However, when designing a REST API, it is very common that we want to return a 400 Bad Request because the client request parameters are invalid.

It would be more useful if we could simply throw an IllegalArgumentException and have that exception translated to a 400 Bad Request response.

Maybe there is a convenient mechanism for handling this but I looked at all the documentation and the examples and couldn't find any clues on how to implement this is a nice way.

@picaron picaron changed the title Ability to convert IllegalArgumentException as a 400 Bad Request response Ability to convert IllegalArgumentException as an HTTP 400 (Bad Request) response Jul 26, 2017
@nmihajlovski
Copy link
Contributor

Hi, please take a look at On.error(...). E.g.

On.error(SecurityException.class).handler((req1, resp, e) -> "ON SEC");

@picaron
Copy link
Author

picaron commented Aug 2, 2017

Hi, that seems to do what I need
Thanks!

@picaron picaron closed this as completed Aug 2, 2017
@picaron picaron reopened this Aug 3, 2017
@picaron
Copy link
Author

picaron commented Aug 3, 2017

Hi @nmihajlovski !

I'm reopening this because using the On.error(...) handlers doesn't work when using POJO annotation based controllers.

@picaron
Copy link
Author

picaron commented Aug 8, 2017

Hi @nmihajlovski ,

Thanks for the fast follow-up

I tried updating to 5.4.1, I still have the same issue. The errors I throw are not handled by my custom handlers.

My POJO controller isn't registered using App.beans, I'm using the @Controller annotation. Also the REST endpoint are annotated using @Transaction

@picaron
Copy link
Author

picaron commented Aug 8, 2017

One more interesting behavior:

If before setting up my On.error handlers I configure a My.wrappers handler, then my On.error handlers will work 😮

@nmihajlovski
Copy link
Contributor

Hi @picaron, I created a new issue for this (#133).

@picaron
Copy link
Author

picaron commented Aug 12, 2017

Ok thanks for the follow-up @nmihajlovski

I'll close this one then.

@picaron picaron closed this as completed Aug 12, 2017
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