Skip to content

Commit

Permalink
fix: adds error handling for OpenApi errors (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctran88 authored Jan 13, 2025
1 parent a1ce3e7 commit 827bc5c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ Layout/LineLength:
Metrics/MethodLength:
Max: 50

Metrics/AbcSize:
Max: 20
10 changes: 5 additions & 5 deletions lib/passageidentity/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,16 @@ def create_magic_link(args, opts)

def handle_magic_link_creation(args)
@magic_links_client.create_magic_link(@app_id, args, @req_opts).magic_link
rescue Faraday::Error => e
raise PassageError.new(
status_code: e.response[:status],
body: e.response[:body]
)
rescue OpenapiClient::ApiError => e
raise PassageError.new(
status_code: e.code,
body: try_parse_json_string(e.response_body)
)
rescue Faraday::Error => e
raise PassageError.new(
status_code: e.response[:status],
body: e.response[:body]
)
end

def try_parse_json_string(string)
Expand Down
124 changes: 96 additions & 28 deletions lib/passageidentity/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ def initialize(app_id:, req_opts:)
end

def get(user_id:)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?
raise ArgumentError, 'user_id is required.' if blank_str?(user_id)

begin
response = @user_client.get_user(@app_id, user_id, @req_opts)
response.user
rescue OpenapiClient::ApiError => e
raise PassageError.new(
status_code: e.code,
body: try_parse_json_string(e.response_body)
)
rescue Faraday::Error => e
raise PassageError.new(
status_code: e.response[:status],
Expand All @@ -29,11 +34,16 @@ def get(user_id:)
end

def get_by_identifier(identifier:)
raise ArgumentError, 'identifier is required.' unless identifier && !identifier.empty?
raise ArgumentError, 'identifier is required.' if blank_str?(identifier)

begin
req_opts = set_get_by_identifier_query_params(identifier: identifier)
response = @user_client.list_paginated_users(@app_id, req_opts)
rescue OpenapiClient::ApiError => e
raise PassageError.new(
status_code: e.code,
body: try_parse_json_string(e.response_body)
)
rescue Faraday::Error => e
raise PassageError.new(
status_code: e.response[:status],
Expand All @@ -45,11 +55,16 @@ def get_by_identifier(identifier:)
end

def activate(user_id:)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?
raise ArgumentError, 'user_id is required.' if blank_str?(user_id)

begin
response = @user_client.activate_user(@app_id, user_id, @req_opts)
response.user
rescue OpenapiClient::ApiError => e
raise PassageError.new(
status_code: e.code,
body: try_parse_json_string(e.response_body)
)
rescue Faraday::Error => e
raise PassageError.new(
status_code: e.response[:status],
Expand All @@ -59,11 +74,16 @@ def activate(user_id:)
end

def deactivate(user_id:)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?
raise ArgumentError, 'user_id is required.' if blank_str?(user_id)

begin
response = @user_client.deactivate_user(@app_id, user_id, @req_opts)
response.user
rescue OpenapiClient::ApiError => e
raise PassageError.new(
status_code: e.code,
body: try_parse_json_string(e.response_body)
)
rescue Faraday::Error => e
raise PassageError.new(
status_code: e.response[:status],
Expand All @@ -73,35 +93,57 @@ def deactivate(user_id:)
end

def update(user_id:, options:)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?
raise ArgumentError, 'options are required.' unless options && !options.empty?

response = @user_client.update_user(@app_id, user_id, options, @req_opts)
response.user
rescue Faraday::Error => e
raise PassageError.new(
status_code: e.response[:status],
body: e.response[:body]
)
raise ArgumentError, 'user_id is required.' if blank_str?(user_id)
raise ArgumentError, 'options are required.' if options.empty?

begin
response = @user_client.update_user(@app_id, user_id, options, @req_opts)
response.user
rescue OpenapiClient::ApiError => e
raise PassageError.new(
status_code: e.code,
body: try_parse_json_string(e.response_body)
)
rescue Faraday::Error => e
raise PassageError.new(
status_code: e.response[:status],
body: e.response[:body]
)
end
end

def create(args:)
raise ArgumentError, 'At least one of args.email or args.phone is required.' unless args['phone'] || args['email']

response = @user_client.create_user(@app_id, args, @req_opts)
response.user
rescue Faraday::Error => e
raise PassageError.new(
status_code: e.response[:status],
body: e.response[:body]
)
if blank_str?(args['email']) && blank_str?(args['phone'])
raise ArgumentError,
'At least one of args.email or args.phone is required.'
end

begin
response = @user_client.create_user(@app_id, args, @req_opts)
response.user
rescue OpenapiClient::ApiError => e
raise PassageError.new(
status_code: e.code,
body: try_parse_json_string(e.response_body)
)
rescue Faraday::Error => e
raise PassageError.new(
status_code: e.response[:status],
body: e.response[:body]
)
end
end

def delete(user_id:)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?
raise ArgumentError, 'user_id is required.' if blank_str?(user_id)

begin
@user_client.delete_user(@app_id, user_id, @req_opts)
rescue OpenapiClient::ApiError => e
raise PassageError.new(
status_code: e.code,
body: try_parse_json_string(e.response_body)
)
rescue Faraday::Error => e
raise PassageError.new(
'failed to delete Passage User',
Expand All @@ -112,11 +154,16 @@ def delete(user_id:)
end

def revoke_device(user_id:, device_id:)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?
raise ArgumentError, 'device_id is required.' unless device_id && !device_id.empty?
raise ArgumentError, 'user_id is required.' if blank_str?(user_id)
raise ArgumentError, 'device_id is required.' if blank_str?(device_id)

begin
@user_device_client.delete_user_devices(@app_id, user_id, device_id, @req_opts)
rescue OpenapiClient::ApiError => e
raise PassageError.new(
status_code: e.code,
body: try_parse_json_string(e.response_body)
)
rescue Faraday::Error => e
raise PassageError.new(
status_code: e.response[:status],
Expand All @@ -126,11 +173,16 @@ def revoke_device(user_id:, device_id:)
end

def list_devices(user_id:)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?
raise ArgumentError, 'user_id is required.' if blank_str?(user_id)

begin
response = @user_device_client.list_user_devices(@app_id, user_id, @req_opts)
response.devices
rescue OpenapiClient::ApiError => e
raise PassageError.new(
status_code: e.code,
body: try_parse_json_string(e.response_body)
)
rescue Faraday::Error => e
raise PassageError.new(
status_code: e.response[:status],
Expand All @@ -140,10 +192,15 @@ def list_devices(user_id:)
end

def revoke_refresh_tokens(user_id:)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?
raise ArgumentError, 'user_id is required.' if blank_str?(user_id)

begin
@tokens_client.revoke_user_refresh_tokens(@app_id, user_id, @req_opts)
rescue OpenapiClient::ApiError => e
raise PassageError.new(
status_code: e.code,
body: try_parse_json_string(e.response_body)
)
rescue Faraday::Error => e
raise PassageError.new(
status_code: e.response[:status],
Expand All @@ -154,6 +211,11 @@ def revoke_refresh_tokens(user_id:)

private

def blank_str?(str)
blank_pattern = /\A[[:space:]]*\z/
str.empty? || blank_pattern.match?(str)
end

def set_get_by_identifier_query_params(identifier:)
req_opts = @req_opts.dup
req_opts[:limit] = 1
Expand All @@ -174,5 +236,11 @@ def handle_get_by_identifier(users:)

get(user_id: users.first.id)
end

def try_parse_json_string(string)
JSON.parse(string)
rescue JSON::ParserError
string
end
end
end

0 comments on commit 827bc5c

Please sign in to comment.