From 827bc5ceefab5270d76073879ead423fce88bb8b Mon Sep 17 00:00:00 2001 From: Chris Tran Date: Mon, 13 Jan 2025 11:26:54 -0600 Subject: [PATCH] fix: adds error handling for OpenApi errors (#166) --- .rubocop.yml | 2 + lib/passageidentity/auth.rb | 10 +-- lib/passageidentity/user.rb | 124 ++++++++++++++++++++++++++++-------- 3 files changed, 103 insertions(+), 33 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 6c0bfb7..bb713ba 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -13,3 +13,5 @@ Layout/LineLength: Metrics/MethodLength: Max: 50 +Metrics/AbcSize: + Max: 20 diff --git a/lib/passageidentity/auth.rb b/lib/passageidentity/auth.rb index 04794f2..833d470 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 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) diff --git a/lib/passageidentity/user.rb b/lib/passageidentity/user.rb index 50abf74..6cf368e 100644 --- a/lib/passageidentity/user.rb +++ b/lib/passageidentity/user.rb @@ -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], @@ -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], @@ -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], @@ -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], @@ -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', @@ -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], @@ -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], @@ -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], @@ -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 @@ -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