File size: 4,577 Bytes
fa90b1d
 
cb15d3a
3e848bc
f36740b
 
 
c006992
3e848bc
f36740b
 
 
 
3e848bc
 
 
 
 
65c7dd4
3e848bc
 
 
f36740b
cb15d3a
 
f36740b
c006992
cb15d3a
 
 
3e848bc
 
 
 
f36740b
 
 
c006992
78d0ff1
 
 
cb15d3a
78d0ff1
3e848bc
 
f06c14e
0f08209
 
 
 
 
78d0ff1
0f08209
 
 
a3c0252
f36740b
 
 
c006992
78d0ff1
 
 
 
 
a3c0252
f06c14e
 
82f3d24
f36740b
 
 
c006992
78d0ff1
 
 
82f3d24
 
 
f06c14e
f36740b
 
 
c006992
f36740b
cb15d3a
f06c14e
f36740b
 
 
 
92cc291
f36740b
c006992
78d0ff1
 
 
 
 
f36740b
 
 
 
c006992
 
8d368b7
 
 
 
 
 
 
78d0ff1
8d368b7
 
 
 
 
f5c8ab8
 
92cc291
f5c8ab8
 
78d0ff1
 
cb15d3a
78d0ff1
f5c8ab8
 
 
 
 
 
1f09504
c006992
 
 
78d0ff1
c006992
5a8d2be
c006992
 
 
 
 
5a8d2be
c006992
5a8d2be
c006992
 
 
 
 
1f09504
 
41a7e8f
 
 
 
78d0ff1
41a7e8f
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
__copyright__ = "Copyright (C) 2020 Nidhal Baccouri"


class BaseError(Exception):
    """
    base error structure class
    """

    def __init__(self, val, message):
        """
        @param val: actual value
        @param message: message shown to the user
        """
        self.val = val
        self.message = message
        super().__init__()

    def __str__(self):
        return "{} --> {}".format(self.val, self.message)


class LanguageNotSupportedException(BaseError):
    """
    exception thrown if the user uses a language
    that is not supported by the deep_translator
    """

    def __init__(
        self, val, message="There is no support for the chosen language"
    ):
        super().__init__(val, message)


class NotValidPayload(BaseError):
    """
    exception thrown if the user enters an invalid payload
    """

    def __init__(
        self,
        val,
        message="text must be a valid text with maximum 5000 character, otherwise it cannot be translated",  # noqa
    ):
        super(NotValidPayload, self).__init__(val, message)


class InvalidSourceOrTargetLanguage(BaseError):
    """
    exception thrown if the user enters an invalid payload
    """

    def __init__(self, val, message="Invalid source or target language!"):
        super(InvalidSourceOrTargetLanguage, self).__init__(val, message)


class TranslationNotFound(BaseError):
    """
    exception thrown if no translation was found for the text provided by the user
    """

    def __init__(
        self,
        val,
        message="No translation was found using the current translator. Try another translator?",
    ):
        super(TranslationNotFound, self).__init__(val, message)


class ElementNotFoundInGetRequest(BaseError):
    """
    exception thrown if the html element was not found in the body parsed by beautifulsoup
    """

    def __init__(
        self, val, message="Required element was not found in the API response"
    ):
        super(ElementNotFoundInGetRequest, self).__init__(val, message)


class NotValidLength(BaseError):
    """
    exception thrown if the provided text exceed the length limit of the translator
    """

    def __init__(self, val, min_chars, max_chars):
        message = f"Text length need to be between {min_chars} and {max_chars} characters"
        super(NotValidLength, self).__init__(val, message)


class RequestError(Exception):
    """
    exception thrown if an error occurred during the request call, e.g a connection problem.
    """

    def __init__(
        self,
        message="Request exception can happen due to an api connection error. "
        "Please check your connection and try again",
    ):
        self.message = message

    def __str__(self):
        return self.message


class MicrosoftAPIerror(Exception):
    """
    exception thrown if Microsoft API returns one of its errors
    """

    def __init__(self, api_message):
        self.api_message = str(api_message)
        self.message = "Microsoft API returned the following error"

    def __str__(self):
        return "{}: {}".format(self.message, self.api_message)


class TooManyRequests(Exception):
    """
    exception thrown if an error occurred during the request call, e.g a connection problem.
    """

    def __init__(
        self,
        message="Server Error: You made too many requests to the server. According to google, you are allowed to make 5 requests per second and up to 200k requests per day. You can wait and try again later or you can try the translate_batch function",  # noqa
    ):
        self.message = message

    def __str__(self):
        return self.message


class ServerException(Exception):
    """
    Default YandexTranslate exception from the official website
    """

    errors = {
        400: "ERR_BAD_REQUEST",
        401: "ERR_KEY_INVALID",
        402: "ERR_KEY_BLOCKED",
        403: "ERR_DAILY_REQ_LIMIT_EXCEEDED",
        404: "ERR_DAILY_CHAR_LIMIT_EXCEEDED",
        413: "ERR_TEXT_TOO_LONG",
        429: "ERR_TOO_MANY_REQUESTS",
        422: "ERR_UNPROCESSABLE_TEXT",
        500: "ERR_INTERNAL_SERVER_ERROR",
        501: "ERR_LANG_NOT_SUPPORTED",
        503: "ERR_SERVICE_NOT_AVAIBLE",
    }

    def __init__(self, status_code, *args):
        message = self.errors.get(status_code, "API server error")
        super(ServerException, self).__init__(message, *args)


class AuthorizationException(Exception):
    def __init__(self, api_key, *args):
        msg = "Unauthorized access with the api key " + api_key
        super().__init__(msg, *args)