Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
Stable Diffusion Webui
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
novelai-storage
Stable Diffusion Webui
Commits
5387576c
Commit
5387576c
authored
Mar 15, 2023
by
Vladimir Mandic
Committed by
GitHub
Mar 15, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
api error handler
parent
a9fed7c3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
1 deletion
+45
-1
modules/api/api.py
modules/api/api.py
+44
-1
requirements.txt
requirements.txt
+1
-0
No files found.
modules/api/api.py
View file @
5387576c
...
...
@@ -6,8 +6,11 @@ import uvicorn
from
threading
import
Lock
from
io
import
BytesIO
from
gradio.processing_utils
import
decode_base64_to_file
from
fastapi
import
APIRouter
,
Depends
,
FastAPI
,
HTTPException
,
Request
,
Response
from
fastapi
import
APIRouter
,
Depends
,
FastAPI
,
Request
,
Response
from
fastapi.security
import
HTTPBasic
,
HTTPBasicCredentials
from
fastapi.exceptions
import
HTTPException
from
fastapi.responses
import
JSONResponse
from
fastapi.encoders
import
jsonable_encoder
from
secrets
import
compare_digest
import
modules.shared
as
shared
...
...
@@ -90,6 +93,16 @@ def encode_pil_to_base64(image):
return
base64
.
b64encode
(
bytes_data
)
def
api_middleware
(
app
:
FastAPI
):
rich_available
=
True
try
:
import
anyio
# importing just so it can be placed on silent list
import
starlette
# importing just so it can be placed on silent list
from
rich.console
import
Console
console
=
Console
()
except
:
import
traceback
rich_available
=
False
@
app
.
middleware
(
"http"
)
async
def
log_and_time
(
req
:
Request
,
call_next
):
ts
=
time
.
time
()
...
...
@@ -110,6 +123,36 @@ def api_middleware(app: FastAPI):
))
return
res
def
handle_exception
(
request
:
Request
,
e
:
Exception
):
err
=
{
"error"
:
type
(
e
)
.
__name__
,
"detail"
:
vars
(
e
)
.
get
(
'detail'
,
''
),
"body"
:
vars
(
e
)
.
get
(
'body'
,
''
),
"errors"
:
str
(
e
),
}
print
(
f
"API error: {request.method}: {request.url} {err}"
)
if
not
isinstance
(
e
,
HTTPException
):
# do not print backtrace on known httpexceptions
if
rich_available
:
console
.
print_exception
(
show_locals
=
True
,
max_frames
=
2
,
extra_lines
=
1
,
suppress
=
[
anyio
,
starlette
],
word_wrap
=
False
,
width
=
min
([
console
.
width
,
200
]))
else
:
traceback
.
print_exc
()
return
JSONResponse
(
status_code
=
vars
(
e
)
.
get
(
'status_code'
,
500
),
content
=
jsonable_encoder
(
err
))
@
app
.
middleware
(
"http"
)
async
def
exception_handling
(
request
:
Request
,
call_next
):
try
:
return
await
call_next
(
request
)
except
Exception
as
e
:
return
handle_exception
(
request
,
e
)
@
app
.
exception_handler
(
Exception
)
async
def
fastapi_exception_handler
(
request
:
Request
,
e
:
Exception
):
return
handle_exception
(
request
,
e
)
@
app
.
exception_handler
(
HTTPException
)
async
def
http_exception_handler
(
request
:
Request
,
e
:
HTTPException
):
return
handle_exception
(
request
,
e
)
class
Api
:
def
__init__
(
self
,
app
:
FastAPI
,
queue_lock
:
Lock
):
...
...
requirements.txt
View file @
5387576c
...
...
@@ -30,3 +30,4 @@ GitPython
torchsde
safetensors
psutil
rich
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