在使用fastapi的fastapi_jsonrpc框架时,遇到一个问题,fastapi_jsonrpc内部定义了一些错误,例如内部错误,解析错误,但是我想要捕获这些错误,然后以自己的格式返回,要怎么处理,使用了如下的格式,但是依然没有作用。
fastapi_jsonrpc 框架地址 https://github.com/smagafurov/fastapi-jsonrpc
框架内部定义的错误有
class ParseError(BaseError):
"""Invalid JSON was received by the server"""
CODE = -32700
MESSAGE = "Parse error"
class InvalidRequest(BaseError):
"""The JSON sent is not a valid Request object"""
CODE = -32600
MESSAGE = "Invalid Request"
error_model = ErrorModel
class MethodNotFound(BaseError):
"""The method does not exist / is not available"""
CODE = -32601
MESSAGE = "Method not found"
class InvalidParams(BaseError):
"""Invalid method parameter(s)"""
CODE = -32602
MESSAGE = "Invalid params"
error_model = ErrorModel
class InternalError(BaseError):
"""Internal JSON-RPC error"""
CODE = -32603
MESSAGE = "Internal error"
想要捕获这些错误,该如何处理?
@app_rpc.exception_handler(Exception)
async def default_exception_handler(request,exc):
return {"detail": "An error occurred"}