Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include response headers in error report #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/plugsnag/basic_error_report_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ defmodule Plugsnag.BasicErrorReportBuilder do
scheme: conn.scheme,
query_string: conn.query_string,
params: filter(:params, conn.params),
headers: collect_req_headers(conn),
req_headers: collect_headers(conn, :req),
resp_headers: collect_headers(conn, :resp),
client_ip: format_ip(conn.remote_ip)
}
}
end

defp collect_req_headers(conn) do
headers = Enum.reduce(conn.req_headers, %{}, fn({header, _}, acc) ->
Map.put(acc, header, Plug.Conn.get_req_header(conn, header) |> List.first)
defp collect_headers(conn, type) do
headers = Enum.reduce(Map.get(conn, :"#{type}_headers"), %{}, fn({header, _}, acc) ->
Map.put(acc, header, apply(Plug.Conn, :"get_#{type}_header", [conn, header]) |> List.first)
end)
filter(:headers, headers)
end
Expand Down
9 changes: 7 additions & 2 deletions test/plugsnag/basic_error_report_builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Plugsnag.BasicErrorReportBuilderTest do
conn
|> put_req_header("accept", "application/json")
|> put_req_header("x-user-id", "abc123")
|> put_resp_header("x-request-id", "def456")

error_report = BasicErrorReportBuilder.build_error_report(
%ErrorReport{}, conn
Expand All @@ -27,10 +28,14 @@ defmodule Plugsnag.BasicErrorReportBuilderTest do
scheme: :http,
query_string: "hello=computer",
params: %{"hello" => "computer"},
headers: %{
req_headers: %{
"accept" => "application/json",
"x-user-id" => "abc123"
},
resp_headers: %{
"x-request-id" => "def456",
"cache-control" => "max-age=0, private, must-revalidate"
},
client_ip: "127.0.0.1"
}
}
Expand Down Expand Up @@ -63,7 +68,7 @@ defmodule Plugsnag.BasicErrorReportBuilderTest do
assert %ErrorReport{
metadata: %{
request: %{
headers: %{"authorization" => "[FILTERED]"}
req_headers: %{"authorization" => "[FILTERED]"}
}
}
} = error_report
Expand Down