Skip to content

Commit

Permalink
fix: adds error handling for OpenApi errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ctran88 committed Jan 10, 2025
1 parent a1ce3e7 commit 0fbea15
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
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 OpenapiClient::ApiError => e
raise PassageError.new(

Check failure on line 112 in lib/passageidentity/auth.rb

View workflow job for this annotation

GitHub Actions / Format

Layout/IndentationWidth: Use 2 (not 4) spaces for indentation.
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]
)
rescue OpenapiClient::ApiError => e
raise PassageError.new(
status_code: e.code,
body: try_parse_json_string(e.response_body)
)
end

def try_parse_json_string(string)
Expand Down
57 changes: 56 additions & 1 deletion lib/passageidentity/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def get(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 @@ -34,6 +39,11 @@ def get_by_identifier(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 @@ -50,12 +60,16 @@ def activate(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],
body: e.response[:body]
)
end
end

def deactivate(user_id:)
Expand All @@ -64,6 +78,11 @@ def deactivate(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 @@ -78,6 +97,11 @@ def update(user_id:, options:)

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],
Expand All @@ -90,6 +114,11 @@ def create(args:)

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],
Expand All @@ -102,6 +131,11 @@ def delete(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 @@ -117,6 +151,11 @@ def revoke_device(user_id:, 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 @@ -131,6 +170,11 @@ def list_devices(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 @@ -144,6 +188,11 @@ def revoke_refresh_tokens(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 Down Expand Up @@ -174,5 +223,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 0fbea15

Please sign in to comment.