Skip to content

Commit

Permalink
Add a typed exception for 403 errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
woodhull committed Jan 17, 2024
1 parent 87e452f commit 5f60439
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/action_network_rest/response/raise_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def on_complete(response)
if error_message == 'You must specify a valid person id'
raise MustSpecifyValidPersonId, error_message(response)
end
elsif status_code == 403
raise AuthorizationError, error_message(response)
elsif status_code == 404
raise NotFoundError, error_message(response)
elsif status_code == 429
Expand All @@ -31,6 +33,8 @@ def error_message(response)
end
end

class AuthorizationError < StandardError; end

class MissingActionNetworkId < StandardError; end

class MustSpecifyValidPersonId < StandardError; end
Expand Down
7 changes: 7 additions & 0 deletions spec/response/raise_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
.to(raise_error(ActionNetworkRest::Response::MustSpecifyValidPersonId, /You must specify a valid person id/))
end


it 'should raise AuthorizationError if status is 403' do
response = { status: '403', body: { error: 'API Key invalid or not present' }.to_json }

expect { subject.on_complete(response) }.to raise_error(ActionNetworkRest::Response::AuthorizationError, /API Key invalid/)
end

it 'should raise NotFoundError if status is 404' do
response = { status: '404', body: { error: 'Not found' }.to_json }

Expand Down

0 comments on commit 5f60439

Please sign in to comment.