Module: Genius::Errors

Included in:
Songs
Defined in:
lib/genius/api/errors.rb

Overview

Genius::Errors module includes custom exception classes and methods to handle all errors during requests to api.genius.com or during the work with library methods.

Exception classes fields provide custom message and error types (connection_error, token_error, auth_required, etc.)

There will be a standard output of each exception if there will be no params provided.

Examples:

module Genius
  module Foo
    include Genius::Errors
    class << self
      def bar(params)
        # body
      rescue TokenError => e
        puts "Error description: #{e.msg}"            #=> Invalid token!....
        puts "Error description: #{e.exception_type}" #=> token_error
      end
    end
  end
end
begin
  raise TokenError.new(msg: "Message", error_type: "error_type")
rescue TokenError => e
  puts e.message        #=> Message
  puts e.exception_type #=> error_type
end
begin
  raise TokenError
rescue TokenError => e
  puts e.message        #=> Invalid token!....
  puts e.exception_type #=> token_error
end

Defined Under Namespace

Modules: DynamicRescue Classes: GeniusExceptionSuperClass, LyricsNotFoundError, PageNotFound, TokenError

Constant Summary collapse

ENDPOINT =

Endpoint for resource.

"#{Api::RESOURCE}/account/?access_token".freeze

Class Method Summary collapse

Class Method Details

.error_handle?(token, method_name: nil) ⇒ Boolean

Deprecated.

Use validate_token instead.

Validates token and raises on failure. Returns true if valid.

Parameters:

  • token (String?)

    Token to validate.

  • method_name (String?) (defaults to: nil)

    Optional method name for error hints.

Returns:

  • (Boolean)

Raises:

  • (StandardError)

    if token is invalid.



180
181
182
183
184
185
186
187
188
189
# File 'lib/genius/api/errors.rb', line 180

def error_handle?(token, method_name: nil)
  if token.nil?
    raise TokenError.new(msg: 'Token is required for this method. Please, add token via ' \
                              "`Genius::Auth.login=``token''` method and continue",
                         method_name: method_name)
  elsif token.size != 64 || check_status?(token) == false
    raise TokenError.new(method_name: method_name)
  end
  true
end

.validate_token(token, method_name: nil) ⇒ void

This method returns an undefined value.

Validates the access token by checking length and making a test request to the API.

Parameters:

  • token (String?)

    Token to validate.

  • method_name (String?) (defaults to: nil)

    Optional method name for error hints.

Raises:

  • (StandardError)

    if token is nil, wrong length, or invalid.



165
166
167
168
169
170
171
# File 'lib/genius/api/errors.rb', line 165

def validate_token(token, method_name: nil)
  raise TokenError.new(method_name: method_name) if token.nil? || token.size != 64

  response = HTTParty.get("#{ENDPOINT}=#{token}").body
  status = JSON.parse(response).dig('meta', 'status')
  raise TokenError.new(method_name: method_name) unless status == 200
end