Sha256: 7caf07f056822b17f15045a3452647772bb9934c5a1d4967c62d44f6d09507b3

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

require 'faraday'
require 'multi_json'

# @api private
module Faraday
  class Response::RaiseCloudConnectError < Response::Middleware
    def on_complete(response)
      case response[:status].to_i
      when 400
        raise CloudConnect::BadRequest, error_message(response)
      when 401
        raise CloudConnect::Unauthorized, error_message(response)
      when 403
        raise CloudConnect::Forbidden, error_message(response)
      when 404
        raise CloudConnect::NotFound, error_message(response)
      when 406
        raise CloudConnect::NotAcceptable, error_message(response)
      when 422
        raise CloudConnect::UnprocessableEntity, error_message(response)
      when 500
        raise CloudConnect::InternalServerError, error_message(response)
      when 501
        raise CloudConnect::NotImplemented, error_message(response)
      when 502
        raise CloudConnect::BadGateway, error_message(response)
      when 503
        raise CloudConnect::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[:title]}: #{body[:message]} Error(s): [#{body[:errors].join(", ")}]"
      else
        ''
      end
      "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{message}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cloud_connect-3.2.0 lib/faraday/response/raise_cloud_connect_error.rb
cloud_connect-3.1.0 lib/faraday/response/raise_cloud_connect_error.rb
cloud_connect-3.0.3 lib/faraday/response/raise_cloud_connect_error.rb