File size: 1,259 Bytes
7ce5341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import inspect

from loguru import logger


class CursorWebError(Exception):
    def __init__(self, status_code: int, message: str, response_status_code: int = 500):
        self.status_code = status_code
        self.message = message
        self.response_status_code = response_status_code
        # 获取调用者信息
        frame = inspect.currentframe()
        try:
            caller_frame = frame.f_back  # 上一层调用栈
            if caller_frame:
                filename = caller_frame.f_code.co_filename
                line_number = caller_frame.f_lineno
                function_name = caller_frame.f_code.co_name
                logger.error(f"{self.__str__()} - Called from {filename}:{line_number} in {function_name}")
            else:
                logger.error(self.__str__())
        finally:
            del frame  # 避免循环引用

    def __str__(self) -> str:
        return f"CursorWebError: {self.status_code}, {self.message}"

    def to_openai_error(self) -> dict[str, dict[str, str]]:
        return {
            "error": {
                "message": self.__str__(),
                "type": "cursorweb_error",
                "code": "cursorweb_error"
            }
        }