mirror of
https://github.com/harry0703/MoneyPrinterTurbo.git
synced 2026-11-14 18:22:59 +01:00
Protect v1 API routes and generated task files when an API key is configured. Preserve unauthenticated local access when the key is empty. Reported-by: ether-btc Resolves: #882
32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
import traceback
|
|
from typing import Any
|
|
|
|
from loguru import logger
|
|
|
|
|
|
class HttpException(Exception):
|
|
def __init__(
|
|
self, task_id: str, status_code: int, message: str = "", data: Any = None
|
|
):
|
|
self.message = message
|
|
self.status_code = status_code
|
|
self.data = data
|
|
# Retrieve the exception stack trace information.
|
|
tb_str = traceback.format_exc().strip()
|
|
if not tb_str or tb_str == "NoneType: None":
|
|
msg = f"HttpException: {status_code}, {task_id}, {message}"
|
|
else:
|
|
msg = f"HttpException: {status_code}, {task_id}, {message}\n{tb_str}"
|
|
|
|
# 400/401 都是可预期的客户端输入问题。尤其鉴权开启后,公网扫描可能
|
|
# 产生大量无效 Key;使用 WARNING 既保留定位信息,也避免污染 ERROR
|
|
# 告警。服务端配置错误和其它异常仍保持 ERROR。
|
|
if status_code in (400, 401):
|
|
logger.warning(msg)
|
|
else:
|
|
logger.error(msg)
|
|
|
|
|
|
class FileNotFoundException(Exception):
|
|
pass
|