GUIDES
GUIDESDOCS
GUIDES
These docs are for v0.7.1. Click to read the latest docs for v0.13.

Exceptions

You can handle exception with resume_from DSL like this:

rescue_from Unauthorized do
  status 401
  "Unauthorized"
end
rescue_from [MatchError, UndefinedFunctionError] do
  status 500
  "Run time error"
end
rescue_from :all do
  status 500
  "Server Error"
end

It is compiled to:

def call(conn, opts) do
  try do
    super(conn, opts)
  rescue
    Unauthorized ->
      status 401
      "Unauthorized"
  end
end
def call(conn, opts) do
  try do
    super(conn, opts)
  rescue
    [MatchError, UndefinedFunctionError] ->
      status 500
	    "Run time error"
  end
end
def call(conn, opts) do
  try do
    super(conn, opts)
  rescue
    _ ->
      status 500
      "Server Error"
  end
end

Use resume_from/3 if you need the exception variable.

rescue_from [MatchError, UndefinedFunctionError], as: e do
  e |> IO.inspect
  
  status 500
  "Run time error"
end

and it is compiled to:

def call(conn, opts) do
  try do
    super(conn, opts)
  rescue
    e in [MatchError, UndefinedFunctionError] ->
      e |> IO.inspect
  
      status 500
      "Run time error"
  end
end