Sha256: cf2c6c0fd57a725d04dc1101cbbb77976945484acba4f8a2835d7fe671a7595d

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

require 'faraday'
require 'multi_json'

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

    def error_message(response)
      message = if (body = response[:body]) && !body.empty?
        if body.is_a?(String)
          body = MultiJson.load(body, :symbolize_keys => true)
        end
        ": #{body[:error] || body[:message] || ''}"
      else
        ''
      end
      "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{message}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ted_api-0.1 lib/faraday/response/raise_ted_api_error.rb