lastfmget.errors

Custom Exception subclasses for lastfmget.

View Source
"""
Custom Exception subclasses for lastfmget.
"""
from enum import Enum

class __LastFmErrorCodes(Enum):
    """Last.fm response error codes."""
    InvalidParams = 6
    InvalidApiKey = 10
    Offline       = 11
    RateLimit     = 29

def raise_lastfm_error(code, msg=''):
    """
    Raises a LastFmError exception.

    Arguments:
      * code (int) -- Error code
      * msg (str) -- Error message
    """
    match code:
        case __LastFmErrorCodes.InvalidParams.value: raise ParamError(msg)
        case __LastFmErrorCodes.InvalidApiKey.value: raise ApiKeyError
        case __LastFmErrorCodes.Offline.value:       raise OfflineError
        case __LastFmErrorCodes.RateLimit.value:     raise RateLimitError
        case _:                                      raise LastFmError(code, msg)

class LastFmGetError(Exception):
    """Generic lastfmget error."""

    def __init__(self, msg='Generic lastfmget error'):
        """Calls Exception(msg)"""
        super().__init__(f'LastFmGetError: {msg}')

class NotConfiguredError(LastFmGetError):
    """lastfmget not configured - must call lastfmget.init() first."""

    def __init__(self, msg='lastfmget not configured'):
        """Calls LastFmGetError(msg)"""
        super().__init__(f'NotConfiguredError: {msg}')


class LastFmError(LastFmGetError):
    """Generic Last.fm response error."""

    def __init__(self, msg='Generic Last.fm response error', code=None):
        """Calls LastFmGetError(msg)"""
        codestr = f'[{code}]' if code else ''
        super().__init__(f'LastFmError{codestr}: {msg}')

class ParamError(LastFmError):
    """Invalid parameters provided - example: user not found."""

    def __init__(self, msg):
        """Calls LastFmError(msg)"""
        super().__init__(f'ParamError: {msg}')

class ApiKeyError(LastFmError):
    """Last.fm API key is invalid."""

    def __init__(self, msg='Invalid API key provided'):
        """Calls LastFmError(msg)"""
        super().__init__(f'ApiKeyError: {msg}')

class OfflineError(LastFmError):
    """Last.fm offline."""

    def __init__(self, msg='Last.fm is offline'):
        """Calls LastFmError(msg)"""
        super().__init__(f'OfflineError: {msg}')

class RateLimitError(LastFmError):
    """Last.fm API rate limit exceeded - decrease api_cfg.call_rate."""

    def __init__(self, msg='Last.fm API rate limit exceeded'):
        """Calls LastFmError(msg)"""
        super().__init__(f'RateLimitError: {msg}')
#   def raise_lastfm_error(code, msg=''):
View Source
def raise_lastfm_error(code, msg=''):
    """
    Raises a LastFmError exception.

    Arguments:
      * code (int) -- Error code
      * msg (str) -- Error message
    """
    match code:
        case __LastFmErrorCodes.InvalidParams.value: raise ParamError(msg)
        case __LastFmErrorCodes.InvalidApiKey.value: raise ApiKeyError
        case __LastFmErrorCodes.Offline.value:       raise OfflineError
        case __LastFmErrorCodes.RateLimit.value:     raise RateLimitError
        case _:                                      raise LastFmError(code, msg)

Raises a LastFmError exception.

Arguments:

  • code (int) -- Error code
  • msg (str) -- Error message
#   class LastFmGetError(builtins.Exception):
View Source
class LastFmGetError(Exception):
    """Generic lastfmget error."""

    def __init__(self, msg='Generic lastfmget error'):
        """Calls Exception(msg)"""
        super().__init__(f'LastFmGetError: {msg}')

Generic lastfmget error.

#   LastFmGetError(msg='Generic lastfmget error')
View Source
    def __init__(self, msg='Generic lastfmget error'):
        """Calls Exception(msg)"""
        super().__init__(f'LastFmGetError: {msg}')

Calls Exception(msg)

#   class NotConfiguredError(LastFmGetError):
View Source
class NotConfiguredError(LastFmGetError):
    """lastfmget not configured - must call lastfmget.init() first."""

    def __init__(self, msg='lastfmget not configured'):
        """Calls LastFmGetError(msg)"""
        super().__init__(f'NotConfiguredError: {msg}')

lastfmget not configured - must call lastfmget.init() first.

#   NotConfiguredError(msg='lastfmget not configured')
View Source
    def __init__(self, msg='lastfmget not configured'):
        """Calls LastFmGetError(msg)"""
        super().__init__(f'NotConfiguredError: {msg}')

Calls LastFmGetError(msg)

#   class LastFmError(LastFmGetError):
View Source
class LastFmError(LastFmGetError):
    """Generic Last.fm response error."""

    def __init__(self, msg='Generic Last.fm response error', code=None):
        """Calls LastFmGetError(msg)"""
        codestr = f'[{code}]' if code else ''
        super().__init__(f'LastFmError{codestr}: {msg}')

Generic Last.fm response error.

#   LastFmError(msg='Generic Last.fm response error', code=None)
View Source
    def __init__(self, msg='Generic Last.fm response error', code=None):
        """Calls LastFmGetError(msg)"""
        codestr = f'[{code}]' if code else ''
        super().__init__(f'LastFmError{codestr}: {msg}')

Calls LastFmGetError(msg)

#   class ParamError(LastFmError):
View Source
class ParamError(LastFmError):
    """Invalid parameters provided - example: user not found."""

    def __init__(self, msg):
        """Calls LastFmError(msg)"""
        super().__init__(f'ParamError: {msg}')

Invalid parameters provided - example: user not found.

#   ParamError(msg)
View Source
    def __init__(self, msg):
        """Calls LastFmError(msg)"""
        super().__init__(f'ParamError: {msg}')

Calls LastFmError(msg)

#   class ApiKeyError(LastFmError):
View Source
class ApiKeyError(LastFmError):
    """Last.fm API key is invalid."""

    def __init__(self, msg='Invalid API key provided'):
        """Calls LastFmError(msg)"""
        super().__init__(f'ApiKeyError: {msg}')

Last.fm API key is invalid.

#   ApiKeyError(msg='Invalid API key provided')
View Source
    def __init__(self, msg='Invalid API key provided'):
        """Calls LastFmError(msg)"""
        super().__init__(f'ApiKeyError: {msg}')

Calls LastFmError(msg)

#   class OfflineError(LastFmError):
View Source
class OfflineError(LastFmError):
    """Last.fm offline."""

    def __init__(self, msg='Last.fm is offline'):
        """Calls LastFmError(msg)"""
        super().__init__(f'OfflineError: {msg}')

Last.fm offline.

#   OfflineError(msg='Last.fm is offline')
View Source
    def __init__(self, msg='Last.fm is offline'):
        """Calls LastFmError(msg)"""
        super().__init__(f'OfflineError: {msg}')

Calls LastFmError(msg)

#   class RateLimitError(LastFmError):
View Source
class RateLimitError(LastFmError):
    """Last.fm API rate limit exceeded - decrease api_cfg.call_rate."""

    def __init__(self, msg='Last.fm API rate limit exceeded'):
        """Calls LastFmError(msg)"""
        super().__init__(f'RateLimitError: {msg}')

Last.fm API rate limit exceeded - decrease api_cfg.call_rate.

#   RateLimitError(msg='Last.fm API rate limit exceeded')
View Source
    def __init__(self, msg='Last.fm API rate limit exceeded'):
        """Calls LastFmError(msg)"""
        super().__init__(f'RateLimitError: {msg}')

Calls LastFmError(msg)