Handling errors from Plug.Parser

For file sanity checks that is being uploaded to phoenix ,

  • To validate mime, filename,etc
    • we get a Plug.Upload after being parsed by Plug.Parser, in actions.
  • but to put file size limit , Plug.Parsers are used.
    • in router
    • or in endpoint

But if file is large than given certain threshold, it raises.
exceptions.

defmodule RequestTooLargeError do
    @moduledoc """
    Error raised when the request is too large.
    """

    defexception message:
                   "the request is too large. If you are willing to process " <>
                     "larger requests, please give a :length to Plug.Parsers",
                 plug_status: 413
  end

how do i show info to the user, that what happened?. Instead of internal server error

defmodule RTValues do
  @moduledoc false

  def size_limit(), do: Application.get_env(:core, :size_limit)

  def parser_options(),i
    do: [
      parsers: [
        :urlencoded,
        {:multipart, length: size_limit()},
        :json
      ],
      pass: ["*/*"],
      json_decoder: Phoenix.json_library()
    ]
end
defmodule Web.Router do

    pipeline :browser do
      plug Plug.Parsers,RTValues.parser_options()
     .
     . 
     .
    end
    .
    .
    .
  end

how do i show info to the user, that what happened?. Instead of internal server error
what am i doing wrong?
SAME GOES FOR API ONLY ENDPOINT HANDLING

The plug upload limit is more of a safety feature than a validation. Consider using something like live view uploads if you want a more modern and interactive upload experience that will make it easier to provide feedback on stuff like file size.

1 Like

what about api endpoints?

use Plug.ErrorHandler
but dont know how to use that
i tried calling Parser from custom parser but that also didint work