Sha256: edca35edf03ca136d02d921e377fde2f195cef6cf21d19e2fa4c990269b3d816

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

require 'faraday'
require 'multi_json'

# @api private
module Faraday
  class Response::RaiseOctokitError < Response::Middleware
    def on_complete(response)
      case response[:status].to_i
      when 400
        raise Octokit::BadRequest, error_message(response)
      when 401
        raise Octokit::Unauthorized, error_message(response)
      when 403
        raise Octokit::Forbidden, error_message(response)
      when 404
        raise Octokit::NotFound, error_message(response)
      when 406
        raise Octokit::NotAcceptable, error_message(response)
      when 422
        raise Octokit::UnprocessableEntity, error_message(response)
      when 500
        raise Octokit::InternalServerError, error_message(response)
      when 501
        raise Octokit::NotImplemented, error_message(response)
      when 502
        raise Octokit::BadGateway, error_message(response)
      when 503
        raise Octokit::ServiceUnavailable, error_message(response)
      end
    end

    def error_message(response)
      message = if ( body = response[:body] ) && !body.empty?
        if body.is_a? String
          body = ::MultiJson.decode body, :symbolize_keys => true
        end
        ": #{body[:error] || body[:message] || ''}"
      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
octokit-1.0.2 lib/faraday/response/raise_octokit_error.rb
octokit-1.0.1 lib/faraday/response/raise_octokit_error.rb
octokit-1.0.0 lib/faraday/response/raise_octokit_error.rb