Skip to content

Commit

Permalink
Add details to errors (#29)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Kaevan89 committed Jun 11, 2024
1 parent ad55c97 commit a5a52e3
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 142 deletions.
84 changes: 53 additions & 31 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,61 +1,76 @@
PATH
remote: .
specs:
veryfi (1.0.4)
veryfi (2.0.0)
base64 (~> 0.1)
faraday (>= 1.7, < 3.0)
openssl (>= 2.2, < 3.1)

GEM
remote: https://rubygems.org/
specs:
addressable (2.8.1)
activesupport (6.1.7.8)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.8)
bundler-audit (0.9.1)
bundler (>= 1.2.0, < 3)
thor (~> 1.0)
coderay (1.1.3)
crack (0.4.5)
concurrent-ruby (1.3.3)
crack (1.0.0)
bigdecimal
rexml
diff-lcs (1.5.0)
diff-lcs (1.5.1)
docile (1.4.0)
faraday (2.7.12)
faraday (2.8.1)
base64
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
hashdiff (1.0.1)
method_source (1.0.0)
hashdiff (1.1.0)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
method_source (1.1.0)
minitest (5.23.1)
openssl (3.0.2)
parallel (1.22.1)
parser (3.2.0.0)
parallel (1.24.0)
parser (3.3.2.0)
ast (~> 2.4.1)
pry (0.14.1)
racc
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (5.0.1)
public_suffix (5.0.5)
racc (1.8.0)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.6.1)
rexml (3.2.5)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.0)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.1)
rake (13.2.1)
regexp_parser (2.9.2)
rexml (3.2.9)
strscan
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (~> 3.13.0)
rspec-its (1.3.0)
rspec-core (>= 3.0.0)
rspec-expectations (>= 3.0.0)
rspec-mocks (3.12.1)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (0.93.1)
parallel (~> 1.10)
parser (>= 2.7.1.5)
Expand All @@ -65,12 +80,12 @@ GEM
rubocop-ast (>= 0.6.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (1.24.1)
parser (>= 3.1.1.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
rubocop-rspec (1.44.1)
rubocop (~> 0.87)
rubocop-ast (>= 0.7.1)
ruby-progressbar (1.11.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
simplecov (0.22.0)
docile (~> 1.1)
Expand All @@ -79,24 +94,31 @@ GEM
simplecov-badge (2.0.2)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
thor (1.2.1)
strscan (3.1.0)
thor (1.3.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (1.8.0)
vcr (6.1.0)
webmock (3.18.1)
webmock (3.23.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
zeitwerk (2.6.15)

PLATFORMS
arm64-darwin-23
ruby
x86_64-darwin-19
x86_64-linux

DEPENDENCIES
activesupport (~> 6.0)
bundler (~> 2.2)
bundler-audit (~> 0.9)
pry (~> 0.14)
rake (~> 13.0)
rexml (~> 3.2.7)
rspec (~> 3.9)
rspec-its (~> 1.3)
rubocop (~> 0.82)
Expand All @@ -108,4 +130,4 @@ DEPENDENCIES
webmock (~> 3.14)

BUNDLED WITH
2.2.33
2.2.26
57 changes: 13 additions & 44 deletions lib/veryfi/error.rb
Original file line number Diff line number Diff line change
@@ -1,63 +1,32 @@
# frozen_string_literal: true

require "json"

module Veryfi
class Error
def self.from_response(status, response)
if response.empty?
VeryfiError.new(format("%<code>d", code: status))
else
get_error(status, response)
end
end

def self.get_error(status, response)
case status
when 400 then BadRequest
when 401 then UnauthorizedAccessToken
when 404 then ResourceNotFound
when 405 then UnexpectedHTTPMethod
when 409 then AccessLimitReached
when 500 then InternalError
else VeryfiError.new(format("%<code>d, %<message>s", code: status, message: response["error"]))
end
end
class BadRequest < VeryfiError
def to_s
"400, Bad Request"
end
end

class UnauthorizedAccessToken < VeryfiError
def to_s
"401, Unauthorized Access Token"
end
end

class ResourceNotFound < VeryfiError
def to_s
"404, Resource not found"
VeryfiError.new(format("%<code>d, %<message>s", code: status, message: response["error"]), response)
end
end

class UnexpectedHTTPMethod < VeryfiError
def to_s
"405, Unexpected HTTP Method"
end
end
class VeryfiError < StandardError
attr_reader :message

class AccessLimitReached < VeryfiError
def to_s
"409, Access Limit Reached"
def initialize(message = "An error occurred", response = {})
@message = if response.empty?
message
else
JSON.pretty_generate(response)
end
super(message)
end
end

class InternalError < VeryfiError
def to_s
"500, Internal Server Error"
message
end
end

class VeryfiError < StandardError
end
end
end
2 changes: 1 addition & 1 deletion lib/veryfi/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Veryfi
VERSION = "1.0.4"
VERSION = "2.0.0"
end
111 changes: 45 additions & 66 deletions spec/veryfi/request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "spec_helper"
require "active_support/core_ext/string/strip"

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

it "raises error" do
expect { client.document.all }.to raise_error(
Veryfi::Error::BadRequest,
"400, Bad Request"
)
end
end

context "when server responds with 401 error" do
before do
stub_request(:get, /\.*/).to_return(status: 401, body: '{"error": "Unauthorized Access Token"}')
let(:expected_error) do
<<-TEXT.strip_heredoc.chomp
{
"code": 400,
"error": "Bad Request"
}
TEXT
end

it "raises error" do
expect { client.document.all }.to raise_error(
Veryfi::Error::UnauthorizedAccessToken,
"401, Unauthorized Access Token"
Veryfi::Error::VeryfiError,
expected_error
)
end
end

context "when server responds with 404 error" do
context "when server responds with 400 error and details" do
before do
stub_request(:get, /\.*/).to_return(status: 404, body: '{"error": "Resource Not Found"}')
end

it "raises error" do
expect { client.document.all }.to raise_error(
Veryfi::Error::ResourceNotFound,
"404, Resource not found"
stub_request(:get, /\.*/).to_return(
status: 400,
body: <<-JSON
{
"code": 400,
"error": "Bad Request",
"details": [
{
"type": "value_error",
"loc": [],
"msg": "Value error, Only one of ..."
}
]
}
JSON
)
end
end

context "when server responds with 405 error" do
before do
stub_request(:get, /\.*/).to_return(status: 405, body: '{"error": "Unexpected HTTP Method"}')
end
let(:expected_error) do
<<-TEXT.strip_heredoc.chomp
{
"code": 400,
"error": "Bad Request",
"details": [
{
"type": "value_error",
"loc": [
it "raises error" do
expect { client.document.all }.to raise_error(
Veryfi::Error::UnexpectedHTTPMethod,
"405, Unexpected HTTP Method"
)
end
end

context "when server responds with 409 error" do
before do
stub_request(:get, /\.*/).to_return(status: 409, body: '{"error": "Access Limit Reached"}')
],
"msg": "Value error, Only one of ..."
}
]
}
TEXT
end

it "raises error" do
expect { client.document.all }.to raise_error(
Veryfi::Error::AccessLimitReached,
"409, Access Limit Reached"
)
end
end

context "when server responds with 500 error" do
before do
stub_request(:get, /\.*/).to_return(status: 500, body: '{"error": "Internal Server Error"}')
end

it "raises error" do
expect { client.document.all }.to raise_error(
Veryfi::Error::InternalError,
"500, Internal Server Error"
)
Veryfi::Error::VeryfiError
) { |error|
expect(error.to_s).to eq(expected_error)
}
end
end

Expand All @@ -117,17 +109,4 @@
)
end
end

context "when server responds with unknown error and body" do
before do
stub_request(:get, /\.*/).to_return(status: 504, body: '{"error": "Gateway Timeout"}')
end

it "raises error" do
expect { client.document.all }.to raise_error(
Veryfi::Error::VeryfiError,
"504, Gateway Timeout"
)
end
end
end
2 changes: 2 additions & 0 deletions veryfi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "simplecov-badge", "~> 2.0"
spec.add_development_dependency "vcr", "~> 6.0"
spec.add_development_dependency "webmock", "~> 3.14"
spec.add_development_dependency "rexml", "~> 3.2.7"
spec.add_development_dependency "activesupport", "~> 6.0"
end

0 comments on commit a5a52e3

Please sign in to comment.