from urllib import parse from fastapi import Request from fastapi.exceptions import HTTPException from fastapi.responses import RedirectResponse from starlette import status def http_exception_handler(request: Request, exc: HTTPException): # 非同期API呼び出しの場合、detailにdictが入ってくるため、そのまま流す if hasattr(exc, 'detail') is True and type(exc.detail) == dict: raise exc error_detail = exc.detail if hasattr(exc, 'detail') else '' reason = parse.quote(error_detail) return RedirectResponse(f'/logout/?reason={reason}', status_code=status.HTTP_303_SEE_OTHER)