Skip to content

Commit a5a52e3

Browse files
authored
Add details to errors (#29)
* add test * Add details * fix code style * update version and bundle, and fix vulnerability * more platforms * fix dependencies with ruby 2.6.10 * just return all details * handle all errors * update version
1 parent ad55c97 commit a5a52e3

File tree

5 files changed

+114
-142
lines changed

5 files changed

+114
-142
lines changed

Gemfile.lock

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,76 @@
11
PATH
22
remote: .
33
specs:
4-
veryfi (1.0.4)
4+
veryfi (2.0.0)
55
base64 (~> 0.1)
66
faraday (>= 1.7, < 3.0)
77
openssl (>= 2.2, < 3.1)
88

99
GEM
1010
remote: https://rubygems.org/
1111
specs:
12-
addressable (2.8.1)
12+
activesupport (6.1.7.8)
13+
concurrent-ruby (~> 1.0, >= 1.0.2)
14+
i18n (>= 1.6, < 2)
15+
minitest (>= 5.1)
16+
tzinfo (~> 2.0)
17+
zeitwerk (~> 2.3)
18+
addressable (2.8.6)
1319
public_suffix (>= 2.0.2, < 6.0)
1420
ast (2.4.2)
1521
base64 (0.2.0)
22+
bigdecimal (3.1.8)
1623
bundler-audit (0.9.1)
1724
bundler (>= 1.2.0, < 3)
1825
thor (~> 1.0)
1926
coderay (1.1.3)
20-
crack (0.4.5)
27+
concurrent-ruby (1.3.3)
28+
crack (1.0.0)
29+
bigdecimal
2130
rexml
22-
diff-lcs (1.5.0)
31+
diff-lcs (1.5.1)
2332
docile (1.4.0)
24-
faraday (2.7.12)
33+
faraday (2.8.1)
2534
base64
2635
faraday-net_http (>= 2.0, < 3.1)
2736
ruby2_keywords (>= 0.0.4)
2837
faraday-net_http (3.0.2)
29-
hashdiff (1.0.1)
30-
method_source (1.0.0)
38+
hashdiff (1.1.0)
39+
i18n (1.14.5)
40+
concurrent-ruby (~> 1.0)
41+
method_source (1.1.0)
42+
minitest (5.23.1)
3143
openssl (3.0.2)
32-
parallel (1.22.1)
33-
parser (3.2.0.0)
44+
parallel (1.24.0)
45+
parser (3.3.2.0)
3446
ast (~> 2.4.1)
35-
pry (0.14.1)
47+
racc
48+
pry (0.14.2)
3649
coderay (~> 1.1)
3750
method_source (~> 1.0)
38-
public_suffix (5.0.1)
51+
public_suffix (5.0.5)
52+
racc (1.8.0)
3953
rainbow (3.1.1)
40-
rake (13.0.6)
41-
regexp_parser (2.6.1)
42-
rexml (3.2.5)
43-
rspec (3.12.0)
44-
rspec-core (~> 3.12.0)
45-
rspec-expectations (~> 3.12.0)
46-
rspec-mocks (~> 3.12.0)
47-
rspec-core (3.12.0)
48-
rspec-support (~> 3.12.0)
49-
rspec-expectations (3.12.1)
54+
rake (13.2.1)
55+
regexp_parser (2.9.2)
56+
rexml (3.2.9)
57+
strscan
58+
rspec (3.13.0)
59+
rspec-core (~> 3.13.0)
60+
rspec-expectations (~> 3.13.0)
61+
rspec-mocks (~> 3.13.0)
62+
rspec-core (3.13.0)
63+
rspec-support (~> 3.13.0)
64+
rspec-expectations (3.13.0)
5065
diff-lcs (>= 1.2.0, < 2.0)
51-
rspec-support (~> 3.12.0)
66+
rspec-support (~> 3.13.0)
5267
rspec-its (1.3.0)
5368
rspec-core (>= 3.0.0)
5469
rspec-expectations (>= 3.0.0)
55-
rspec-mocks (3.12.1)
70+
rspec-mocks (3.13.1)
5671
diff-lcs (>= 1.2.0, < 2.0)
57-
rspec-support (~> 3.12.0)
58-
rspec-support (3.12.0)
72+
rspec-support (~> 3.13.0)
73+
rspec-support (3.13.1)
5974
rubocop (0.93.1)
6075
parallel (~> 1.10)
6176
parser (>= 2.7.1.5)
@@ -65,12 +80,12 @@ GEM
6580
rubocop-ast (>= 0.6.0)
6681
ruby-progressbar (~> 1.7)
6782
unicode-display_width (>= 1.4.0, < 2.0)
68-
rubocop-ast (1.24.1)
69-
parser (>= 3.1.1.0)
83+
rubocop-ast (1.30.0)
84+
parser (>= 3.2.1.0)
7085
rubocop-rspec (1.44.1)
7186
rubocop (~> 0.87)
7287
rubocop-ast (>= 0.7.1)
73-
ruby-progressbar (1.11.0)
88+
ruby-progressbar (1.13.0)
7489
ruby2_keywords (0.0.5)
7590
simplecov (0.22.0)
7691
docile (~> 1.1)
@@ -79,24 +94,31 @@ GEM
7994
simplecov-badge (2.0.2)
8095
simplecov-html (0.12.3)
8196
simplecov_json_formatter (0.1.4)
82-
thor (1.2.1)
97+
strscan (3.1.0)
98+
thor (1.3.1)
99+
tzinfo (2.0.6)
100+
concurrent-ruby (~> 1.0)
83101
unicode-display_width (1.8.0)
84102
vcr (6.1.0)
85-
webmock (3.18.1)
103+
webmock (3.23.1)
86104
addressable (>= 2.8.0)
87105
crack (>= 0.3.2)
88106
hashdiff (>= 0.4.0, < 2.0.0)
107+
zeitwerk (2.6.15)
89108

90109
PLATFORMS
110+
arm64-darwin-23
91111
ruby
92112
x86_64-darwin-19
93113
x86_64-linux
94114

95115
DEPENDENCIES
116+
activesupport (~> 6.0)
96117
bundler (~> 2.2)
97118
bundler-audit (~> 0.9)
98119
pry (~> 0.14)
99120
rake (~> 13.0)
121+
rexml (~> 3.2.7)
100122
rspec (~> 3.9)
101123
rspec-its (~> 1.3)
102124
rubocop (~> 0.82)
@@ -108,4 +130,4 @@ DEPENDENCIES
108130
webmock (~> 3.14)
109131

110132
BUNDLED WITH
111-
2.2.33
133+
2.2.26

lib/veryfi/error.rb

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,32 @@
11
# frozen_string_literal: true
22

3+
require "json"
4+
35
module Veryfi
46
class Error
57
def self.from_response(status, response)
68
if response.empty?
79
VeryfiError.new(format("%<code>d", code: status))
810
else
9-
get_error(status, response)
10-
end
11-
end
12-
13-
def self.get_error(status, response)
14-
case status
15-
when 400 then BadRequest
16-
when 401 then UnauthorizedAccessToken
17-
when 404 then ResourceNotFound
18-
when 405 then UnexpectedHTTPMethod
19-
when 409 then AccessLimitReached
20-
when 500 then InternalError
21-
else VeryfiError.new(format("%<code>d, %<message>s", code: status, message: response["error"]))
22-
end
23-
end
24-
class BadRequest < VeryfiError
25-
def to_s
26-
"400, Bad Request"
27-
end
28-
end
29-
30-
class UnauthorizedAccessToken < VeryfiError
31-
def to_s
32-
"401, Unauthorized Access Token"
33-
end
34-
end
35-
36-
class ResourceNotFound < VeryfiError
37-
def to_s
38-
"404, Resource not found"
11+
VeryfiError.new(format("%<code>d, %<message>s", code: status, message: response["error"]), response)
3912
end
4013
end
4114

42-
class UnexpectedHTTPMethod < VeryfiError
43-
def to_s
44-
"405, Unexpected HTTP Method"
45-
end
46-
end
15+
class VeryfiError < StandardError
16+
attr_reader :message
4717

48-
class AccessLimitReached < VeryfiError
49-
def to_s
50-
"409, Access Limit Reached"
18+
def initialize(message = "An error occurred", response = {})
19+
@message = if response.empty?
20+
message
21+
else
22+
JSON.pretty_generate(response)
23+
end
24+
super(message)
5125
end
52-
end
5326

54-
class InternalError < VeryfiError
5527
def to_s
56-
"500, Internal Server Error"
28+
message
5729
end
5830
end
59-
60-
class VeryfiError < StandardError
61-
end
6231
end
6332
end

lib/veryfi/version.rb

Lines changed: 1 addition & 1 deletion
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.4"
4+
VERSION = "2.0.0"
55
end

spec/veryfi/request_spec.rb

Lines changed: 45 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "spec_helper"
4+
require "active_support/core_ext/string/strip"
45

56
RSpec.describe Veryfi::Request do
67
include_context :with_veryfi_client
@@ -32,76 +33,67 @@
3233
stub_request(:get, /\.*/).to_return(status: 400, body: '{"code": 400, "error": "Bad Request"}')
3334
end
3435

35-
it "raises error" do
36-
expect { client.document.all }.to raise_error(
37-
Veryfi::Error::BadRequest,
38-
"400, Bad Request"
39-
)
40-
end
41-
end
42-
43-
context "when server responds with 401 error" do
44-
before do
45-
stub_request(:get, /\.*/).to_return(status: 401, body: '{"error": "Unauthorized Access Token"}')
36+
let(:expected_error) do
37+
<<-TEXT.strip_heredoc.chomp
38+
{
39+
"code": 400,
40+
"error": "Bad Request"
41+
}
42+
TEXT
4643
end
4744

4845
it "raises error" do
4946
expect { client.document.all }.to raise_error(
50-
Veryfi::Error::UnauthorizedAccessToken,
51-
"401, Unauthorized Access Token"
47+
Veryfi::Error::VeryfiError,
48+
expected_error
5249
)
5350
end
5451
end
5552

56-
context "when server responds with 404 error" do
53+
context "when server responds with 400 error and details" do
5754
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"
55+
stub_request(:get, /\.*/).to_return(
56+
status: 400,
57+
body: <<-JSON
58+
{
59+
"code": 400,
60+
"error": "Bad Request",
61+
"details": [
62+
{
63+
"type": "value_error",
64+
"loc": [],
65+
"msg": "Value error, Only one of ..."
66+
}
67+
]
68+
}
69+
JSON
6570
)
6671
end
67-
end
6872

69-
context "when server responds with 405 error" do
70-
before do
71-
stub_request(:get, /\.*/).to_return(status: 405, body: '{"error": "Unexpected HTTP Method"}')
72-
end
73+
let(:expected_error) do
74+
<<-TEXT.strip_heredoc.chomp
75+
{
76+
"code": 400,
77+
"error": "Bad Request",
78+
"details": [
79+
{
80+
"type": "value_error",
81+
"loc": [
7382
74-
it "raises error" do
75-
expect { client.document.all }.to raise_error(
76-
Veryfi::Error::UnexpectedHTTPMethod,
77-
"405, Unexpected HTTP Method"
78-
)
79-
end
80-
end
81-
82-
context "when server responds with 409 error" do
83-
before do
84-
stub_request(:get, /\.*/).to_return(status: 409, body: '{"error": "Access Limit Reached"}')
83+
],
84+
"msg": "Value error, Only one of ..."
85+
}
86+
]
87+
}
88+
TEXT
8589
end
8690

8791
it "raises error" do
8892
expect { client.document.all }.to raise_error(
89-
Veryfi::Error::AccessLimitReached,
90-
"409, Access Limit Reached"
91-
)
92-
end
93-
end
94-
95-
context "when server responds with 500 error" do
96-
before do
97-
stub_request(:get, /\.*/).to_return(status: 500, body: '{"error": "Internal Server Error"}')
98-
end
99-
100-
it "raises error" do
101-
expect { client.document.all }.to raise_error(
102-
Veryfi::Error::InternalError,
103-
"500, Internal Server Error"
104-
)
93+
Veryfi::Error::VeryfiError
94+
) { |error|
95+
expect(error.to_s).to eq(expected_error)
96+
}
10597
end
10698
end
10799

@@ -117,17 +109,4 @@
117109
)
118110
end
119111
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"
130-
)
131-
end
132-
end
133112
end

veryfi.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,6 @@ Gem::Specification.new do |spec|
8282
spec.add_development_dependency "simplecov-badge", "~> 2.0"
8383
spec.add_development_dependency "vcr", "~> 6.0"
8484
spec.add_development_dependency "webmock", "~> 3.14"
85+
spec.add_development_dependency "rexml", "~> 3.2.7"
86+
spec.add_development_dependency "activesupport", "~> 6.0"
8587
end

0 commit comments

Comments
 (0)