zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

exceptions.ex (2014B)


      1 # This file defines the Plug.Exception protocol and
      2 # the exceptions that implement such protocol.
      3 
      4 defprotocol Plug.Exception do
      5   @moduledoc """
      6   A protocol that extends exceptions to be status-code aware.
      7 
      8   By default, it looks for an implementation of the protocol,
      9   otherwise checks if the exception has the `:plug_status` field
     10   or simply returns 500.
     11   """
     12 
     13   @fallback_to_any true
     14 
     15   @type action :: %{label: String.t(), handler: {module(), atom(), list()}}
     16 
     17   @doc """
     18   Receives an exception and returns its HTTP status code.
     19   """
     20   @spec status(t) :: Plug.Conn.status()
     21   def status(exception)
     22 
     23   @doc """
     24   Receives an exception and returns the possible actions that could be triggered for that error.
     25   Should return a list of actions in the following structure:
     26 
     27       %{
     28         label: "Text that will be displayed in the button",
     29         handler: {Module, :function, [args]}
     30       }
     31 
     32   Where:
     33 
     34     * `label` a string/binary that names this action
     35     * `handler` a MFArgs that will be executed when this action is triggered
     36 
     37   It will be rendered in the `Plug.Debugger` generated error page as buttons showing the `label`
     38   that upon pressing executes the MFArgs defined in the `handler`.
     39 
     40   ## Examples
     41 
     42       defimpl Plug.Exception, for: ActionableExample do
     43         def actions(_), do: [%{label: "Print HI", handler: {IO, :puts, ["Hi!"]}}]
     44       end
     45   """
     46   @spec actions(t) :: [action()]
     47   def actions(exception)
     48 end
     49 
     50 defimpl Plug.Exception, for: Any do
     51   def status(%{plug_status: status}), do: Plug.Conn.Status.code(status)
     52   def status(_), do: 500
     53   def actions(_exception), do: []
     54 end
     55 
     56 defmodule Plug.BadRequestError do
     57   @moduledoc """
     58   The request will not be processed due to a client error.
     59   """
     60 
     61   defexception message: "could not process the request due to client error", plug_status: 400
     62 end
     63 
     64 defmodule Plug.TimeoutError do
     65   @moduledoc """
     66   Timeout while waiting for the request.
     67   """
     68 
     69   defexception message: "timeout while waiting for request data", plug_status: 408
     70 end