Sha256: 932f86c366d29525dd2bfc4608a400d61378e08595597434cfe028fe797ec0ce
Contents?: true
Size: 1.2 KB
Versions: 62
Compression:
Stored size: 1.2 KB
Contents
# frozen_string_literal: true module ErpIntegration module Middleware # The `ErrorHandling` middleware allows to raise our own exceptions that # are easier to catch for the host application (e.g. `mejuri-web`). class ErrorHandling < Faraday::Response::Middleware HTTP_ERROR_CODES = { 400 => ErpIntegration::HttpError::BadRequest, 401 => ErpIntegration::HttpError::AuthorizationRequired, 402 => ErpIntegration::HttpError::PaymentRequired, 403 => ErpIntegration::HttpError::Forbidden, 404 => ErpIntegration::HttpError::NotFound, 405 => ErpIntegration::HttpError::MethodNotAllowed, 406 => ErpIntegration::HttpError::NotAccepted, 422 => ErpIntegration::HttpError::UnprocessableEntity, 429 => ErpIntegration::HttpError::TooManyRequests, 500 => ErpIntegration::HttpError::InternalServerError }.freeze def on_complete(response) key = response[:status].to_i raise HTTP_ERROR_CODES[key], response_values(response) if HTTP_ERROR_CODES.key?(key) end def response_values(response) { status: response.status, headers: response.response_headers, body: response.body } end end end end
Version data entries
62 entries across 62 versions & 1 rubygems