Sha256: 31f555d24d0494b230b3d6b8ad2322a60fb26572ee37eea4992c0f650783e1d0

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

require 'faraday'
require 'multi_json'

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

    def error_message(response)
      if !response[:body].blank?
        body = ::MultiJson.decode(response[:body])
        message = body["error"] || body["message"]
      end
      "#{response[:method].to_s.upcase} #{response[:url].to_s} [#{response[:status]}]: #{message}"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
githu3-0.0.8 lib/faraday/response/raise_githu3_error.rb
githu3-0.0.7 lib/faraday/response/raise_githu3_error.rb
githu3-0.0.6 lib/faraday/response/raise_githu3_error.rb
githu3-0.0.5 lib/faraday/response/raise_githu3_error.rb
githu3-0.0.4 lib/faraday/response/raise_githu3_error.rb
githu3-0.0.3 lib/faraday/response/raise_githu3_error.rb