Sha256: 835e164280bce16a6c2a08376227259b68f8a43321eb34a795b3e25cf2002581

Contents?: true

Size: 974 Bytes

Versions: 9

Compression:

Stored size: 974 Bytes

Contents

module Helium
  # Custom error class for rescuing from Helium API errors
  class Error < StandardError

    # Returns the appropriate Helium::Error subclass based on status and
    # response message
    #
    # @param [Typhoeus::Response] response
    # @return [Helium::Error]
    def self.from_response(response)
      status  = response.code
      # Default the error message in the case of no error body
      message = if response.body && response.body.length >= 2
                  JSON.parse(response.body)["errors"].first["detail"]
                else
                  "Unknown error with code: #{response.code}"
                end

      klass =  case status
               when 401   then Helium::InvalidApiKey
               else self
               end

      klass.new(message)
    end
  end

  # Raised on errors in the 400-499 range
  class ClientError < Error; end

  # Raised when Helium returns a 401 error
  class InvalidApiKey < ClientError; end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
helium-ruby-0.26.0 lib/helium/error.rb
helium-ruby-0.25.0 lib/helium/error.rb
helium-ruby-0.24.0 lib/helium/error.rb
helium-ruby-0.23.0 lib/helium/error.rb
helium-ruby-0.22.0 lib/helium/error.rb
helium-ruby-0.21.0 lib/helium/error.rb
helium-ruby-0.20.0 lib/helium/error.rb
helium-ruby-0.19.0 lib/helium/error.rb
helium-ruby-0.18.0 lib/helium/error.rb