Sha256: ffc8bcbb165794ee79a715e9b27c823feccdec9be1e8e7f320519c7bd8c2fd1a
Contents?: true
Size: 1.4 KB
Versions: 3
Compression:
Stored size: 1.4 KB
Contents
require 'faraday' require 'json' module Footrest module HttpError class ErrorBase < StandardError attr_reader :status, :body, :method, :response def initialize(response=nil) @response = response @status = @response[:status] @body = @response[:body] @method = @response[:method] super("HTTP ERROR: #{@status} #{errors}") end def errors JSON::pretty_generate(JSON::parse(@body)["errors"]) rescue @body end end %w( BadRequest Unauthorized Forbidden NotFound MethodNotAllowed InternalServerError NotImplemented BadGateway ServiceUnavailable ).each do |error| const_set error.to_sym, Class.new(ErrorBase) end end class RaiseFootrestErrors < Faraday::Response::Middleware ERROR_MAP = { 400 => Footrest::HttpError::BadRequest, 401 => Footrest::HttpError::Unauthorized, 403 => Footrest::HttpError::Forbidden, 404 => Footrest::HttpError::NotFound, 405 => Footrest::HttpError::MethodNotAllowed, 500 => Footrest::HttpError::InternalServerError, 501 => Footrest::HttpError::NotImplemented, 502 => Footrest::HttpError::BadGateway, 503 => Footrest::HttpError::ServiceUnavailable } def on_complete(response) key = response[:status].to_i raise ERROR_MAP[key].new(response) if ERROR_MAP.has_key? key end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
footrest-0.3.1 | lib/footrest/http_error.rb |
footrest-0.3.0 | lib/footrest/http_error.rb |
footrest-0.2.2 | lib/footrest/http_error.rb |