amberframework / amber

A Crystal web framework that makes building applications fast, simple, and enjoyable. Get started with quick prototyping, less bugs, and blazing fast performance.
https://amberframework.org
MIT License
2.57k stars 204 forks source link

Enhancement: Automatically return 400 response if param validation fails #1327

Open a-alhusaini opened 1 year ago

a-alhusaini commented 1 year ago

Right now, if a validation fails the developer needs to write a block similar to this one in order to return a response:

unless params.valid?
  response.puts {errors: params.errors}.to_json
  response.status_code 400
end

A 400 response with the appropriate body should be the default procedure should a call to validate! fail. This simplifies error handling especially in situations where the response is json because in that scenario, you rarely want to return custom output in situations where the client created an invalid request.

In situations where the user has a page then we can easily return a default error page. We should have default error pages anyway so we may as well add them while we add this enhancement to controller error handling.

hugopl commented 8 months ago

If you implement your own Error pipe and use it instead of Amber::Pipe::Error you can have what you want. Not ideal since you end up needing to copy some code from Amber::Pipe::Error but at least it's possible to do what you want.