@@ -8,12 +8,13 @@ module Resources
8
8
9
9
before do
10
10
allow ( ErrorHandler ) . to receive ( :logger ) . and_return ( logger )
11
+ allow ( SecureRandom ) . to receive ( :urlsafe_base64 ) . and_return ( "bYWfn-+yWPlf" )
11
12
end
12
13
13
14
let ( :logger ) { double ( 'logger' ) . as_null_object }
14
15
let ( :error ) { StandardError . new ( 'test error' ) }
15
16
let ( :thing ) { double ( 'thing' , call : nil , another_call : nil ) }
16
- let ( :options ) { { env : env } }
17
+ let ( :options ) { { env : env , error_reference : "bYWfnyWPlf" } }
17
18
let ( :request ) { double ( 'request' ) }
18
19
let ( :response ) { double ( 'response' , :body= => nil ) }
19
20
let ( :env ) { double ( 'env' ) }
@@ -37,33 +38,59 @@ module Resources
37
38
subject
38
39
end
39
40
40
- context "when the error is a PactBroker::Error or subclass" do
41
- let ( :error ) { Class . new ( PactBroker ::Error ) . new ( 'test error' ) }
42
-
43
- it "does not invoke the api error reporters" do
44
- expect ( thing ) . to_not receive ( :call ) . with ( error , options )
45
- subject
46
- end
47
- end
48
-
49
- it "creates a json error response body" do
41
+ it "includes an error reference" do
50
42
expect ( response ) . to receive ( :body= ) do | body |
51
- expect ( JSON . parse ( body ) [ 'error' ] ) . to include 'message ' => 'test error'
43
+ expect ( JSON . parse ( body ) [ 'error' ] ) . to include 'reference ' => "bYWfnyWPlf"
52
44
end
53
45
subject
54
46
end
55
47
56
-
57
48
context "when show_backtrace_in_error_response? is true" do
58
49
before do
59
50
allow ( PactBroker . configuration ) . to receive ( :show_backtrace_in_error_response? ) . and_return ( true )
60
51
end
61
52
62
- it "includes the backtrace in the error response" do
63
- expect ( response ) . to receive ( :body= ) do | body |
64
- expect ( body ) . to include ( "backtrace" )
53
+ context "when the error is a PactBroker::Error or subclass" do
54
+ let ( :error ) { Class . new ( PactBroker ::Error ) . new ( 'test error' ) }
55
+
56
+ it "does not invoke the api error reporters" do
57
+ expect ( thing ) . to_not receive ( :call ) . with ( error , options )
58
+ subject
59
+ end
60
+
61
+ it "uses the error message as the message" do
62
+ expect ( response ) . to receive ( :body= ) do | body |
63
+ expect ( JSON . parse ( body ) [ 'error' ] ) . to include 'message' => "test error"
64
+ end
65
+ subject
66
+ end
67
+
68
+ it "includes the backtrace in the error response" do
69
+ expect ( response ) . to receive ( :body= ) do | body |
70
+ expect ( body ) . to include ( "backtrace" )
71
+ end
72
+ subject
73
+ end
74
+ end
75
+ context "when the error is not a PactBroker::Error or subclass" do
76
+ it "invokes the api error reporters" do
77
+ expect ( thing ) . to receive ( :call ) . with ( error , options )
78
+ subject
79
+ end
80
+
81
+ it "uses the error message as the message" do
82
+ expect ( response ) . to receive ( :body= ) do | body |
83
+ expect ( JSON . parse ( body ) [ 'error' ] ) . to include 'message' => "test error"
84
+ end
85
+ subject
86
+ end
87
+
88
+ it "includes the backtrace in the error response" do
89
+ expect ( response ) . to receive ( :body= ) do | body |
90
+ expect ( body ) . to include ( "backtrace" )
91
+ end
92
+ subject
65
93
end
66
- subject
67
94
end
68
95
end
69
96
@@ -72,11 +99,47 @@ module Resources
72
99
allow ( PactBroker . configuration ) . to receive ( :show_backtrace_in_error_response? ) . and_return ( false )
73
100
end
74
101
75
- it "does not include the backtrace in the error response" do
76
- expect ( response ) . to receive ( :body= ) do | body |
77
- expect ( body ) . to_not include ( "backtrace" )
102
+ context "when the error is a PactBroker::Error or subclass" do
103
+ let ( :error ) { Class . new ( PactBroker ::Error ) . new ( 'test error' ) }
104
+
105
+ it "does not invoke the api error reporters" do
106
+ expect ( thing ) . to_not receive ( :call ) . with ( error , options )
107
+ subject
108
+ end
109
+
110
+ it "uses the error message as the message" do
111
+ expect ( response ) . to receive ( :body= ) do | body |
112
+ expect ( JSON . parse ( body ) [ 'error' ] ) . to include 'message' => "test error"
113
+ end
114
+ subject
115
+ end
116
+
117
+ it "does not include the backtrace in the error response" do
118
+ expect ( response ) . to receive ( :body= ) do | body |
119
+ expect ( body ) . to_not include ( "backtrace" )
120
+ end
121
+ subject
122
+ end
123
+ end
124
+ context "when the error is not a PactBroker::Error or subclass" do
125
+ it "invokes the api error reporters" do
126
+ expect ( thing ) . to receive ( :call ) . with ( error , options )
127
+ subject
128
+ end
129
+
130
+ it "uses a hardcoded error message" do
131
+ expect ( response ) . to receive ( :body= ) do | body |
132
+ expect ( JSON . parse ( body ) [ 'error' ] [ 'message' ] ) . to match /An error/
133
+ end
134
+ subject
135
+ end
136
+
137
+ it "does not include the backtrace in the error response" do
138
+ expect ( response ) . to receive ( :body= ) do | body |
139
+ expect ( body ) . to_not include ( "backtrace" )
140
+ end
141
+ subject
78
142
end
79
- subject
80
143
end
81
144
end
82
145
0 commit comments