From 5f604393f1b9d904541645e6cf47826a973ad9ab Mon Sep 17 00:00:00 2001 From: Nathan Woodhull Date: Tue, 16 Jan 2024 22:59:18 -0500 Subject: [PATCH] Add a typed exception for 403 errors. --- lib/action_network_rest/response/raise_error.rb | 4 ++++ spec/response/raise_error_spec.rb | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/lib/action_network_rest/response/raise_error.rb b/lib/action_network_rest/response/raise_error.rb index 0aa8416..bef65da 100644 --- a/lib/action_network_rest/response/raise_error.rb +++ b/lib/action_network_rest/response/raise_error.rb @@ -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 @@ -31,6 +33,8 @@ def error_message(response) end end + class AuthorizationError < StandardError; end + class MissingActionNetworkId < StandardError; end class MustSpecifyValidPersonId < StandardError; end diff --git a/spec/response/raise_error_spec.rb b/spec/response/raise_error_spec.rb index a3f6fa2..4a1234d 100644 --- a/spec/response/raise_error_spec.rb +++ b/spec/response/raise_error_spec.rb @@ -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 }