aboutsummaryrefslogtreecommitdiff
path: root/pandora/errors.py
blob: c978708011e12b81dfe8c59f5f92591ba1485fae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
Pandora API Exceptions

This module contains the API exceptions from the Pandora API. The exception
classes are automatically generated from the API spec below and are added to
this module at first import. Exceptions will always be a sub-class of the base
PandoraException.

The name of the exception class is the message name with space removed and each
word capitalized. If the exception message contains a dash the class name will
not contain the dash or anything that follows it.
"""

__API_EXCEPTIONS__ = {
    0: "Internal Server Error",
    1: "Maintenance Mode",
    2: "Missing API Method",
    3: "Missing Auth Token",
    4: "Missing Partner ID",
    5: "Missing User ID",
    6: "Secure Protocol Required",
    7: "Certificate Required",
    8: "Parameter Type Mismatch",
    9: "Parameter Missing",
    10: "Parameter Value Invalid",
    11: "API Version Not Supported",
    12: "Pandora not available in this country",
    13: "Bad Sync Time",
    14: "Unknown Method Name",
    15: "Wrong Protocol - (http/https)",
    1000: "Read Only Mode",
    1001: "Invalid Auth Token",
    1002: "Invalid Partner Login",
    1003: "Listener Not Authorized - Subscription or Trial Expired",
    1004: "User Not Authorized",
    1005: "Station limit reached",
    1006: "Station does not exist",
    1009: "Device Not Found",
    1010: "Partner Not Authorized",
    1011: "Invalid Username",
    1012: "Invalid Password",
    1023: "Device Model Invalid",
    1039: "Too many requests for a new playlist",
    9999: "Authentication Required",
}


class PandoraException(Exception):
    """Pandora API Exception

    Translates exceptions to user readable info.
    """

    code = None
    message = "Unknown Exception"

    def __init__(self, extended_message=""):
        self.extended_message = extended_message
        super().__init__(self.message)

    @classmethod
    def from_code(cls, code, extended_message):
        exc = __API_EXCEPTIONS__.get(code)

        if not exc:
            exc = PandoraException(extended_message)
            exc.code = code
            return exc
        else:
            return exc(extended_message)

    @staticmethod
    def _format_name(name):
        output = []

        for part in name.split(" "):
            if part == "-":
                break
            else:
                output.append(part.capitalize())

        return "".join(output)

    @staticmethod
    def export_exceptions(export_to):
        for code, api_message in __API_EXCEPTIONS__.items():
            name = PandoraException._format_name(api_message)

            exception = type(name, (PandoraException,), {
                "code": code,
                "message": api_message,
            })

            export_to[name] = __API_EXCEPTIONS__[code] = exception


PandoraException.export_exceptions(locals())


class InvalidUserLogin(InvalidPartnerLogin):  # noqa: F821
    """Pydora Internal Login Error

    This is thrown around a user login to disambiguate a login that is invalid
    due to user error vs a login that is invalid due to a partner credential
    error. The Pandora API returns 1002 in both cases.
    """

    message = "Invalid User Login"