Sha256: 5cc5beed0648849635e2cb9f71ca1b18d0ef0e30374f71161edd0f43369fcb59

Contents?: true

Size: 651 Bytes

Versions: 9

Compression:

Stored size: 651 Bytes

Contents

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
finix-0.10 lib/finix/response/finix_error_middleware.rb
finix-0.9 lib/finix/response/finix_error_middleware.rb
finix-0.8 lib/finix/response/finix_error_middleware.rb
finix-0.7 lib/finix/response/finix_error_middleware.rb
finix-0.6 lib/finix/response/finix_error_middleware.rb
finix-0.5 lib/finix/response/finix_error_middleware.rb
finix-0.4 lib/finix/response/finix_error_middleware.rb
finix-0.3 lib/finix/response/finix_error_middleware.rb
finix-0.2 lib/finix/response/finix_error_middleware.rb