Skip to content

Commit

Permalink
fix: update PassageError message precedence (#120)
Browse files Browse the repository at this point in the history
* set explicit msg precedence in PassageError

* align get-by-identifier error with other SDKs

* use symbols to reference error body param

* update handwritten error codes to match auth OpenAPI schema

* lint
  • Loading branch information
bertrmz authored Dec 11, 2024
1 parent afb787a commit 0e3bfbf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
18 changes: 9 additions & 9 deletions lib/passageidentity/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def authenticate_request(request)
if @auth_strategy == Passage::COOKIE_STRATEGY
unless request.cookies.key?('psg_auth_token')
raise PassageError.new(
status_code: 400,
status_code: 401,
body: {
error: 'missing authentication token: expected "psg_auth_token" cookie',
code: 'missing_auth_token'
code: 'invalid_access_token'
}
)
end
Expand All @@ -47,10 +47,10 @@ def authenticate_request(request)
headers = request.headers
unless headers.key?('Authorization')
raise PassageError.new(
status_code: 400,
status_code: 401,
body: {
error: 'no authentication token in header',
code: 'missing_auth_token'
code: 'invalid_access_token'
}
)
end
Expand All @@ -68,8 +68,8 @@ def validate_jwt(token)
raise PassageError.new(
status_code: 401,
body: {
error: 'invalid authentication token',
code: 'invalid_jwks'
error: 'invalid JWKs',
code: 'invalid_access_token'
}
)
end
Expand All @@ -96,7 +96,7 @@ def validate_jwt(token)
status_code: 401,
body: {
error: e.message,
code: 'invalid_jwt'
code: 'invalid_access_token'
}
)
end
Expand Down Expand Up @@ -139,7 +139,7 @@ def create_magic_link(
status_code: 400,
body: {
error: 'channel: must be either Passage::EMAIL_CHANNEL or Passage::PHONE_CHANNEL',
code: 'bad_request_data'
code: 'invalid_request'
}
)
end
Expand Down Expand Up @@ -220,7 +220,7 @@ def user_exists?(user_id)
status_code: 400,
body: {
error: 'Must supply a valid user_id',
code: 'user_not_found'
code: 'invalid_request'
}
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/passageidentity/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def initialize(app_id:, api_key: '', auth_strategy: COOKIE_STRATEGY)
status_code: 400,
body: {
error: 'Invalid auth strategy',
code: 'invalid_auth_strategy'
code: 'invalid_argument'
}
)
end
Expand Down
49 changes: 36 additions & 13 deletions lib/passageidentity/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,44 @@ module Passage
class PassageError < StandardError
attr_reader :status_code, :error_code, :message, :status_text, :error

def initialize(message: nil, status_code: nil, body: nil)
super(message)
def initialize(message: nil, status_code: nil, body: {})
# Ensure body is a hash
body = {} if body.nil?

body = symbolize_keys(body)
@status_code = status_code
@error_code = body['code']
@message = body['error']
@status_text =
(
if status_code.nil?
nil
else
Net::HTTPResponse::CODE_TO_OBJ[status_code.to_s]
end
)
@error = body.nil? ? nil : body['error']
@error_code = body[:code]
# Message precedence: explicit message > body error > default message
@message = determine_message(message, body)
# Determine status text from status code if available
@status_text = determine_status_text(status_code)
# Set error field based on message
@error = @message

super(@message)
end

private

def determine_message(message, body)
return message if message

return body[:error] if body[:error]

'Unknown error occurred'
end

def determine_status_text(code)
return nil if code.nil?

status_code_str = code.to_s
Net::HTTPResponse::CODE_TO_OBJ[status_code_str]
rescue StandardError
nil
end

def symbolize_keys(hash)
hash.transform_keys(&:to_sym)
end
end
end
2 changes: 1 addition & 1 deletion lib/passageidentity/user_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_by_identifier(user_identifier:)
raise PassageError.new(
status_code: 404,
body: {
error: "Passage User with identifer \"#{user_identifier}\" does not exist",
error: 'User not found.',
code: 'user_not_found'
}
)
Expand Down

0 comments on commit 0e3bfbf

Please sign in to comment.