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"

Class Method Summary collapse

Class Method Details

.error_handle(token, method_name: nil) ⇒ Boolean

Deprecated.

Since 0.2.1

Genius::Errors.error_handle(token) -> true or false

name where exception was raised. This method is necessary to handle all errors during validation. token param is not optional and it is needed to validate token itself. method_name param optional and it to passes method name in error exception for dynamical error message, and because of unimportance this method is nil by default. If you are ready to pass method, it will look like this:

Examples:

begin
  Genius::Errors.validate_token(token)
rescue Genius::Errors::TokenError => e
  puts e.message
  puts e.exception_type
end
begin
  Genius::Errors.error_handle(token, method_name: __method__)
rescue Genius::Errors::TokenError => e
  puts e.message
  puts e.exception_type
end

Parameters:

  • token (String)

    Token to access api.genius.com.

  • method_name (NilClass or String) (defaults to: nil)

    Optional param to pass method

Returns:

  • (Boolean)


200
201
202
203
204
205
206
207
208
209
# File 'lib/genius/api/errors.rb', line 200

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, method_name: method_name
  end
  true
end

.validate_token(token, method_name: nil) ⇒ Object

Raises:



162
163
164
165
166
167
168
# File 'lib/genius/api/errors.rb', line 162

def validate_token(token, method_name: nil)
  raise TokenError, 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, method_name: method_name unless status == 200
end