disable rich exception output in console for API by default, use WEBUI_RICH_EXCEPTIONS env var to enable

This commit is contained in:
AUTOMATIC1111 2023-07-15 07:44:37 +03:00
parent 95ee0cb188
commit b8bd8ce4cf

View File

@ -1,5 +1,6 @@
import base64 import base64
import io import io
import os
import time import time
import datetime import datetime
import uvicorn import uvicorn
@ -98,14 +99,16 @@ def encode_pil_to_base64(image):
def api_middleware(app: FastAPI): def api_middleware(app: FastAPI):
rich_available = True rich_available = False
try: try:
import anyio # importing just so it can be placed on silent list if os.environ.get('WEBUI_RICH_EXCEPTIONS', None) is not None:
import starlette # importing just so it can be placed on silent list import anyio # importing just so it can be placed on silent list
from rich.console import Console import starlette # importing just so it can be placed on silent list
console = Console() from rich.console import Console
console = Console()
rich_available = True
except Exception: except Exception:
rich_available = False pass
@app.middleware("http") @app.middleware("http")
async def log_and_time(req: Request, call_next): async def log_and_time(req: Request, call_next):
@ -116,14 +119,14 @@ def api_middleware(app: FastAPI):
endpoint = req.scope.get('path', 'err') endpoint = req.scope.get('path', 'err')
if shared.cmd_opts.api_log and endpoint.startswith('/sdapi'): if shared.cmd_opts.api_log and endpoint.startswith('/sdapi'):
print('API {t} {code} {prot}/{ver} {method} {endpoint} {cli} {duration}'.format( print('API {t} {code} {prot}/{ver} {method} {endpoint} {cli} {duration}'.format(
t = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"), t=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"),
code = res.status_code, code=res.status_code,
ver = req.scope.get('http_version', '0.0'), ver=req.scope.get('http_version', '0.0'),
cli = req.scope.get('client', ('0:0.0.0', 0))[0], cli=req.scope.get('client', ('0:0.0.0', 0))[0],
prot = req.scope.get('scheme', 'err'), prot=req.scope.get('scheme', 'err'),
method = req.scope.get('method', 'err'), method=req.scope.get('method', 'err'),
endpoint = endpoint, endpoint=endpoint,
duration = duration, duration=duration,
)) ))
return res return res
@ -134,7 +137,7 @@ def api_middleware(app: FastAPI):
"body": vars(e).get('body', ''), "body": vars(e).get('body', ''),
"errors": str(e), "errors": str(e),
} }
if not isinstance(e, HTTPException): # do not print backtrace on known httpexceptions if not isinstance(e, HTTPException): # do not print backtrace on known httpexceptions
message = f"API error: {request.method}: {request.url} {err}" message = f"API error: {request.method}: {request.url} {err}"
if rich_available: if rich_available:
print(message) print(message)