From 0fbea15ae76f28134bcd8bfd94c068d8c4d8f62a Mon Sep 17 00:00:00 2001 From: Chris Tran Date: Fri, 10 Jan 2025 14:27:20 -0600 Subject: [PATCH] fix: adds error handling for OpenApi errors --- lib/passageidentity/auth.rb | 10 +++---- lib/passageidentity/user.rb | 57 ++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/lib/passageidentity/auth.rb b/lib/passageidentity/auth.rb index 04794f2..cf6bb98 100644 --- a/lib/passageidentity/auth.rb +++ b/lib/passageidentity/auth.rb @@ -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( + 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) diff --git a/lib/passageidentity/user.rb b/lib/passageidentity/user.rb index 50abf74..85ef3da 100644 --- a/lib/passageidentity/user.rb +++ b/lib/passageidentity/user.rb @@ -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], @@ -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], @@ -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:) @@ -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], @@ -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], @@ -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], @@ -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', @@ -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], @@ -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], @@ -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], @@ -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