Sha256: 3dbeb2453a68d90506cd9b6cc82ee0cb2fc35f7a19418ba7d2a0bb222fcb2211
Contents?: true
Size: 1.86 KB
Versions: 2
Compression:
Stored size: 1.86 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} #{method} #{url}\n#{errors}\n#{headers}") end def method @method.to_s.upcase rescue => e "[Unable to show HTTP method: #{e}]" end def url @response.url.to_s rescue => e "[Unable to show URL: #{e}]" end def headers JSON::pretty_generate( request: @response.request_headers, response: @response.response_headers ) rescue => e "[Unable to show headers: #{e}]" 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
footrest-0.5.0 | lib/footrest/http_error.rb |
footrest-0.4.1 | lib/footrest/http_error.rb |