Packages
Ewebmachine contains macros and plugs to allow you to compose HTTP decision handlers and run the HTTP decision tree to get your HTTP response. This project is a rewrite for Elixir and Plug of basho webmachine.
Current section
Files
Jump to
Current section
Files
lib/ewebmachine/plug.error_as_forward.ex
defmodule Ewebmachine.Plug.ErrorAsForward do
@moduledoc """
This plug take an argument `forward_pattern` (default to `"/error/:status"`),
and, when the current response status is an error, simply forward to a `GET`
to the path defined by the pattern and this status.
"""
@doc false
def init(opts), do: (opts[:forward_pattern] || "/error/:status")
@doc false
def call(%{status: code, state: :set}=conn, pattern) when code > 399 do
path = pattern |> String.slice(1..-1) |> String.replace(":status", to_string(code)) |> String.split("/")
%{ conn | path_info: path, method: "GET", state: :unset }
end
def call(conn, _), do: conn
end