openai/chatkit-python

Public

mirrored from https://github.com/openai/chatkit-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b231f471130128d091d1c3f38daefaa7512005fb

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

chatkit/errors.py

61lines · modeblame

f688d870victor-openai8 months ago1from abc import ABC
3de07454Dan Wang7 months ago2from enum import Enum
f688d870victor-openai8 months ago3
4
5# Not a closed enum, new error codes can and will be added as needed
3de07454Dan Wang7 months ago6class ErrorCode(str, Enum):
f688d870victor-openai8 months ago7STREAM_ERROR = "stream.error"
8
9
10DEFAULT_STATUS: dict[ErrorCode, int] = {
11ErrorCode.STREAM_ERROR: 500,
12}
13
14DEFAULT_ALLOW_RETRY: dict[ErrorCode, bool] = {
15ErrorCode.STREAM_ERROR: True,
16}
17
18
19class BaseStreamError(ABC, Exception):
20allow_retry: bool
21
22
23class StreamError(BaseStreamError):
24"""
25Error with a specific error code that maps to a localized user-facing
26error message.
27"""
28
29code: ErrorCode
30
31def __init__(
32self,
33code: ErrorCode,
34*,
35allow_retry: bool | None = None,
36):
37self.code = code
38self.status_code = DEFAULT_STATUS.get(code, 500)
39if allow_retry is None:
40self.allow_retry = DEFAULT_ALLOW_RETRY.get(code, False)
41else:
42self.allow_retry = allow_retry
43
44
45class CustomStreamError(BaseStreamError):
46"""
47Error with a custom user-facing error message. The message should be
48localized as needed before raising the error.
49"""
50
51message: str
52"""The user-facing error message to display."""
53
54def __init__(
55self,
56message: str,
57*,
58allow_retry: bool = False,
59):
60self.message = message
61self.allow_retry = allow_retry