Skip to content

Commit af48726

Browse files
authored
Use backend message instead of custom error (#14)
* Use backend-message instead of custom class. * Update error tests. * Update new coverage badge. * Update version and bundle * Use double quote instead of single quote. * Remove not needed whitespaces. * Handle empty response. * If empty body, return raise error without message. * Add errors for requests.
1 parent cee1b7e commit af48726

File tree

6 files changed

+114
-58
lines changed

6 files changed

+114
-58
lines changed

Diff for: Gemfile.lock

+32-34
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
veryfi (1.0.0)
4+
veryfi (1.0.1)
55
base64 (~> 0.1)
66
faraday (~> 1.7)
77
openssl (>= 2.2, < 3.1)
@@ -13,15 +13,15 @@ GEM
1313
public_suffix (>= 2.0.2, < 5.0)
1414
ast (2.4.2)
1515
base64 (0.1.1)
16-
bundler-audit (0.9.0)
16+
bundler-audit (0.9.1)
1717
bundler (>= 1.2.0, < 3)
1818
thor (~> 1.0)
1919
coderay (1.1.3)
2020
crack (0.4.5)
2121
rexml
22-
diff-lcs (1.4.4)
22+
diff-lcs (1.5.0)
2323
docile (1.4.0)
24-
faraday (1.10.0)
24+
faraday (1.10.1)
2525
faraday-em_http (~> 1.0)
2626
faraday-em_synchrony (~> 1.0)
2727
faraday-excon (~> 1.1)
@@ -37,46 +37,44 @@ GEM
3737
faraday-em_synchrony (1.0.0)
3838
faraday-excon (1.1.0)
3939
faraday-httpclient (1.0.1)
40-
faraday-multipart (1.0.3)
41-
multipart-post (>= 1.2, < 3)
40+
faraday-multipart (1.0.4)
41+
multipart-post (~> 2)
4242
faraday-net_http (1.0.1)
4343
faraday-net_http_persistent (1.2.0)
4444
faraday-patron (1.0.0)
4545
faraday-rack (1.0.0)
4646
faraday-retry (1.0.3)
4747
hashdiff (1.0.1)
48-
ipaddr (1.2.4)
4948
method_source (1.0.0)
50-
multipart-post (2.1.1)
51-
openssl (2.2.1)
52-
ipaddr
53-
parallel (1.20.1)
54-
parser (3.0.2.0)
49+
multipart-post (2.2.3)
50+
openssl (3.0.0)
51+
parallel (1.22.1)
52+
parser (3.1.2.1)
5553
ast (~> 2.4.1)
5654
pry (0.14.1)
5755
coderay (~> 1.1)
5856
method_source (~> 1.0)
59-
public_suffix (4.0.6)
60-
rainbow (3.0.0)
57+
public_suffix (4.0.7)
58+
rainbow (3.1.1)
6159
rake (13.0.6)
62-
regexp_parser (2.1.1)
60+
regexp_parser (2.5.0)
6361
rexml (3.2.5)
64-
rspec (3.10.0)
65-
rspec-core (~> 3.10.0)
66-
rspec-expectations (~> 3.10.0)
67-
rspec-mocks (~> 3.10.0)
68-
rspec-core (3.10.1)
69-
rspec-support (~> 3.10.0)
70-
rspec-expectations (3.10.1)
62+
rspec (3.11.0)
63+
rspec-core (~> 3.11.0)
64+
rspec-expectations (~> 3.11.0)
65+
rspec-mocks (~> 3.11.0)
66+
rspec-core (3.11.0)
67+
rspec-support (~> 3.11.0)
68+
rspec-expectations (3.11.0)
7169
diff-lcs (>= 1.2.0, < 2.0)
72-
rspec-support (~> 3.10.0)
70+
rspec-support (~> 3.11.0)
7371
rspec-its (1.3.0)
7472
rspec-core (>= 3.0.0)
7573
rspec-expectations (>= 3.0.0)
76-
rspec-mocks (3.10.2)
74+
rspec-mocks (3.11.1)
7775
diff-lcs (>= 1.2.0, < 2.0)
78-
rspec-support (~> 3.10.0)
79-
rspec-support (3.10.2)
76+
rspec-support (~> 3.11.0)
77+
rspec-support (3.11.0)
8078
rubocop (0.93.1)
8179
parallel (~> 1.10)
8280
parser (>= 2.7.1.5)
@@ -86,8 +84,8 @@ GEM
8684
rubocop-ast (>= 0.6.0)
8785
ruby-progressbar (~> 1.7)
8886
unicode-display_width (>= 1.4.0, < 2.0)
89-
rubocop-ast (1.11.0)
90-
parser (>= 3.0.1.1)
87+
rubocop-ast (1.21.0)
88+
parser (>= 3.1.1.0)
9189
rubocop-rspec (1.44.1)
9290
rubocop (~> 0.87)
9391
rubocop-ast (>= 0.7.1)
@@ -99,11 +97,11 @@ GEM
9997
simplecov_json_formatter (~> 0.1)
10098
simplecov-badge (2.0.2)
10199
simplecov-html (0.12.3)
102-
simplecov_json_formatter (0.1.3)
103-
thor (1.1.0)
104-
unicode-display_width (1.7.0)
105-
vcr (6.0.0)
106-
webmock (3.14.0)
100+
simplecov_json_formatter (0.1.4)
101+
thor (1.2.1)
102+
unicode-display_width (1.8.0)
103+
vcr (6.1.0)
104+
webmock (3.17.1)
107105
addressable (>= 2.8.0)
108106
crack (>= 0.3.2)
109107
hashdiff (>= 0.4.0, < 2.0.0)
@@ -129,4 +127,4 @@ DEPENDENCIES
129127
webmock (~> 3.14)
130128

131129
BUNDLED WITH
132-
2.2.26
130+
2.2.33

Diff for: coverage/coverage-badge.png

-195 Bytes
Loading

Diff for: lib/veryfi/error.rb

+27-9
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,62 @@
22

33
module Veryfi
44
class Error
5-
def self.from_response(response)
6-
case response.status
5+
def self.from_response(status, response)
6+
if response.empty?
7+
VeryfiError.new(format("%<code>d", code: status))
8+
else
9+
get_error(status, response)
10+
end
11+
end
12+
13+
def self.get_error(status, response)
14+
case status
715
when 400 then BadRequest
816
when 401 then UnauthorizedAccessToken
17+
when 404 then ResourceNotFound
918
when 405 then UnexpectedHTTPMethod
1019
when 409 then AccessLimitReached
11-
else InternalError
20+
when 500 then InternalError
21+
else VeryfiError.new(format("%<code>d, %<message>s", code: status, message: response["error"]))
1222
end
1323
end
14-
1524
class BadRequest < StandardError
1625
def to_s
17-
"Bad Request"
26+
"400, Bad Request"
1827
end
1928
end
2029

2130
class UnauthorizedAccessToken < StandardError
2231
def to_s
23-
"Unauthorized Access Token"
32+
"401, Unauthorized Access Token"
33+
end
34+
end
35+
36+
class ResourceNotFound < StandardError
37+
def to_s
38+
"404, Resource not found"
2439
end
2540
end
2641

2742
class UnexpectedHTTPMethod < StandardError
2843
def to_s
29-
"Unexpected HTTP Method"
44+
"405, Unexpected HTTP Method"
3045
end
3146
end
3247

3348
class AccessLimitReached < StandardError
3449
def to_s
35-
"Access Limit Reached"
50+
"409, Access Limit Reached"
3651
end
3752
end
3853

3954
class InternalError < StandardError
4055
def to_s
41-
"Internal Server Error"
56+
"500, Internal Server Error"
4257
end
4358
end
59+
60+
class VeryfiError < StandardError
61+
end
4462
end
4563
end

Diff for: lib/veryfi/request.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ def make_request(http_verb, path, params = {})
5656
headers = generate_headers(params)
5757

5858
response = conn.public_send(http_verb, url, body, headers)
59+
json_response = process_response(response)
5960

6061
if response.success?
61-
process_response(http_verb, response)
62+
json_response
6263
else
63-
raise Veryfi::Error.from_response(response)
64+
raise Veryfi::Error.from_response(response.status, json_response)
6465
end
6566
end
6667

@@ -102,8 +103,8 @@ def generate_signature(params, timestamp)
102103
Veryfi::Signature.new(client_secret, params, timestamp).to_base64
103104
end
104105

105-
def process_response(_http_verb, response)
106-
# return response if http_verb == :delete
106+
def process_response(response)
107+
return {} if response.body.empty?
107108

108109
JSON.parse(response.body)
109110
end

Diff for: lib/veryfi/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Veryfi
4-
VERSION = "1.0.0"
4+
VERSION = "1.0.1"
55
end

Diff for: spec/veryfi/request_spec.rb

+49-10
Original file line numberDiff line numberDiff line change
@@ -29,65 +29,104 @@
2929

3030
context "when server responds with 400 error" do
3131
before do
32-
stub_request(:get, /\.*/).to_return(status: 400)
32+
stub_request(:get, /\.*/).to_return(status: 400, body: '{"code": 400, "error": "Bad Request"}')
3333
end
3434

3535
it "raises error" do
3636
expect { client.document.all }.to raise_error(
3737
Veryfi::Error::BadRequest,
38-
"Bad Request"
38+
"400, Bad Request"
3939
)
4040
end
4141
end
4242

4343
context "when server responds with 401 error" do
4444
before do
45-
stub_request(:get, /\.*/).to_return(status: 401)
45+
stub_request(:get, /\.*/).to_return(status: 401, body: '{"error": "Unauthorized Access Token"}')
4646
end
4747

4848
it "raises error" do
4949
expect { client.document.all }.to raise_error(
5050
Veryfi::Error::UnauthorizedAccessToken,
51-
"Unauthorized Access Token"
51+
"401, Unauthorized Access Token"
52+
)
53+
end
54+
end
55+
56+
context "when server responds with 404 error" do
57+
before do
58+
stub_request(:get, /\.*/).to_return(status: 404, body: '{"error": "Resource Not Found"}')
59+
end
60+
61+
it "raises error" do
62+
expect { client.document.all }.to raise_error(
63+
Veryfi::Error::ResourceNotFound,
64+
"404, Resource not found"
5265
)
5366
end
5467
end
5568

5669
context "when server responds with 405 error" do
5770
before do
58-
stub_request(:get, /\.*/).to_return(status: 405)
71+
stub_request(:get, /\.*/).to_return(status: 405, body: '{"error": "Unexpected HTTP Method"}')
5972
end
6073

6174
it "raises error" do
6275
expect { client.document.all }.to raise_error(
6376
Veryfi::Error::UnexpectedHTTPMethod,
64-
"Unexpected HTTP Method"
77+
"405, Unexpected HTTP Method"
6578
)
6679
end
6780
end
6881

6982
context "when server responds with 409 error" do
7083
before do
71-
stub_request(:get, /\.*/).to_return(status: 409)
84+
stub_request(:get, /\.*/).to_return(status: 409, body: '{"error": "Access Limit Reached"}')
7285
end
7386

7487
it "raises error" do
7588
expect { client.document.all }.to raise_error(
7689
Veryfi::Error::AccessLimitReached,
77-
"Access Limit Reached"
90+
"409, Access Limit Reached"
7891
)
7992
end
8093
end
8194

8295
context "when server responds with 500 error" do
8396
before do
84-
stub_request(:get, /\.*/).to_return(status: 500)
97+
stub_request(:get, /\.*/).to_return(status: 500, body: '{"error": "Internal Server Error"}')
8598
end
8699

87100
it "raises error" do
88101
expect { client.document.all }.to raise_error(
89102
Veryfi::Error::InternalError,
90-
"Internal Server Error"
103+
"500, Internal Server Error"
104+
)
105+
end
106+
end
107+
108+
context "when server responds with empty body" do
109+
before do
110+
stub_request(:get, /\.*/).to_return(status: 501, body: "")
111+
end
112+
113+
it "raises error" do
114+
expect { client.document.all }.to raise_error(
115+
Veryfi::Error::VeryfiError,
116+
"501"
117+
)
118+
end
119+
end
120+
121+
context "when server responds with unknown error and body" do
122+
before do
123+
stub_request(:get, /\.*/).to_return(status: 504, body: '{"error": "Gateway Timeout"}')
124+
end
125+
126+
it "raises error" do
127+
expect { client.document.all }.to raise_error(
128+
Veryfi::Error::VeryfiError,
129+
"504, Gateway Timeout"
91130
)
92131
end
93132
end

0 commit comments

Comments
 (0)