-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f8dbb3
commit d08df3b
Showing
7 changed files
with
178 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from .app import FastAPIApp | ||
from .FastAPI_APP import FastAPI_App | ||
def run(config_path,host,port): | ||
FastAPIApp(config_path=config_path)._run(host=host,port=port) | ||
FastAPI_App(config_path=config_path)._run(host=host,port=port) | ||
def return_app(config_path): | ||
app = FastAPIApp(config_path=config_path).app | ||
app = FastAPI_App(config_path=config_path).app | ||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,22 @@ | ||
from .utils import ok_json, error_json, Config, file_path_join,file_if | ||
|
||
from .utils import ok_json, Config, file_path_join,CustomError | ||
async def jm_file(file_name: str, config_path: str): | ||
"""提供文件""" | ||
config = Config(config_path=config_path) | ||
|
||
|
||
|
||
try: | ||
# 拼接路径,构造JSON | ||
path = file_path_join([config.temp_output, file_name]) | ||
return ok_json.file(path=path) | ||
|
||
except FileNotFoundError as e: | ||
# 文件未找到,返回 404 错误 | ||
return error_json.not_found( | ||
msg="文件不存在", | ||
log=f"文件 {file_name} 不存在,Msg: {str(e)}" | ||
raise CustomError.not_found( | ||
msg="文件未找到", | ||
log=f"未找到文件: {file_name}, Msg: {str(e)}" | ||
) | ||
|
||
except Exception as e: | ||
# 其他未知错误 | ||
return error_json.unknown_error( | ||
log=f"文件服务 遇到未知异常,文件名: {file_name}, Msg: {str(e)}" | ||
raise CustomError.unknown_error( | ||
log=f"未知错误: {str(e)}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from .json import Error as error_json | ||
from .json import Ok as ok_json | ||
from .json import _Dict | ||
from .config import Config | ||
from .file import file_if,file_path_join | ||
from .temp import temp_if | ||
from .temp import temp_if | ||
from .exceptions import CustomError,CustomHTTPException |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from inspect import stack | ||
|
||
class CustomError(Exception): | ||
"""自定义异常类,用于处理错误响应并返回详细的异常信息""" | ||
|
||
def __init__(self, msg: str, code: int, log: str, status: str): | ||
# 直接在 __init__ 中获取调用模块的名称 | ||
module_name = stack()[2].function | ||
super().__init__(msg) # 调用父类构造函数 | ||
self.code = code | ||
self.msg = msg | ||
self.status = status | ||
self.module_name = module_name | ||
self.log = log | ||
|
||
@staticmethod | ||
def json(code: int, status: str, msg: str, log: str): | ||
"""返回自定义格式的错误响应对象""" | ||
return CustomError(msg=msg, code=code, log=log, status=status) | ||
|
||
@staticmethod | ||
def _raise_error(msg: str, log: str, code: int): | ||
"""内部错误,返回错误对象""" | ||
return CustomError( | ||
msg=msg, | ||
code=code, | ||
log=log, | ||
status='Internal Server Error' | ||
) | ||
|
||
@staticmethod | ||
def not_found(msg: str, log: str): | ||
"""返回404错误响应对象""" | ||
return CustomError( | ||
code=404, | ||
msg=msg, | ||
log=log, | ||
status="Not Found" | ||
) | ||
|
||
@staticmethod | ||
def unknown_error(log: str): | ||
"""返回500未知错误的响应对象""" | ||
return CustomError( | ||
code=500, | ||
msg="Unknown Error", | ||
log=log, | ||
status="Unknown Error" | ||
) | ||
|
||
def to_dict(self): | ||
"""将异常信息转换为字典格式,方便其他框架使用""" | ||
return { | ||
"code": self.code, | ||
"msg": self.msg, | ||
"log": self.log, | ||
"status": self.status, | ||
"module_name": self.module_name | ||
} | ||
|
||
|
||
# FastAPI 专用的异常类 | ||
from fastapi import HTTPException | ||
|
||
class CustomHTTPException(HTTPException): | ||
"""FastAPI 专用的异常类,继承自 HTTPException""" | ||
|
||
def __init__(self, custom_error: CustomError): | ||
self.status_code = custom_error.code | ||
self.code = custom_error.code | ||
self.msg = custom_error.msg | ||
self.log = custom_error.log | ||
super().__init__(status_code=self.status_code, detail=self._format_detail()) | ||
|
||
def _format_detail(self): | ||
return { | ||
"data": { | ||
"msg": self.msg, | ||
"log": self.log, | ||
} | ||
} |
Oops, something went wrong.