Sha256: 97106b2aeaa9b0b35a663fc2c46c126e10c8a60b26a44adc099fbd4b4e97829b

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

# coding: utf-8

require 'faraday'

module GetGithubPubKeys
  module Response
    class RaiseError < Faraday::Response::Middleware
      def on_complete(env)
        case env[:status].to_i
        when 400
          raise GetGithubPubKeys::BadRequest, error_message(env)
        when 401
          raise GetGithubPubKeys::NotAuthorized, error_message(env)
        when 403
          raise GetGithubPubKyes::Forbidden, error_message(env)
        when 404
          raise GetGithubPubKeys::NotFound, error_message(env)
        when 400...500
          raise GetGithubPubKeys::ClientError, error_message(env)
        when 500
          raise GetGithubPubKeys::InternalServerError, error_message(env)
        when 503
          raise GetGithubPubKeys::ServiceUnavailable, error_message(env)
        when 500...600
          raise GetGithubPubKeys::ServiceUnavailable, error_message(env)
        end
      end

      private
      
      def error_message(env)
        body = env[:body]
        if body.nil?
          nil
        elsif body['error'] && body['error']['message']
          body['error']['message'] 
        elsif body['fault'] && body['fault']['faultstring']
          body['fault']['faultstring']
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
get_github_pub_keys-0.0.2 lib/get_github_pub_keys/response/raise_error.rb
get_github_pub_keys-0.0.1 lib/get_github_pub_keys/response/raise_error.rb