-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6eb4cb9
fix: adds error handling for OpenApi errors
ctran88 982974a
chore: slightly bump AbcSize rule to accommodate the multiple rescue …
ctran88 bcbdce2
fix: param guard correctly checks for blank string
ctran88 d8dbf31
refactor: add ? to end of method returning boolean
ctran88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ Layout/LineLength: | |
Metrics/MethodLength: | ||
Max: 50 | ||
|
||
Metrics/AbcSize: | ||
Max: 20 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
passage-ruby/lib/passageidentity/user.rb
Lines 153 to 170 in 982974a
but i figured that's not too bad, so adjusting this limit instead