Sha256: 29f67c84b37a7451b1873677e0ef5fdead80c57529e798d654b6850e25714bfd

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require 'faraday'
require 'multi_json'

# @api private
module Faraday
  class Response::RaiseOctokitError < Response::Middleware
    ERROR_MAP = {
      400 => Octokit::BadRequest,
      401 => Octokit::Unauthorized,
      403 => Octokit::Forbidden,
      404 => Octokit::NotFound,
      406 => Octokit::NotAcceptable,
      422 => Octokit::UnprocessableEntity,
      500 => Octokit::InternalServerError,
      501 => Octokit::NotImplemented,
      502 => Octokit::BadGateway,
      503 => Octokit::ServiceUnavailable
    }

    def on_complete(response)
      key = response[:status].to_i
      raise ERROR_MAP[key], error_message(response) if ERROR_MAP.has_key? key
    end

    def error_message(response)
      message = if (body = response[:body]) && !body.empty?
        if body.is_a?(String)
          body = MultiJson.load(body, :symbolize_keys => true)
        end
        ": #{body[:error] || body[:message] || ''}"
      else
        ''
      end
      errors = unless message.empty?
        body[:errors] ?  ": #{body[:errors].map{|e|e[:message]}.join(', ')}" : ''
      end
      "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{message}#{errors}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
octokit-1.21.0 lib/faraday/response/raise_octokit_error.rb
octokit-1.20.0 lib/faraday/response/raise_octokit_error.rb