Module: Asana::HttpClient::ErrorHandling Private
- Includes:
- Errors
- Defined in:
- lib/asana/http_client/error_handling.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Handles errors from the API and re-raises them as proper exceptions.
Constant Summary
Constants included from Errors
Errors::APIError, Errors::Forbidden, Errors::InvalidRequest, Errors::NotAuthorized, Errors::NotFound, Errors::RateLimitEnforced, Errors::ServerError
Class Method Summary collapse
-
.api_error(response) ⇒ Object
private
Returns an APIError exception.
-
.body(response) ⇒ Object
private
Parser a response body from JSON.
-
.forbidden(response) ⇒ Object
private
Returns a Forbidden exception.
-
.handle(&request) ⇒ Faraday::Response
Perform a request handling any API errors correspondingly.
-
.invalid_request(response) ⇒ Object
private
Returns an InvalidRequest exception including a list of errors.
-
.not_authorized(response) ⇒ Object
private
Returns a NotAuthorized exception.
-
.not_found(response) ⇒ Object
private
Returns a NotFound exception.
-
.rate_limit_enforced(response) ⇒ Object
private
Returns a RateLimitEnforced exception with a retry after field.
- .recover_response(response) ⇒ Object private
-
.server_error(response) ⇒ Object
private
Returns a ServerError exception with a unique phrase.
Class Method Details
.api_error(response) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns an APIError exception.
88 89 90 |
# File 'lib/asana/http_client/error_handling.rb', line 88 def api_error(response) APIError.new.tap { |exception| exception.response = response } end |
.body(response) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parser a response body from JSON.
93 94 95 |
# File 'lib/asana/http_client/error_handling.rb', line 93 def body(response) MultiJson.load(response[:body]) end |
.forbidden(response) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a Forbidden exception.
61 62 63 |
# File 'lib/asana/http_client/error_handling.rb', line 61 def forbidden(response) Forbidden.new.tap { |exception| exception.response = response } end |
.handle(&request) ⇒ Faraday::Response
Perform a request handling any API errors correspondingly.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/asana/http_client/error_handling.rb', line 29 def handle(&request) request.call rescue Faraday::ClientError => e raise e unless e.response case e.response[:status] when 400 then raise invalid_request(e.response) when 401 then raise (e.response) when 403 then raise forbidden(e.response) when 404 then raise not_found(e.response) when 412 then recover_response(e.response) when 429 then raise rate_limit_enforced(e.response) when 500 then raise server_error(e.response) else raise api_error(e.response) end end |
.invalid_request(response) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns an InvalidRequest exception including a list of errors.
48 49 50 51 52 53 |
# File 'lib/asana/http_client/error_handling.rb', line 48 def invalid_request(response) errors = body(response).fetch('errors', []).map { |e| e['message'] } InvalidRequest.new(errors).tap do |exception| exception.response = response end end |
.not_authorized(response) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a NotAuthorized exception.
56 57 58 |
# File 'lib/asana/http_client/error_handling.rb', line 56 def (response) NotAuthorized.new.tap { |exception| exception.response = response } end |
.not_found(response) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a NotFound exception.
66 67 68 |
# File 'lib/asana/http_client/error_handling.rb', line 66 def not_found(response) NotFound.new.tap { |exception| exception.response = response } end |
.rate_limit_enforced(response) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a RateLimitEnforced exception with a retry after field.
72 73 74 75 76 77 |
# File 'lib/asana/http_client/error_handling.rb', line 72 def rate_limit_enforced(response) retry_after_seconds = response[:headers]['Retry-After'] RateLimitEnforced.new(retry_after_seconds).tap do |exception| exception.response = response end end |
.recover_response(response) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
97 98 99 100 |
# File 'lib/asana/http_client/error_handling.rb', line 97 def recover_response(response) r = response.dup.tap { |res| res[:body] = body(response) } Response.new(OpenStruct.new(env: OpenStruct.new(r))) end |
.server_error(response) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a ServerError exception with a unique phrase.
80 81 82 83 84 85 |
# File 'lib/asana/http_client/error_handling.rb', line 80 def server_error(response) phrase = body(response).fetch('errors', []).first['phrase'] ServerError.new(phrase).tap do |exception| exception.response = response end end |