{"id":8938,"date":"2023-09-13T01:39:47","date_gmt":"2023-09-12T17:39:47","guid":{"rendered":"\/?p=8938"},"modified":"2023-09-13T01:39:47","modified_gmt":"2023-09-12T17:39:47","slug":"fastapi%e5%9f%ba%e7%a1%80%ef%bc%9a5-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"\/?p=8938","title":{"rendered":"FastAPI\u57fa\u7840\uff1a5.\u5f02\u5e38\u5904\u7406"},"content":{"rendered":"<h2>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/h2>\n<pre><code class=\"language-python\"># \u7ee7\u627fHTTPException\nclass TokenError(HTTPException):\n    pass\n\nclass UnicornException(Exception):\n    def __init__(self, name: str):\n        print(&quot;\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b&quot;)\n        self.name = name<\/code><\/pre>\n<h2>\u5f02\u5e38\u5904\u7406<\/h2>\n<pre><code class=\"language-python\">from fastapi import FastAPI, HTTPException\nfrom starlette.responses import JSONResponse\nfrom uvicorn import run\nfrom fastapi_04 import R\n\n&quot;&quot;&quot;\n\u4e2d\u95f4\u4ef6 &amp; \u4f9d\u8d56\n&quot;&quot;&quot;\n\nclass TokenError(HTTPException):\n    pass\n\nfrom fastapi import Request\n\n# TokenError\u53d1\u751f\u65f6\uff0c\u7684\u5904\u7406\nasync def token_error(request: Request, exc: HTTPException):\n    return JSONResponse(R.fail(&quot;token\u8ba4\u8bc1\u5931\u8d25&quot;).dict())\n\n# https:\/\/www.starlette.io\/exceptions\/\nexception_handlers = {\n    # \u5f02\u5e38\u7c7b\uff1a\u53d1\u751f\u5f02\u5e38\u5bf9\u5e94\u7684\u5904\u7406\u65b9\u6cd5\n    TokenError: token_error\n}\n\napp = FastAPI(title=&quot;\u5f02\u5e38\u5904\u7406&quot;, exception_handlers=exception_handlers)\n\nclass UnicornException(Exception):\n    def __init__(self, name: str):\n        print(&quot;\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b&quot;)\n        self.name = name\n\nfrom fastapi import Request\n\n# \u53d1\u751fUnicornException \u5f02\u5e38\u65f6 \u89e6\u53d1\u51fd\u6570\n@app.exception_handler(UnicornException)\nasync def unicorn_exception_handler(request: Request, exc: UnicornException):\n    return JSONResponse(\n        status_code=418,\n        content={&quot;message&quot;: f&quot;Oops! {exc.name} did something. There goes a rainbow...&quot;},\n    )\n\n@app.get(&quot;\/fail&quot;)\nasync def fail():\n    raise UnicornException(name=&quot;demo&quot;)\n    return &quot;fail&quot;\n\n@app.get(&quot;\/ok&quot;)\nasync def ok():\n    raise TokenError(status_code=401)\n\nif __name__ == &quot;__main__&quot;:\n    # \u542f\u7528web\u670d\u52a1\uff0c reload -&gt; \u5f53\u6539\u53d8\u4ee3\u7801\u65f6\u81ea\u52a8\u91cd\u542f # fastapi_02:app\n    run(&quot;__main__:app&quot;, reload=True)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u81ea\u5b9a\u4e49\u5f02\u5e38 # \u7ee7\u627fHTTPException class TokenError(HTTPException): pass class    \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[204],"tags":[116],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8938"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8938"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8938\/revisions"}],"predecessor-version":[{"id":8939,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8938\/revisions\/8939"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8938"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}