Skip to content

Commit 4268f6f

Browse files
Merge pull request #25 from nbelzer/nbelzer/add-message-to-requesterror
Add message to SolidusShipStation::Api::RequestError
2 parents 80ba539 + a1cee70 commit 4268f6f

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Changelog
22

3+
## v2.0.0
4+
5+
**Breaking changes**
6+
7+
- The default message for a `RequestError` has been changed from the raw response body to a parsed version. If your code depends on the response body, you need to access it through the `.response_body` attribute on the exception instead.
8+
```ruby
9+
# before
10+
error.message
11+
error.to_s
12+
13+
# after
14+
error.response_body
15+
```
16+
317
## [v1.1.0](https://github.com/solidusio-contrib/solidus_shipstation/tree/v1.1.0) (2021-10-26)
418

519
[Full Changelog](https://github.com/solidusio-contrib/solidus_shipstation/compare/v1.0.0...v1.1.0)

lib/solidus_shipstation/api/request_error.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,18 @@ def initialize(response_code:, response_body:, response_headers:)
2626
@response_body = response_body
2727
@response_headers = response_headers
2828

29-
super(response_body)
29+
super(message_from_body)
30+
end
31+
32+
private
33+
34+
def message_from_body
35+
return unless @response_body
36+
37+
parsed_body = JSON.parse(@response_body)
38+
exception_type = parsed_body.fetch('ExceptionType', 'Unknown Exception Type')
39+
exception_message = parsed_body.fetch('ExceptionMessage', '')
40+
"#{exception_type}: #{exception_message}"
3041
end
3142
end
3243
end

spec/lib/solidus_shipstation/api/request_error_spec.rb

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,57 @@
55
'HTTParty::Response',
66
code: 500,
77
headers: { 'Key' => 'Value' },
8-
body: '{ "message": "Internal Server Error" }',
8+
body: '{ "Message": "An error has occured.", "ExceptionMessage": "orders are required", "ExceptionType": "System.ArgumentException" }',
99
)
1010

1111
error = described_class.from_response(response)
1212

1313
expect(error).to have_attributes(
1414
response_code: 500,
1515
response_headers: { 'Key' => 'Value' },
16-
response_body: '{ "message": "Internal Server Error" }',
16+
response_body: '{ "Message": "An error has occured.", "ExceptionMessage": "orders are required", "ExceptionType": "System.ArgumentException" }',
1717
)
1818
end
1919
end
20+
21+
describe '.message' do
22+
it 'returns the name of the exception if there is no message body' do
23+
response = instance_double(
24+
'HTTParty::Response',
25+
code: 500,
26+
headers: { 'Key' => 'Value' },
27+
body: nil
28+
)
29+
30+
error = described_class.from_response(response)
31+
32+
expect(error.message).to eq("SolidusShipstation::Api::RequestError")
33+
end
34+
35+
it 'extracts both the ExceptionMessage and ExceptionType' do
36+
response = instance_double(
37+
'HTTParty::Response',
38+
code: 500,
39+
headers: { 'Key' => 'Value' },
40+
body: '{ "Message": "An error has occured.", "ExceptionMessage": "orders are required", "ExceptionType": "System.ArgumentException" }',
41+
)
42+
43+
error = described_class.from_response(response)
44+
45+
expect(error.message).to eq("System.ArgumentException: orders are required")
46+
end
47+
48+
it 'doesn\'t fail when the ExceptionMessage and ExceptionType are missing' do
49+
response = instance_double(
50+
'HTTParty::Response',
51+
code: 500,
52+
headers: { 'Key' => 'Value' },
53+
body: '{ "Message": "An error has occured." }',
54+
)
55+
56+
error = described_class.from_response(response)
57+
58+
expect(error.message).to eq("Unknown Exception Type: ")
59+
end
60+
end
2061
end

0 commit comments

Comments
 (0)