Skip to content

Commit

Permalink
Use backend message instead of custom error (#14)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Kaevan89 authored Aug 18, 2022
1 parent cee1b7e commit af48726
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 58 deletions.
66 changes: 32 additions & 34 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
veryfi (1.0.0)
veryfi (1.0.1)
base64 (~> 0.1)
faraday (~> 1.7)
openssl (>= 2.2, < 3.1)
Expand All @@ -13,15 +13,15 @@ GEM
public_suffix (>= 2.0.2, < 5.0)
ast (2.4.2)
base64 (0.1.1)
bundler-audit (0.9.0)
bundler-audit (0.9.1)
bundler (>= 1.2.0, < 3)
thor (~> 1.0)
coderay (1.1.3)
crack (0.4.5)
rexml
diff-lcs (1.4.4)
diff-lcs (1.5.0)
docile (1.4.0)
faraday (1.10.0)
faraday (1.10.1)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
Expand All @@ -37,46 +37,44 @@ GEM
faraday-em_synchrony (1.0.0)
faraday-excon (1.1.0)
faraday-httpclient (1.0.1)
faraday-multipart (1.0.3)
multipart-post (>= 1.2, < 3)
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (1.0.1)
faraday-net_http_persistent (1.2.0)
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
hashdiff (1.0.1)
ipaddr (1.2.4)
method_source (1.0.0)
multipart-post (2.1.1)
openssl (2.2.1)
ipaddr
parallel (1.20.1)
parser (3.0.2.0)
multipart-post (2.2.3)
openssl (3.0.0)
parallel (1.22.1)
parser (3.1.2.1)
ast (~> 2.4.1)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (4.0.6)
rainbow (3.0.0)
public_suffix (4.0.7)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.1.1)
regexp_parser (2.5.0)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (~> 3.11.0)
rspec-its (1.3.0)
rspec-core (>= 3.0.0)
rspec-expectations (>= 3.0.0)
rspec-mocks (3.10.2)
rspec-mocks (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
rspec-support (~> 3.11.0)
rspec-support (3.11.0)
rubocop (0.93.1)
parallel (~> 1.10)
parser (>= 2.7.1.5)
Expand All @@ -86,8 +84,8 @@ GEM
rubocop-ast (>= 0.6.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (1.11.0)
parser (>= 3.0.1.1)
rubocop-ast (1.21.0)
parser (>= 3.1.1.0)
rubocop-rspec (1.44.1)
rubocop (~> 0.87)
rubocop-ast (>= 0.7.1)
Expand All @@ -99,11 +97,11 @@ GEM
simplecov_json_formatter (~> 0.1)
simplecov-badge (2.0.2)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.3)
thor (1.1.0)
unicode-display_width (1.7.0)
vcr (6.0.0)
webmock (3.14.0)
simplecov_json_formatter (0.1.4)
thor (1.2.1)
unicode-display_width (1.8.0)
vcr (6.1.0)
webmock (3.17.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
Expand All @@ -129,4 +127,4 @@ DEPENDENCIES
webmock (~> 3.14)

BUNDLED WITH
2.2.26
2.2.33
Binary file modified coverage/coverage-badge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 27 additions & 9 deletions lib/veryfi/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,62 @@

module Veryfi
class Error
def self.from_response(response)
case response.status
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
else InternalError
when 500 then InternalError
else VeryfiError.new(format("%<code>d, %<message>s", code: status, message: response["error"]))
end
end

class BadRequest < StandardError
def to_s
"Bad Request"
"400, Bad Request"
end
end

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

class ResourceNotFound < StandardError
def to_s
"404, Resource not found"
end
end

class UnexpectedHTTPMethod < StandardError
def to_s
"Unexpected HTTP Method"
"405, Unexpected HTTP Method"
end
end

class AccessLimitReached < StandardError
def to_s
"Access Limit Reached"
"409, Access Limit Reached"
end
end

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

class VeryfiError < StandardError
end
end
end
9 changes: 5 additions & 4 deletions lib/veryfi/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ def make_request(http_verb, path, params = {})
headers = generate_headers(params)

response = conn.public_send(http_verb, url, body, headers)
json_response = process_response(response)

if response.success?
process_response(http_verb, response)
json_response
else
raise Veryfi::Error.from_response(response)
raise Veryfi::Error.from_response(response.status, json_response)
end
end

Expand Down Expand Up @@ -102,8 +103,8 @@ def generate_signature(params, timestamp)
Veryfi::Signature.new(client_secret, params, timestamp).to_base64
end

def process_response(_http_verb, response)
# return response if http_verb == :delete
def process_response(response)
return {} if response.body.empty?

JSON.parse(response.body)
end
Expand Down
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.0"
VERSION = "1.0.1"
end
59 changes: 49 additions & 10 deletions spec/veryfi/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,65 +29,104 @@

context "when server responds with 400 error" do
before do
stub_request(:get, /\.*/).to_return(status: 400)
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,
"Bad Request"
"400, Bad Request"
)
end
end

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

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

context "when server responds with 404 error" 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"
)
end
end

context "when server responds with 405 error" do
before do
stub_request(:get, /\.*/).to_return(status: 405)
stub_request(:get, /\.*/).to_return(status: 405, body: '{"error": "Unexpected HTTP Method"}')
end

it "raises error" do
expect { client.document.all }.to raise_error(
Veryfi::Error::UnexpectedHTTPMethod,
"Unexpected HTTP Method"
"405, Unexpected HTTP Method"
)
end
end

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

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

context "when server responds with 500 error" do
before do
stub_request(:get, /\.*/).to_return(status: 500)
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,
"Internal Server Error"
"500, Internal Server Error"
)
end
end

context "when server responds with empty body" do
before do
stub_request(:get, /\.*/).to_return(status: 501, body: "")
end

it "raises error" do
expect { client.document.all }.to raise_error(
Veryfi::Error::VeryfiError,
"501"
)
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
Expand Down

0 comments on commit af48726

Please sign in to comment.