Sha256: 82aee337cf4c18733c231dd852dae17359dc37ff1a58903c5a13242c67e15804

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

require 'faraday'

# @api private
module Faraday
  class Response::RaiseError < Response::Middleware
    def self.register_on_complete(env)
      env[:response].on_complete do |response|
        case response[:status].to_i
        when 400
          raise Open311::BadRequest, error_message(response)
        when 401
          raise Open311::Unauthorized, error_message(response)
        when 403
          raise Open311::Forbidden, error_message(response)
        when 404
          raise Open311::NotFound, error_message(response)
        when 406
          raise Open311::NotAcceptable, error_message(response)
        when 500
          raise Open311::InternalServerError, error_message(response)
        when 502
          raise Open311::BadGateway, error_message(response)
        when 503
          raise Open311::ServiceUnavailable, error_message(response)
        end
      end
    end

    def initialize(app)
      super
      @parser = nil
    end

    private

    def self.error_message(response)
      "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:response_headers]['status']}#{(': ' + response[:body]['error']) if response[:body] && response[:body]['error']}"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
open311-0.1.2 lib/faraday/raise_error.rb
open311-0.1.1 lib/faraday/raise_error.rb
open311-0.0.1 lib/faraday/raise_error.rb
open311-0.0.0 lib/faraday/raise_error.rb