Skip to content

Instantly share code, notes, and snippets.

@jordi-chacon
Last active August 29, 2015 14:21
Show Gist options
  • Save jordi-chacon/3d408f57b437f89b5218 to your computer and use it in GitHub Desktop.
Save jordi-chacon/3d408f57b437f89b5218 to your computer and use it in GitHub Desktop.
Exception on 404 handling after calling Phoenix.Controller.json(conn, %{})
error_view.ex
defmodule FacebookService.ErrorView do
use FacebookService.Web, :view
def render("404.json-api", %{conn: conn}) do
conn
|> HTTPex.Plugs.SetContentTypeResponseHeader.call
|> Phoenix.Controller.json(%{})
end
def render("500.json-api", %{conn: conn}) do
conn
|> HTTPex.Plugs.SetContentTypeResponseHeader.call
|> Phoenix.Controller.json(%{})
end
# In case no render clause matches or no
# template is found, let's render it as 500
def template_not_found(_template, assigns) do
render "500.json-api", assigns
end
end
Server: localhost:4001 (http)
Request: GET /oops
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in Plug.Conn.resp/3
(plug) lib/plug/conn.ex:434: Plug.Conn.resp(%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{correlation_id: "1234", correlation_id_presence_failed?: false, kind: :error, layout: false, reason: %Phoenix.Router.NoRouteError{conn: %Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{correlation_id: "1234", correlation_id_presence_failed?: false, request_reception_timestamp: {1432, 537227, 191474}}, before_send: [#Function<0.101105578/1 in HTTPex.Plugs.HTTPLogger.call/2>], body_params: %{}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "localhost", method: "GET", owner: #PID<0.388.0>, params: %{"format" => "json-api"}, path_info: ["oops"], peer: {{127, 0, 0, 1}, 46052}, port: 4001, private: %{phoenix_endpoint: FacebookService.Endpoint, phoenix_pipelines: [], phoenix_router: FacebookService.Router}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"accept", "application/vnd.api+json"}, {"shim-correlation-id", "1234"}, {"user-agent", "hackney/1.1.0"}, {"host", "localhost:4001"}], resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}], scheme: :http, script_name: [], secret_key_base: "mcV+tU17meac6XZ+cvGLaV5+By4XXr24wR87fTTUVLy/G6w+E4umFDLYe72cBA5J", state: :unset, status: nil}, message: "no route found for GET /oops (FacebookService.Router)", plug_status: 404, router: FacebookService.Router}, request_reception_timestamp: {1432, 537227, 191474}, stack: [{FacebookService.Router, :match, 4, [file: 'web/router.ex', line: 1]}, {FacebookService.Router, :call, 2, [file: 'lib/phoenix/router.ex', line: 2]}, {FacebookService.Endpoint, :phoenix_endpoint_pipeline, 1, [file: 'lib/facebook_service/endpoint.ex', line: 1]}, {FacebookService.Endpoint, :call, 2, [file: 'lib/phoenix/endpoint/render_errors.ex', line: 34]}, {Phoenix.Endpoint.CowboyHandler, :upgrade, 4, [file: 'lib/phoenix/endpoint/cowboy_handler.ex', line: 52]}, {:cowboy_protocol, :execute, 4, [file: 'src/cowboy_protocol.erl', line: 435]}]}, before_send: [#Function<0.101105578/1 in HTTPex.Plugs.HTTPLogger.call/2>], body_params: %{}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "localhost", method: "GET", owner: #PID<0.388.0>, params: %{"format" => "json-api"}, path_info: ["oops"], peer: {{127, 0, 0, 1}, 46052}, port: 4001, private: %{phoenix_endpoint: FacebookService.Endpoint, phoenix_layout: false, phoenix_pipelines: [], phoenix_router: FacebookService.Router, phoenix_template: "404.json-api", phoenix_view: FacebookService.ErrorView}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"accept", "application/vnd.api+json"}, {"shim-correlation-id", "1234"}, {"user-agent", "hackney/1.1.0"}, {"host", "localhost:4001"}], resp_body: nil, resp_cookies: %{}, resp_headers: [{"content-type", "application/vnd.api+json; charset=utf-8"}, {"cache-control", "max-age=0, private, must-revalidate"}], scheme: :http, script_name: [], secret_key_base: "mcV+tU17meac6XZ+cvGLaV5+By4XXr24wR87fTTUVLy/G6w+E4umFDLYe72cBA5J", state: :unset, status: 404}, 404, %Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{correlation_id: "1234", correlation_id_presence_failed?: false, kind: :error, layout: false, reason: %Phoenix.Router.NoRouteError{conn: %Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{correlation_id: "1234", correlation_id_presence_failed?: false, request_reception_timestamp: {1432, 537227, 191474}}, before_send: [#Function<0.101105578/1 in HTTPex.Plugs.HTTPLogger.call/2>], body_params: %{}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "localhost", method: "GET", owner: #PID<0.388.0>, params: %{"format" => "json-api"}, path_info: ["oops"], peer: {{127, 0, 0, 1}, 46052}, port: 4001, private: %{phoenix_endpoint: FacebookService.Endpoint, phoenix_pipelines: [], phoenix_router: FacebookService.Router}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"accept", "application/vnd.api+json"}, {"shim-correlation-id", "1234"}, {"user-agent", "hackney/1.1.0"}, {"host", "localhost:4001"}], resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}], scheme: :http, script_name: [], secret_key_base: "mcV+tU17meac6XZ+cvGLaV5+By4XXr24wR87fTTUVLy/G6w+E4umFDLYe72cBA5J", state: :unset, status: nil}, message: "no route found for GET /oops (FacebookService.Router)", plug_status: 404, router: FacebookService.Router}, request_reception_timestamp: {1432, 537227, 191474}, stack: [{FacebookService.Router, :match, 4, [file: 'web/router.ex', line: 1]}, {FacebookService.Router, :call, 2, [file: 'lib/phoenix/router.ex', line: 2]}, {FacebookService.Endpoint, :phoenix_endpoint_pipeline, 1, [file: 'lib/facebook_service/endpoint.ex', line: 1]}, {FacebookService.Endpoint, :call, 2, [file: 'lib/phoenix/endpoint/render_errors.ex', line: 34]}, {Phoenix.Endpoint.CowboyHandler, :upgrade, 4, [file: 'lib/phoenix/endpoint/cowboy_handler.ex', line: 52]}, {:cowboy_protocol, :execute, 4, [file: 'src/cowboy_protocol.erl', line: 435]}]}, before_send: [#Function<0.101105578/1 in HTTPex.Plugs.HTTPLogger.call/2>], body_params: %{}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "localhost", method: "GET", owner: #PID<0.388.0>, params: %{"format" => "json-api"}, path_info: ["oops"], peer: {{127, 0, 0, 1}, 46052}, port: 4001, private: %{phoenix_endpoint: FacebookService.Endpoint, phoenix_layout: false, phoenix_pipelines: [], phoenix_router: FacebookService.Router, phoenix_template: "404.json-api", phoenix_view: FacebookService.ErrorView}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"accept", "application/vnd.api+json"}, {"shim-correlation-id", "1234"}, {"user-agent", "hackney/1.1.0"}, {"host", "localhost:4001"}], resp_body: nil, resp_cookies: %{}, resp_headers: [{"content-type", "application/json; charset=utf-8"}, {"cache-control", "max-age=0, private, must-revalidate"}], scheme: :http, script_name: [], secret_key_base: "mcV+tU17meac6XZ+cvGLaV5+By4XXr24wR87fTTUVLy/G6w+E4umFDLYe72cBA5J", state: :sent, status: 404})
(plug) lib/plug/conn.ex:424: Plug.Conn.send_resp/3
(phoenix) lib/phoenix/endpoint/cowboy_handler.ex:52: Phoenix.Endpoint.CowboyHandler.upgrade/4
(cowboy) src/cowboy_protocol.erl:435: :cowboy_protocol.execute/4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment