Skip to content

Commit 9fb22b4

Browse files
Merge pull request #77 from getsentry/better-error-log
better error logging
2 parents dd64631 + 43dbfa5 commit 9fb22b4

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

lib/sentry/client.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ defmodule Sentry.Client do
4848
end
4949
end
5050

51-
defp request(method, url, headers, body) do
51+
def request(method, url, headers, body) do
5252
case :hackney.request(method, url, headers, body, []) do
5353
{:ok, 200, _headers, client} ->
5454
case :hackney.body(client) do
@@ -61,6 +61,10 @@ defmodule Sentry.Client do
6161
log_api_error(body)
6262
:error
6363
end
64+
{:ok, status, headers, client} ->
65+
error_header = :proplists.get_value("X-Sentry-Error", headers, "")
66+
log_api_error("#{body}\nReceived #{status} from Sentry server: #{error_header}")
67+
:error
6468
_ ->
6569
log_api_error(body)
6670
:error

test/client_test.exs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
defmodule Sentry.ClientTest do
2-
use ExUnit.Case, async: true
2+
use ExUnit.Case
3+
import ExUnit.CaptureLog
4+
35
alias Sentry.Client
46
@sentry_dsn "https://public:[email protected]/1"
57

@@ -8,11 +10,33 @@ defmodule Sentry.ClientTest do
810
assert Client.authorization_header(public_key, private_key) =~ ~r/Sentry sentry_version=5, sentry_client=sentry-elixir\/#{Application.spec(:sentry, :vsn)}, sentry_timestamp=\d{10}, sentry_key=public, sentry_secret=secret/
911
end
1012

11-
test "parning dsn" do
13+
test "parsing dsn" do
1214
assert {"https://app.getsentry.com:443/api/1/store/", "public", "secret"} =
1315
Sentry.Client.parse_dsn!("https://public:[email protected]/1")
1416

1517
assert {"http://app.getsentry.com:9000/api/1/store/", "public", "secret"} =
1618
Sentry.Client.parse_dsn!("http://public:[email protected]:9000/1")
1719
end
20+
21+
test "logs api errors" do
22+
bypass = Bypass.open
23+
Bypass.expect bypass, fn conn ->
24+
{:ok, _body, conn} = Plug.Conn.read_body(conn)
25+
assert conn.request_path == "/api/1/store/"
26+
assert conn.method == "POST"
27+
Plug.Conn.put_resp_header(conn, "X-Sentry-Error", "Creation of this event was denied due to rate limiting.")
28+
|> Plug.Conn.resp(400, "Something bad happened")
29+
end
30+
31+
Application.put_env(:sentry, :dsn, "http://public:secret@localhost:#{bypass.port}/1")
32+
33+
try do
34+
Event.not_a_function
35+
rescue
36+
e ->
37+
assert capture_log(fn ->
38+
Sentry.capture_exception(e)
39+
end) =~ ~r/400.*Creation of this event was denied due to rate limiting/
40+
end
41+
end
1842
end

test/logger_test.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ defmodule Sentry.LoggerTest do
1515
end
1616

1717
Application.put_env(:sentry, :dsn, "http://public:secret@localhost:#{bypass.port}/1")
18-
Application.put_env(:sentry, :included_environments, [:test])
1918
:error_logger.add_report_handler(Sentry.Logger)
2019

2120
capture_log fn ->

test/support/test_client.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ defmodule Sentry.TestClient do
22
def send_event(%Sentry.Event{} = event) do
33
{endpoint, _public_key, _secret_key} = Sentry.Client.parse_dsn!(Application.fetch_env!(:sentry, :dsn))
44
body = Poison.encode!(event)
5-
:hackney.request(:post, endpoint, [], body, [])
5+
Sentry.Client.request(:post, endpoint, [], body)
66
end
77
end

0 commit comments

Comments
 (0)