@@ -3,16 +3,36 @@ defmodule PlugsnagTest do
3
3
use Plug.Test
4
4
5
5
defmodule TestException do
6
- defexception plug_status: 403 , message: "oops"
6
+ defexception plug_status: 503 , message: "oops"
7
+ end
8
+
9
+ defmodule NotFoundException do
10
+ defexception plug_status: 404 , message: "not found"
7
11
end
8
12
9
13
defmodule ErrorRaisingPlug do
10
14
defmacro __using__ ( _env ) do
11
15
quote do
12
16
def call ( conn , _opts ) do
13
- raise Plug.Conn.WrapperError , conn: conn ,
14
- kind: :error , stack: System . stacktrace ,
15
- reason: TestException . exception ( [ ] )
17
+ raise Plug.Conn.WrapperError ,
18
+ conn: conn ,
19
+ kind: :error ,
20
+ stack: System . stacktrace ( ) ,
21
+ reason: TestException . exception ( [ ] )
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ defmodule NotFoundRaisingPlug do
28
+ defmacro __using__ ( _env ) do
29
+ quote do
30
+ def call ( conn , _opts ) do
31
+ raise Plug.Conn.WrapperError ,
32
+ conn: conn ,
33
+ kind: :error ,
34
+ stack: System . stacktrace ( ) ,
35
+ reason: NotFoundException . exception ( [ ] )
16
36
end
17
37
end
18
38
end
@@ -23,66 +43,84 @@ defmodule PlugsnagTest do
23
43
use ErrorRaisingPlug
24
44
end
25
45
46
+ defmodule NotFoundPlug do
47
+ use Plugsnag
48
+ use NotFoundRaisingPlug
49
+ end
50
+
26
51
defmodule FakePlugsnag do
27
52
def report ( exception , options \\ [ ] ) do
28
- send self ( ) , { :report , { exception , options } }
53
+ send ( self ( ) , { :report , { exception , options } } )
29
54
end
30
55
end
31
56
32
57
setup do
33
58
Application . put_env ( :plugsnag , :reporter , FakePlugsnag )
34
59
end
35
60
36
- test "Raising an error on failure" do
37
- conn = conn ( :get , "/" )
61
+ describe "5xx exception" do
62
+ test "Raising an error on failure" do
63
+ conn = conn ( :get , "/" )
38
64
39
- assert_raise Plug.Conn.WrapperError , "** (PlugsnagTest.TestException) oops" , fn ->
40
- TestPlug . call ( conn , [ ] )
41
- end
65
+ assert_raise Plug.Conn.WrapperError , "** (PlugsnagTest.TestException) oops" , fn ->
66
+ TestPlug . call ( conn , [ ] )
67
+ end
42
68
43
- assert_received { :report , { % TestException { } , _ } }
44
- end
69
+ assert_received { :report , { % TestException { } , _ } }
70
+ end
45
71
46
- test "includes connection metadata in the report" do
47
- conn = conn ( :get , "/?hello=computer" )
72
+ test "includes connection metadata in the report" do
73
+ conn = conn ( :get , "/?hello=computer" )
48
74
49
- catch_error TestPlug . call ( conn , [ ] )
50
- assert_received { :report , { % TestException { } , options } }
51
- metadata = Keyword . get ( options , :metadata )
75
+ catch_error ( TestPlug . call ( conn , [ ] ) )
76
+ assert_received { :report , { % TestException { } , options } }
77
+ metadata = Keyword . get ( options , :metadata )
52
78
53
- assert get_in ( metadata , [ :request , :query_string ] ) == "hello=computer"
54
- end
79
+ assert get_in ( metadata , [ :request , :query_string ] ) == "hello=computer"
80
+ end
55
81
56
- test "allows modifying bugsnag report options before it's sent" do
57
- defmodule TestErrorReportBuilder do
58
- @ behaviour Plugsnag.ErrorReportBuilder
82
+ test "allows modifying bugsnag report options before it's sent" do
83
+ defmodule TestErrorReportBuilder do
84
+ @ behaviour Plugsnag.ErrorReportBuilder
59
85
60
- def build_error_report ( error_report , conn ) do
61
- user_info = % {
62
- id: conn |> get_req_header ( "x-user-id" ) |> List . first
63
- }
86
+ def build_error_report ( error_report , conn ) do
87
+ user_info = % {
88
+ id: conn |> get_req_header ( "x-user-id" ) |> List . first ( )
89
+ }
64
90
65
- % { error_report | user: user_info }
91
+ % { error_report | user: user_info }
92
+ end
66
93
end
67
- end
68
94
69
- defmodule TestPlugsnagCallbackPlug do
70
- use Plugsnag , error_report_builder: TestErrorReportBuilder
71
- use ErrorRaisingPlug
72
- end
95
+ defmodule TestPlugsnagCallbackPlug do
96
+ use Plugsnag , error_report_builder: TestErrorReportBuilder
97
+ use ErrorRaisingPlug
98
+ end
73
99
74
- conn = conn ( :get , "/" )
100
+ conn = conn ( :get , "/" )
75
101
76
- conn =
77
- conn
78
- |> put_req_header ( "x-user-id" , "abc123" )
102
+ conn =
103
+ conn
104
+ |> put_req_header ( "x-user-id" , "abc123" )
79
105
80
- catch_error TestPlugsnagCallbackPlug . call ( conn , [ ] )
81
- assert_received { :report , { % TestException { } , options } }
106
+ catch_error ( TestPlugsnagCallbackPlug . call ( conn , [ ] ) )
107
+ assert_received { :report , { % TestException { } , options } }
82
108
83
- assert Keyword . get ( options , :user ) == % {
84
- id: "abc123"
85
- }
109
+ assert Keyword . get ( options , :user ) == % {
110
+ id: "abc123"
111
+ }
112
+ end
86
113
end
87
114
115
+ describe "4xx exception" do
116
+ test "Raising an error on failure" do
117
+ conn = conn ( :get , "/" )
118
+
119
+ assert_raise Plug.Conn.WrapperError , "** (PlugsnagTest.NotFoundException) not found" , fn ->
120
+ NotFoundPlug . call ( conn , [ ] )
121
+ end
122
+
123
+ refute_receive { :report , { % NotFoundException { } , _ } } , 100
124
+ end
125
+ end
88
126
end
0 commit comments