Skip to content

Commit

Permalink
added InvalidRequestTokenError error (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartes authored Mar 9, 2022
1 parent 06cdb22 commit 9284360
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## master

- [#253](https://github.com/castle/castle-ruby/pull/253)
* added InvalidRequestTokenError

## 7.1.2

- [#247](https://github.com/castle/castle-ruby/pull/247)
Expand Down
28 changes: 25 additions & 3 deletions lib/castle/core/process_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ module ProcessResponse
401 => Castle::UnauthorizedError,
403 => Castle::ForbiddenError,
404 => Castle::NotFoundError,
419 => Castle::UserUnauthorizedError,
422 => Castle::InvalidParametersError
419 => Castle::UserUnauthorizedError
}.freeze

INVALID_REQUEST_TOKEN = 'invalid_request_token'

class << self
# @param response [Response]
# @param config [Castle::Configuration, Castle::SingletonConfiguration, nil]
Expand All @@ -36,8 +37,29 @@ def verify!(response)

raise Castle::InternalServerError if response.code.to_i.between?(500, 599)

raise_error422(response) if response.code.to_i == 422

error = RESPONSE_ERRORS.fetch(response.code.to_i, Castle::ApiError)
raise error, response[:message]

raise error
end

def raise_error422(response)
if response.body
begin
parsed_body = JSON.parse(response.body, symbolize_names: true)
if parsed_body.is_a?(Hash) && parsed_body.key?(:type)
if parsed_body[:type] == INVALID_REQUEST_TOKEN
raise Castle::InvalidRequestTokenError, parsed_body[:message]
else
raise Castle::InvalidParametersError, parsed_body[:message]
end
end
rescue JSON::ParserError
end
end

raise Castle::InvalidParametersError
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/castle/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class UserUnauthorizedError < Castle::ApiError
class InvalidParametersError < Castle::ApiError
end

# api error invalid param 422 (invalid token)
class InvalidRequestTokenError < Castle::ApiError
end

# api error unauthorized 401
class UnauthorizedError < Castle::ApiError
end
Expand Down
11 changes: 8 additions & 3 deletions spec/lib/castle/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@
let(:time_auto) { time_now.utc.iso8601(3) }
let(:time_user) { (Time.now - 10_000).utc.iso8601(3) }
let(:response_body) { {}.to_json }
let(:response_code) { 200 }

let(:stub_response) do
stub_request(:any, /api.castle.io/)
.with(basic_auth: ['', 'secret'])
.to_return(status: response_code, body: response_body, headers: {})
end

before do
Timecop.freeze(time_now)
stub_const('Castle::VERSION', '2.2.0')
stub_request(:any, /api.castle.io/)
.with(basic_auth: ['', 'secret'])
.to_return(status: 200, body: response_body, headers: {})
stub_response
end

after { Timecop.return }
Expand Down
30 changes: 30 additions & 0 deletions spec/support/shared_examples/action_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,34 @@
it { expect(request_response[:failover_reason]).to be_eql('Castle::InternalServerError') }
end
end

context 'when request is 422' do
describe 'throw InvalidParametersError' do
let(:response_body) { { type: 'bad_request', message: 'wrong params' }.to_json }
let(:response_code) { 422 }

it do
expect { request_response }.to raise_error(Castle::InvalidParametersError, 'wrong params')
end
end

describe 'throw InvalidParametersError for legacy endpoints' do
let(:response_body) { {}.to_json }
let(:response_code) { 422 }

it { expect { request_response }.to raise_error(Castle::InvalidParametersError) }
end

describe 'throw InvalidRequestTokenError' do
let(:response_body) { { type: 'invalid_request_token', message: 'invalid token' }.to_json }
let(:response_code) { 422 }

it do
expect { request_response }.to raise_error(
Castle::InvalidRequestTokenError,
'invalid token'
)
end
end
end
end

0 comments on commit 9284360

Please sign in to comment.