413-RequestTooLargeError How to Handle Gracefully?

Hi all,

I’m writing an elixir app, which can accept multipart/formdata but when the data size if more than 8MB, it raises an exception, but what I want to do is, capture the exception and send a nice message back to the user? I’ve even tried to use a try do in the endpoint but it is of no use.

@chrismccord? Any help regarding this would be appreciable!

Thanks.

1 Like

try do in the endpoint

On a plug macro?

You’d probably want to catch the exception in Plug.ErrorHandler instead or something like that.

You can also implement a Plug.Exception for it to happen “automatically”, I think:

defimpl Plug.Exception, for: <your error> do
  def status(_), do: 413
end

But I’d expect phoenix and plug do this by default. As far as I remember, plug just reraises the error after “gracefully” sending 413 (or some other code) to the client.