Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
PyFund
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陈涛
PyFund
Commits
a0885d49
Commit
a0885d49
authored
Mar 21, 2023
by
yangmingcheng@matrixone.io
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
5bbbb95f
Pipeline
#402
canceled with stages
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
63 additions
and
4 deletions
+63
-4
__init__.py
exception/__init__.py
+17
-0
db.py
exception/db.py
+3
-1
http.py
exception/http.py
+4
-1
token.py
exception/token.py
+5
-2
main.py
main.py
+34
-0
No files found.
exception/__init__.py
View file @
a0885d49
import
traceback
from
typing
import
Optional
from
loguru
import
logger
class
MyException
(
Exception
):
default_error
=
'系统错误'
status_code
=
400
def
__init__
(
self
,
message
:
Optional
[
str
]
=
None
):
if
not
message
:
logger
.
warning
(
traceback
.
format_exc
())
self
.
message
=
message
or
self
.
default_error
def
__str__
(
self
):
return
self
.
message
exception/db.py
View file @
a0885d49
# 数据不存在异常
from
starlette
import
status
from
exception
import
MyException
class
NotFundError
(
Exception
):
class
NotFundError
(
MyException
):
status_code
=
status
.
HTTP_404_NOT_FOUND
exception/http.py
View file @
a0885d49
class
ReqException
(
Exception
):
from
exception
import
MyException
class
ReqException
(
MyException
):
pass
exception/token.py
View file @
a0885d49
class
RequestPubKeyError
(
Exception
):
from
exception
import
MyException
class
RequestPubKeyError
(
MyException
):
pass
class
TokenError
(
Exception
):
class
TokenError
(
My
Exception
):
pass
main.py
View file @
a0885d49
import
traceback
import
uvicorn
as
uvicorn
from
fastapi
import
FastAPI
from
fastapi.exceptions
import
RequestValidationError
from
loguru
import
logger
from
starlette
import
status
from
starlette.requests
import
Request
from
api
import
api_router
from
configs
import
settings
from
db.mongodb_helper
import
AioMongodbManager
from
exception
import
MyException
from
model
import
error_response
app
=
FastAPI
()
...
...
@@ -12,5 +21,30 @@ app.state.mongodb_manger = mongodb_manger
# 添加路由
app
.
include_router
(
api_router
)
@
app
.
exception_handler
(
MyException
)
async
def
not_fund_exception_handler
(
request
:
Request
,
exc
:
MyException
):
return
error_response
(
str
(
exc
),
status_code
=
exc
.
status_code
)
@
app
.
exception_handler
(
RequestValidationError
)
async
def
request_validation_exception_handler
(
request
:
Request
,
exc
:
RequestValidationError
):
"""
请求参数验证异常
:param request: 请求头信息
:param exc: 异常对象
:return:
"""
# 日志记录异常详细上下文
return
error_response
(
'参数错误 '
+
str
(
exc
),
status_code
=
status
.
HTTP_400_BAD_REQUEST
)
@
app
.
exception_handler
(
Exception
)
async
def
sys_exception_handler
(
request
:
Request
,
exc
:
Exception
):
logger
.
error
(
f
"全局异
\n
{request.method}URL{request.url}
\n
Headers:{request.headers}
\n
{traceback.format_exc()}"
)
return
error_response
(
'系统异常'
+
f
' {str(exc)}'
if
settings
.
name
in
[
'本地环境'
,
"测试环境"
]
else
''
,
status_code
=
status
.
HTTP_500_INTERNAL_SERVER_ERROR
)
if
__name__
==
'__main__'
:
uvicorn
.
run
(
'main:app'
,
host
=
'0.0.0.0'
,
port
=
8000
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment