require 'faraday' require 'finix/error' module Faraday class Response::RaiseApiError < Response::Middleware HTTP_STATUS_CODES = { 400 => Finix::BadRequest, 401 => Finix::Unauthorized, 403 => Finix::Forbidden, 404 => Finix::NotFound, 405 => Finix::MethodNotAllowed, 422 => Finix::UnprocessableEntity } def on_complete(response) status_code = response[:status].to_i error_class = HTTP_STATUS_CODES[status_code] raise Finix::ResourceErrors.new(response) if error_class.nil? and status_code >= 400 raise error_class.new(response) if error_class end end end