elixir-maru / maru

Elixir RESTful Framework
https://maru.readme.io
BSD 3-Clause "New" or "Revised" License
1.32k stars 85 forks source link

add `:plug_status` defaults for known exceptions #81

Closed brentspell closed 7 years ago

brentspell commented 7 years ago

I really like where maru is going, and it is so much simpler than phoenix for APIs.

These changes add defaults for standard HTTP status codes for maru's built-in exception types (InvalidFormat, Validation, NotFound, and MethodNotAllowed). the :plug_status field is used by the Plug.Exception protocol (https://hexdocs.pm/plug/Plug.Exception.html) to map exception types to HTTP status codes. Plug.Exception falls back to 500 if the field doesn't exist and the exception type doesn't implement the exception protocol. This allows you to do the following:

rescue_from :all, as: e do
  conn
  |> put_status(Plug.Exception.status(e))
  |> text("Request Failed")
end

The status codes can be overridden at raise time, but these defaults seemed sensible for the built-in exception types.

falood commented 7 years ago

@brentspell Thank you! ❤️ I like this idea very much!