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

[IE-673] Update api errors handling #21

Merged
merged 5 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
51 changes: 44 additions & 7 deletions lib/qualtrics_api/request_error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module QualtricsAPI
class RequestErrorHandler < Faraday::Response::Middleware
HTTP_RESPONSE_ERROR_RANGE = 400..599

def on_complete(env)
raise_http_errors(env[:status], env[:body])
show_notices(env[:body])
Expand All @@ -10,15 +12,39 @@ def on_complete(env)
private

def raise_http_errors(code, body)
return unless HTTP_RESPONSE_ERROR_RANGE.include?(code)
Copy link

Choose a reason for hiding this comment

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

Love the clean guard here.


raise http_error_class(code), error_message(JSON.parse(body))
end

def http_error_class(code)
case code
when 404
raise NotFoundError, error_message(JSON.parse(body))
when 400
raise BadRequestError, error_message(JSON.parse(body))
BadRequestError
when 401
raise UnauthorizedError, error_message(JSON.parse(body))
UnauthorizedError
when 403
ForbiddenError
when 404
NotFoundError
when 409
ConflictError
when 413
RequestEntityTooLargeError
when 414
URITooLongError
when 415
UnsupportedMediaTypeError
when 429
TooManyRequestsError
when 500
raise InternalServerError, error_message(JSON.parse(body))
InternalServerError
when 503
TemporaryInternalServerError
when 504
GatewayTimeoutError
else
UnknownResponseError
end
end

Expand All @@ -43,11 +69,22 @@ def error_message(response)
end
end

class NotYetFetchedError < StandardError; end
class NotFoundError < StandardError; end
# HTTP Reponse Status Errors
class BadRequestError < StandardError; end
class UnauthorizedError < StandardError; end
class ForbiddenError < StandardError; end
class NotFoundError < StandardError; end
class ConflictError < StandardError; end
class RequestEntityTooLargeError < StandardError; end
class URITooLongError < StandardError; end
class UnsupportedMediaTypeError < StandardError; end
class TooManyRequestsError < StandardError; end
class InternalServerError < StandardError; end
class TemporaryInternalServerError < StandardError; end
class GatewayTimeoutError < StandardError; end
class UnknownResponseError < StandardError; end

# Application errors
class NotSupported < StandardError; end
class FileNotReadyError < StandardError; end
end
2 changes: 1 addition & 1 deletion lib/qualtrics_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module QualtricsAPI
VERSION = "0.1.0".freeze
VERSION = "0.1.1".freeze
end
Loading