Skip to content

Commit

Permalink
Update error handling to new conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenclouston committed Nov 25, 2024
1 parent 6473eab commit bdf9cd0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ Authsignal.enroll_verified_authenticator user_id: 'AS_001',
# returns:
# {
# success?: false,
# status: 400,
# error: 'invalid_request',
# status_code: 400,
# error_code: 'invalid_request',
# error_description: '/body/oobChannel must be equal to one of the allowed values'
# }
```
Expand All @@ -109,7 +109,7 @@ Authsignal.enroll_verified_authenticator! user_id: 'AS_001',
}

# raise:
# <Authsignal::ApiError: invalid_request status: 400, error: invalid_request, description: /body/oobChannel must be equal to one o...
# <Authsignal::ApiError: AuthsignalError: 400 - /body/oobChannel must be equal to one of the allowed values. status_code: 401, error_code: invalid_request, error_description: /body/oobChannel must be equal to one of the allowed values.
```

## Development
Expand Down
12 changes: 6 additions & 6 deletions lib/authsignal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ def handle_success_response(response)
def handle_error_response(response)
case response.body
when Hash
response.body.merge(status: response.status, success?: false)
{ status_code: response.status, success?: false, error_code: response.body[:error], error_description: response.body[:error_description] }
else
{ status: response&.status || 500, success?: false }
{ status_code: response&.status || 500, success?: false }
end
end
end
Expand All @@ -110,11 +110,11 @@ def handle_error_response(response)
(methods - NON_API_METHODS).each do |method|
define_singleton_method("#{method}!") do |*args, **kwargs|
send(method, *args, **kwargs).tap do |response|
status = response[:status]
err = response[:error]
desc = response[:error_description]
status_code = response[:status_code]
error_code = response[:error_code]
error_description = response[:error_description]

raise ApiError.new(err, status, err, desc) unless response[:success?]
raise ApiError.new(status_code, error_code, error_description) unless response[:success?]
end
end
end
Expand Down
24 changes: 18 additions & 6 deletions lib/authsignal/api_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@

module Authsignal
class ApiError < StandardError
attr_reader :status, :error, :description
attr_reader :status_code, :error_code, :error_description

def initialize(message = "An unexpected API error occurred", status, error, description)
@status = status || 500
@error = error
@description = description
def initialize(status_code, error_code, error_description = nil)
message = format_message(status_code, error_code, error_description)

super(message)

@status_code = status_code
@error_code = error_code
@error_description = error_description
end

def to_s
"#{super} status: #{status}, error: #{error}, description: #{description}"
"#{super} status_code: #{status_code}, error_code: #{error_code}, error_description: #{error_description}"
end

private

def format_message(status_code, error_code, error_description)
"AuthsignalError: #{status_code} - #{format_description(error_code, error_description)}"
end

def format_description(error_code, error_description)
error_description && error_description.length > 0 ? error_description : error_code
end
end
end
10 changes: 5 additions & 5 deletions spec/authsignal/authsignal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
body: { error: "unauthorized", errorDescription: "Session expired" }.to_json )

response = described_class.track(action: "signIn", idempotency_key: idempotency_key, user_id: "123")
expect(response).to eq status: 401, error: "unauthorized", error_description: "Session expired", success?: false
expect(response).to eq status_code: 401, error_code: "unauthorized", error_description: "Session expired", success?: false
end
end

Expand Down Expand Up @@ -187,7 +187,7 @@
.to_return(status: 400)

response = described_class.track(action: "signIn", idempotency_key: idempotency_key, user_id: "123")
expect(response).to eq status: 400, success?: false
expect(response).to eq status_code: 400, success?: false
end

it 'handles errors' do
Expand All @@ -196,7 +196,7 @@
.to_return_json(status: 401, body: { error: "unauthorized", errorDescription: "Session expired" } )

response = described_class.track(action: "signIn", idempotency_key: idempotency_key, user_id: "123")
expect(response).to eq status: 401, error: "unauthorized", error_description: "Session expired", success?: false
expect(response).to eq status_code: 401, error_code: "unauthorized", error_description: "Session expired", success?: false
end

end
Expand Down Expand Up @@ -343,7 +343,7 @@
})
.to_return(
status: 404,
body: {"message":"Not Found"}.to_json,
body: {"error":"unauthorized", "errorDescription":"The request is unauthorized. Check that your API key and region base URL are correctly configured."}.to_json,
headers: {'Content-Type' => 'application/json'}
)

Expand All @@ -352,7 +352,7 @@
token: "token",
)

expect(response).to eq status: 404, message: "Not Found", success?: false
expect(response).to eq error_code: "unauthorized", status_code: 404, error_description: "The request is unauthorized. Check that your API key and region base URL are correctly configured.", success?: false
end
end
end

0 comments on commit bdf9cd0

Please sign in to comment.