Sha256: ca0a394143152e72d992cfc95dd244609750c0b4ce60a4191c11e3af00dc9f2a

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

require 'faraday'
require 'hashie'

module Faraday
  class Response::RaiseHttpError < Response::Middleware
    def on_complete(response)
      case response[:status].to_i
      when 400
        raise Lelylan::BadRequest, error_message(response)
      when 401
        raise Lelylan::Unauthorized, error_message(response)
      when 403
        raise Lelylan::Forbidden, error_message(response)
      when 404
        raise Lelylan::NotFound, error_message(response)
      when 406
        raise Lelylan::NotAcceptable, error_message(response)
      when 422
        raise Lelylan::UnprocessableEntity, error_message(response)
      when 500
        raise Lelylan::InternalServerError, error_message(response)
      when 501
        raise Lelylan::NotImplemented, error_message(response)
      when 502
        raise Lelylan::BadGateway, error_message(response)
      when 503
        raise Lelylan::ServiceUnavailable, error_message(response)
      end
    end

    def error_message(response)
      body = response[:body] || ''

      begin
        body = Hashie::Mash.new(JSON.parse(response[:body]))
      rescue
      end

      body.is_a?(::Hashie::Mash) ? body.error.description : body
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lelylan-rb-0.1.0 lib/faraday/response/raise_http_error.rb
lelylan-rb-0.0.5 lib/faraday/response/raise_http_error.rb
lelylan-rb-0.0.4 lib/faraday/response/raise_http_error.rb