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.
Defined Under Namespace
Modules: DynamicRescue Classes: GeniusExceptionSuperClass, LyricsNotFoundError, PageNotFound, TokenError
Constant Summary collapse
Class Method Summary collapse
-
.error_handle(token, method_name: nil) ⇒ Boolean
deprecated
Deprecated.
Since 0.2.1
- .validate_token(token, method_name: nil) ⇒ Object
Class Method Details
.error_handle(token, method_name: nil) ⇒ Boolean
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:
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
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 |