Sha256: b1399e60549561df719d5cd5f7421fc46498f65666c1d3b1a09822ce78100e17

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

require 'faraday'

module Smartdc
  class Response
    class RaiseError < Faraday::Response::Middleware
      def on_complete(env)
        case env[:status]
        when 400
          raise Smartdc::Error::BadRequest, response_values(env)
        when 401
          raise Smartdc::Error::Unauthorized, response_values(env)
        when 403
          raise Smartdc::Error::Forbidden, response_values(env)
        when 404
          raise Smartdc::Error::NotFound, response_values(env)
        when 405
          raise Smartdc::Error::MethodNotAllowed, response_values(env)
        when 406
          raise Smartdc::Error::NotAcceptable, response_values(env)
        when 409
          raise Smartdc::Error::Conflict, response_values(env)
        when 500
          raise Smartdc::Error::InternalServerError, response_values(env)
        when 501 
          raise Smartdc::Error::NotImplemented, response_values(env)
        when 502
          raise Smartdc::Error::BadGateway, response_values(env)
        when 503
          raise Smartdc::Error::ServiceUnavailable, response_values(env)
        end
      end

      def response_values(env)
        {:status => env[:status], :headers => env[:response_headers], :body => env[:body]}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smartdc-1.3.0 lib/smartdc/response/raise_error.rb