From ea129520e25b8b437b4340289e2c910251dd1b99 Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Tue, 10 Sep 2024 23:57:45 +0200 Subject: [PATCH] Refactor to cut repetition in raise_error's status code handling --- lib/faraday/response/raise_error.rb | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/lib/faraday/response/raise_error.rb b/lib/faraday/response/raise_error.rb index 054e44bc..0b543219 100644 --- a/lib/faraday/response/raise_error.rb +++ b/lib/faraday/response/raise_error.rb @@ -8,6 +8,16 @@ class RaiseError < Middleware # rubocop:disable Naming/ConstantName ClientErrorStatuses = (400...500) ServerErrorStatuses = (500...600) + ClientErrorStatusesWithCustomExceptions = { + 400 => Faraday::BadRequestError, + 401 => Faraday::UnauthorizedError, + 403 => Faraday::ForbiddenError, + 404 => Faraday::ResourceNotFound, + 408 => Faraday::RequestTimeoutError, + 409 => Faraday::ConflictError, + 422 => Faraday::UnprocessableEntityError, + 429 => Faraday::TooManyRequestsError + }.freeze # rubocop:enable Naming/ConstantName DEFAULT_OPTIONS = { include_request: true, allowed_statuses: [] }.freeze @@ -16,26 +26,12 @@ def on_complete(env) return if Array(options[:allowed_statuses]).include?(env[:status]) case env[:status] - when 400 - raise Faraday::BadRequestError, response_values(env) - when 401 - raise Faraday::UnauthorizedError, response_values(env) - when 403 - raise Faraday::ForbiddenError, response_values(env) - when 404 - raise Faraday::ResourceNotFound, response_values(env) + when *ClientErrorStatusesWithCustomExceptions.keys + raise ClientErrorStatusesWithCustomExceptions[env[:status]], response_values(env) when 407 # mimic the behavior that we get with proxy requests with HTTPS msg = %(407 "Proxy Authentication Required") raise Faraday::ProxyAuthError.new(msg, response_values(env)) - when 408 - raise Faraday::RequestTimeoutError, response_values(env) - when 409 - raise Faraday::ConflictError, response_values(env) - when 422 - raise Faraday::UnprocessableEntityError, response_values(env) - when 429 - raise Faraday::TooManyRequestsError, response_values(env) when ClientErrorStatuses raise Faraday::ClientError, response_values(env) when ServerErrorStatuses