Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adds error handling for OpenApi errors #166

Merged
merged 4 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

@ctran88 ctran88 Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was getting linter errors that code like this was too complicated with a score of 18.79/17

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?
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],
body: e.response[:body]
)
end
end

but i figured that's not too bad, so adjusting this limit instead

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Ruby methods that return a boolean typically end with a question mark.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Loading