Skip to Content
Phoenix Web Development
book

Phoenix Web Development

by Brandon Richey
April 2018
Beginner to intermediate content levelBeginner to intermediate
406 pages
9h 33m
English
Packt Publishing
Content preview from Phoenix Web Development

Adding an Error Handler for 404s for JSON

Phoenix, as of version 1.3, included the ability to define fallback controllers or plugs to handle scenarios where a conn is not returned back out. We can harness this to write custom error handling code for our API to catch these scenarios!

We'll start off by creating a new controller called ErrorController for our API. Create lib/vocial_web/controllers/api/error_controller.ex:

defmodule VocialWeb.Api.ErrorController do  use VocialWeb, :controllerend

Next, for a handler controller, you need to define a call function that will attempt to look for a conn as the first argument and pattern match against the second argument. We'll just implement a simple 404 handler for purposes of this example:

def call(conn, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Phoenix in Action

Phoenix in Action

Geoffrey Lessel
Real-Time Phoenix

Real-Time Phoenix

Stephen Bussey

Publisher Resources

ISBN: 9781787284197Supplemental Content