forked from openai/chatkit-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.py
More file actions
61 lines (46 loc) · 1.32 KB
/
Copy patherrors.py
File metadata and controls
61 lines (46 loc) · 1.32 KB
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
from abc import ABC
from enum import StrEnum
# Not a closed enum, new error codes can and will be added as needed
class ErrorCode(StrEnum):
STREAM_ERROR = "stream.error"
DEFAULT_STATUS: dict[ErrorCode, int] = {
ErrorCode.STREAM_ERROR: 500,
}
DEFAULT_ALLOW_RETRY: dict[ErrorCode, bool] = {
ErrorCode.STREAM_ERROR: True,
}
class BaseStreamError(ABC, Exception):
allow_retry: bool
class StreamError(BaseStreamError):
"""
Error with a specific error code that maps to a localized user-facing
error message.
"""
code: ErrorCode
def __init__(
self,
code: ErrorCode,
*,
allow_retry: bool | None = None,
):
self.code = code
self.status_code = DEFAULT_STATUS.get(code, 500)
if allow_retry is None:
self.allow_retry = DEFAULT_ALLOW_RETRY.get(code, False)
else:
self.allow_retry = allow_retry
class CustomStreamError(BaseStreamError):
"""
Error with a custom user-facing error message. The message should be
localized as needed before raising the error.
"""
message: str
"""The user-facing error message to display."""
def __init__(
self,
message: str,
*,
allow_retry: bool = False,
):
self.message = message
self.allow_retry = allow_retry