Sha256: b45d956c484342f8094bd9b848ceeed0518304aff3c302bcdfae561ff06a992e

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

module Notifications
  class Client
    class RequestError < StandardError
      attr_reader :code, :message

      def initialize(response)
        @code = response.code
        @message = message_from(response.body)
      end

      def to_s
        "#{code} #{message}"
      end

      def message_from(body)
        JSON.parse(body).fetch('errors')
      rescue JSON::ParserError
        body
      end
    end

    class ClientError < RequestError; end
    class BadRequestError < ClientError; end
    class AuthError < ClientError; end
    class NotFoundError < ClientError; end
    class RateLimitError < ClientError; end

    class ServerError < RequestError; end

    module ErrorHandling
      def build_error(response)
        error_class_for_code(response.code.to_i).new(response)
      end

      def error_class_for_code(code)
        case code
        when 400
          BadRequestError
        when 403
          AuthError
        when 404
          NotFoundError
        when 429
          RateLimitError
        when (400..499)
          ClientError
        when (500..599)
          ServerError
        else
          RequestError
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
notifications-ruby-client-3.1.0 lib/notifications/client/request_error.rb
notifications-ruby-client-3.0.0 lib/notifications/client/request_error.rb
notifications-ruby-client-2.10.0 lib/notifications/client/request_error.rb