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 collapse
- MAX_RETRIES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
5
Constants included from Errors
Errors::APIError, Errors::Forbidden, Errors::InvalidRequest, Errors::NotAuthorized, Errors::NotFound, Errors::PremiumOnly, 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(num_retries = 0, &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.
-
.payment_required(response) ⇒ Object
private
Returns a PremiumOnly exception.
-
.rate_limit_enforced(response) ⇒ Object
private
Returns a RateLimitEnforced exception with a retry after field.
-
.recover_response(response) ⇒ Object
private
rubocop:disable Style/OpenStructUse.
-
.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.
106 107 108 |
# File 'lib/asana/http_client/error_handling.rb', line 106 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.
111 112 113 |
# File 'lib/asana/http_client/error_handling.rb', line 111 def body(response) JSON.parse(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.
79 80 81 |
# File 'lib/asana/http_client/error_handling.rb', line 79 def forbidden(response) Forbidden.new.tap { |exception| exception.response = response } end |
.handle(num_retries = 0, &request) ⇒ Faraday::Response
Perform a request handling any API errors correspondingly.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/asana/http_client/error_handling.rb', line 31 def handle(num_retries = 0, &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 402 then raise payment_required(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 # Retry for timeouts or 500s from Asana rescue Faraday::ServerError => e raise server_error(e.response) unless num_retries < MAX_RETRIES handle(num_retries + 1, &request) rescue Net::ReadTimeout => e raise e unless num_retries < MAX_RETRIES handle(num_retries + 1, &request) 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.
61 62 63 64 65 66 |
# File 'lib/asana/http_client/error_handling.rb', line 61 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.
69 70 71 |
# File 'lib/asana/http_client/error_handling.rb', line 69 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.
84 85 86 |
# File 'lib/asana/http_client/error_handling.rb', line 84 def not_found(response) NotFound.new.tap { |exception| exception.response = response } end |
.payment_required(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 PremiumOnly exception.
74 75 76 |
# File 'lib/asana/http_client/error_handling.rb', line 74 def payment_required(response) PremiumOnly.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.
90 91 92 93 94 95 |
# File 'lib/asana/http_client/error_handling.rb', line 90 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.
rubocop:disable Style/OpenStructUse
116 117 118 119 |
# File 'lib/asana/http_client/error_handling.rb', line 116 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.
98 99 100 101 102 103 |
# File 'lib/asana/http_client/error_handling.rb', line 98 def server_error(response) phrase = body(response).fetch('errors', []).first['phrase'] ServerError.new(phrase).tap do |exception| exception.response = response end end |