Skip to content

Instantly share code, notes, and snippets.

@jordi-chacon
Last active August 31, 2015 14:53
Show Gist options
  • Save jordi-chacon/e7d8a2611118e482f907 to your computer and use it in GitHub Desktop.
Save jordi-chacon/e7d8a2611118e482f907 to your computer and use it in GitHub Desktop.
When I send a request to my Elixir application which causes an
exception to be thrown from my request handler, *sometimes*,
the server seems to go down. The following error is printed:
#PID<0.613.0> running UserService.Endpoint terminated
Server: localhost:4001 (http)
Request: GET /v1/users/1234
** (exit) an exception was raised:
** (Poison.EncodeError) unable to encode value: {Plug.Adapters.Cowboy.Conn, {:http_req, #Port<0.9678>, :ranch_tcp, :keepalive, #PID<0.613.0>, "GET", :"HTTP/1.1", {{127, 0, 0, 1}, 48857}, "localhost", :undefined, 4001, "/v1/users/1234", :undefined, "", :undefined, [], [{"accept", "application/vnd.api+json"}, {"correlation-id", "1234"}, {"user-agent", "hackney/1.3.1"}, {"host", "localhost:4001"}], [], :undefined, [], :waiting, "", :undefined, false, :done, [], "", :undefined}}
(poison) lib/poison/encoder.ex:339: Poison.Encoder.Any.encode/2
(poison) lib/poison/encoder.ex:213: anonymous fn/4 in Poison.Encoder.Map.encode/3
(poison) lib/poison/encoder.ex:214: Poison.Encoder.Map."-encode/3-lists^foldl/2-0-"/3
(poison) lib/poison/encoder.ex:214: Poison.Encoder.Map.encode/3
(poison) lib/poison.ex:41: Poison.encode!/2
(phoenix) lib/phoenix/controller.ex:628: Phoenix.Controller.do_render/4
(phoenix) lib/phoenix/endpoint/render_errors.ex:58: Phoenix.Endpoint.RenderErrors.__catch__/5
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
This is my controller:
defmodule UserService.ShowUserController do
use UserService.Web, :controller
plug HTTPex.Plugs.SetContentTypeResponseHeader
def show(conn, params) do
conn
|> assign(:uid, params["uid"])
|> read_user
|> response_body
end
defp read_user(conn) do
assign(conn, :user, UserService.User.read!(conn.assigns[:uid]))
end
defp response_body(conn) do
user = conn.assigns[:user]
response = %{"data" =>
%{"type" => "users",
"id" => user.uid,
"attributes" =>
%{
}
}
}
json(conn, response)
end
end
This is my error_view.ex:
defmodule UserService.ErrorView do
use UserService.Web, :view
import Plug.Conn
def render("500.json-api", %{conn: conn}) do
conn
|> put_resp_content_type("application/vnd.api+json")
|> 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
This is how the conn object looks like right before returning
from the error_view.ex:
%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...},
assigns: %{correlation_id: "1234", correlation_id_presence_failed?: false,
kind: :error, layout: false,
log_fun: #Function<0.60254788/2 in HTTPex.Logger.put_log_fun/1>,
reason: %MatchError{term: []},
request_reception_timestamp: {1441, 32439, 199996},
stack: [{Storex.ETSStore, :read!, 2,
[file: 'lib/storex/ets_store.ex', line: 14]},
{UserService.ShowUserController, :read_user, 1,
[file: 'web/controllers/show_user_controller.ex', line: 14]},
{UserService.ShowUserController, :show, 2,
[file: 'web/controllers/show_user_controller.ex', line: 9]},
{UserService.ShowUserController, :phoenix_controller_pipeline, 2,
[file: 'web/controllers/show_user_controller.ex', line: 1]},
{UserService.Router, :dispatch, 2,
[file: 'lib/phoenix/router.ex', line: 265]},
{UserService.Router, :do_call, 2, [file: 'web/router.ex', line: 1]},
{UserService.Endpoint, :phoenix_pipeline, 1,
[file: 'lib/user_service/endpoint.ex', line: 1]},
{UserService.Endpoint, :call, 2,
[file: 'lib/phoenix/endpoint/render_errors.ex', line: 34]}]},
before_send: [#Function<0.118836940/1 in HTTPex.Plugs.HTTPLogger.call/2>],
body_params: %{}, cookies: %Plug.Conn.Unfetched{aspect: :cookies},
halted: false, host: "localhost", method: "GET", owner: #PID<0.622.0>,
params: %{"uid" => "1234"}, path_info: ["v1", "users", "1234"],
peer: {{127, 0, 0, 1}, 44178}, port: 4001,
private: %{UserService.Router => {[], %{}},
:phoenix_endpoint => UserService.Endpoint, :phoenix_format => "json-api",
:phoenix_layout => false, :phoenix_pipelines => [],
:phoenix_route => #Function<6.87727686/1 in UserService.Router.match/4>,
:phoenix_router => UserService.Router, :phoenix_template => "500.json-api",
:phoenix_view => UserService.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"},
{"correlation-id", "1234"}, {"user-agent", "hackney/1.3.1"},
{"host", "localhost:4001"}], request_path: "/v1/users/1234", resp_body: nil,
resp_cookies: %{},
resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"},
{"content-type", "application/vnd.api+json; charset=utf-8"}], scheme: :http,
script_name: [],
secret_key_base: "mcV+tU17meac6XZ+cvGLaV5+By4XXr24wR87fTTUVLy/G6w+E4umFDLYe72cBA5J",
state: :sent, status: 500}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment