Sha256: 8caa8c7afbb71cbdfadff7d974e63f6005456b01033ac20b4d59b32cb2bcac6f

Contents?: true

Size: 941 Bytes

Versions: 1

Compression:

Stored size: 941 Bytes

Contents

module Dropmark
  module Error
    class BadRequest < StandardError; end
    class Forbidden < StandardError; end
    class NotFound < StandardError; end
    class ServerError < StandardError; end

    class RaiseError < Faraday::Response::Middleware
      def parse(body)
        if body = MultiJson.load(body, :symbolize_keys => true)
          body[:message] if body.has_key?(:message)
        end
      end
      
      def on_complete(env)
        return if (status = env[:status]) < 400 # Ignore any non-error response codes
        
        message = parse(env[:body])
        case status
        when 400
          raise Dropmark::Error::BadRequest, message
        when 403
          raise Dropmark::Error::Forbidden, message
        when 404
          raise Dropmark::Error::NotFound, message
        else
          raise Dropmark::Error::ServerError, message # Treat any other errors as 500
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dropmark-0.1.5 lib/dropmark/error.rb