You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

8.1 KiB

🚚

📤 📚🌐 👆 💪 🚨 👩‍💻 👈 ⚙️ 👆 🛠️.

👉 👩‍💻 💪 🖥 ⏮️ 🕸, 📟 ➡️ 👱 🙆, ☁ 📳, ️.

👆 💪 💪 💬 👩‍💻 👈:

  • 👩‍💻 🚫 ✔️ 🥃 😌 👈 🛠️.
  • 👩‍💻 🚫 ✔️ 🔐 👈 ℹ.
  • 🏬 👩‍💻 🔄 🔐 🚫 🔀.
  • ️.

👫 💼, 👆 🔜 🛎 📨 🇺🇸🔍 👔 📟4️⃣0️⃣0️⃣ (➡️ 4️⃣0️⃣0️⃣ 4️⃣9️⃣9️⃣).

👉 🎏 2️⃣0️⃣0️⃣ 🇺🇸🔍 👔 📟 (➡️ 2️⃣0️⃣0️⃣ 2️⃣9️⃣9️⃣). 👈 "2️⃣0️⃣0️⃣" 👔 📟👈 😫 📤 "🏆" 📨.

👔 📟 4️⃣0️⃣0️⃣ ↔ ⛓ 👈 📤 ➡️ 👩‍💻.

💭 🌐 👈 "4️⃣0️⃣4️⃣ 🚫 🔎" (& 🤣)

⚙️ HTTPException

📨 🇺🇸🔍 📨 ⏮️ 👩‍💻 👆 ⚙️ HTTPException.

🗄 HTTPException

{* ../../docs_src/handling_errors/tutorial001.py hl[1] *}

🤚 HTTPException 👆 📟

HTTPException 😐 🐍⏮️ 🌖 📊 🔗 🔗.

↩️ 🐍 ⚠, 👆 🚫 return ️, 👆 raise ️.

👉👈 🚥 👆 🔘 🚙 🔢 👈 👆 🤙 🔘 👆 🛠️ 🔢, & 👆 🤚 HTTPException ➡️ 🔘 👈 🚙 🔢, 🏆 🚫 🏃 🎂 📟 🛠️ 🔢, 🔜 👈 📨 ▶️↖️ & 📨 🇺🇸🔍 ➡️ HTTPException 👩‍💻.

💰 🙋‍♀ ⚠ 🤭 return😅 💲 🔜 🌖 📄 🔃 🔗 & 💂‍♂.

👉 🖼, 🕐 👩‍💻 📨 🏬 🆔 👈 🚫 🔀, 🤚⏮️ 👔 📟 404:

{* ../../docs_src/handling_errors/tutorial001.py hl[11] *}

📉 📨

🚥 👩‍💻 📨 http://example.com/items/foo ( item_id "foo"), 👈 👩‍💻 🔜 📨 🇺🇸🔍 👔 📟 2️⃣0️⃣0️⃣, & 🎻 📨:

{
  "item": "The Foo Wrestlers"
}

🚥 👩‍💻 📨 http://example.com/items/bar (🚫-🚫 item_id "bar"), 👈 👩‍💻 🔜 📨 🇺🇸🔍 👔 📟 4️⃣0️⃣4️⃣ ("🚫 🔎" ), & 🎻 📨:

{
  "detail": "Item not found"
}

/// tip

🕐 🙋‍♀ HTTPException, 👆 💪 🚶‍♀️ 🙆 💲 👈 💪 🗜 🎻 🔢 detail, 🚫 🕴 str.

👆 💪 🚶‍♀️ dict, list, ️.

👫 🍵 🔁 FastAPI & 🗜 🎻.

///

🚮 🛃 🎚

📤🌐 ️ ⚠ 💪 🚮 🛃 🎚 🇺🇸🔍 . 🖼, 🆎 💂‍♂.

👆 🎲 🏆 🚫 💪 ⚙️ 🔗 👆 📟.

💼 👆 💪 🏧 😐, 👆 💪 🚮 🛃 🎚:

{* ../../docs_src/handling_errors/tutorial002.py hl[14] *}

🛃🐕‍🦺

👆 💪 🚮 🛃🐕‍🦺 ⏮️ 🎏 ⚠ 🚙 ⚪️➡️ 💃.

➡️ 💬 👆 ✔️ 🛃UnicornException 👈 👆 (⚖️ 🗃 👆 ⚙️) 💪 raise.

& 👆 💚 🍵 👉🌐 ⏮️ FastAPI.

👆 💪 🚮 🛃🐕‍🦺 ⏮️ @app.exception_handler():

{* ../../docs_src/handling_errors/tutorial003.py hl[5:7,13:18,24] *}

📥, 🚥 👆 📨 /unicorns/yolo, 🛠️ 🔜 raise UnicornException.

🔜 🍵 unicorn_exception_handler.

, 👆 🔜 📨 🧹 , ⏮️ 🇺🇸🔍 👔 📟 418 & 🎻 🎚:

{"message": "Oops! yolo did something. There goes a rainbow..."}

/// note | 📡

👆 💪 ⚙️ from starlette.requests import Request & from starlette.responses import JSONResponse.

FastAPI 🚚 🎏 starlette.responses fastapi.responses 🏪 👆, 👩‍💻. 🌅 💪 📨 👟 🔗 ➡️ 💃. 🎏 ⏮️ Request.

///

🔐 🔢🐕‍🦺

FastAPI ✔️ 🔢🐕‍🦺.

👫 🐕‍🦺 🈚 🛬 🔢 🎻 📨 🕐 👆 raise HTTPException & 🕐 📨 ✔️ 💽.

👆 💪 🔐 👫🐕‍🦺 ⏮️ 👆 👍.

🔐 📨 🔬

🕐 📨 🔌 📊, FastAPI 🔘 🤚 RequestValidationError.

& 🔌 🔢🐕‍🦺 ️.

🔐 ️, 🗄 RequestValidationError & ⚙️ ⏮️ @app.exception_handler(RequestValidationError) 🎀🐕‍🦺.

🐕‍🦺 🔜 📨 Request & ⚠.

{* ../../docs_src/handling_errors/tutorial004.py hl[2,14:16] *}

🔜, 🚥 👆 🚶 /items/foo, ↩️ 💆‍♂ 🔢 🎻 ⏮️:

{
    "detail": [
        {
            "loc": [
                "path",
                "item_id"
            ],
            "msg": "value is not a valid integer",
            "type": "type_error.integer"
        }
    ]
}

👆 🔜 🤚, ⏮️:

1 validation error
path -> item_id
  value is not a valid integer (type=type_error.integer)

RequestValidationError 🆚 ValidationError

/// warning

👫 📡👈 👆 💪 🚶 🚥 🚫👆 🔜.

///

RequestValidationError 🎧-🎓 Pydantic ValidationError.

FastAPI ⚙️ 👈, 🚥 👆 ⚙️ Pydantic 🏷 response_model, & 👆 💽 ✔️ , 👆 🔜 👀 👆 🕹.

👩‍💻/👩‍💻 🔜 🚫 👀 ️. ↩️, 👩‍💻 🔜 📨 "🔗 💽 " ⏮️ 🇺🇸🔍 👔 📟 500.

🔜 👉 🌌 ↩️ 🚥 👆 ✔️ Pydantic ValidationError 👆 📨 ⚖️ 🙆 👆 📟 (🚫 👩‍💻 📨), 🤙 🐛 👆 📟.

& 👆 🔧 ️, 👆 👩‍💻/👩‍💻 🚫🔜 🚫 ✔️ 🔐 🔗🔃 , 👈 💪 🎦 💂‍♂ ⚠.

🔐 HTTPException 🐕‍🦺

🎏 🌌, 👆 💪 🔐 HTTPException 🐕‍🦺.

🖼, 👆 💪 💚 📨 📨 ↩️ 🎻 👫 :

{* ../../docs_src/handling_errors/tutorial004.py hl[3:4,9:11,22] *}

/// note | 📡

👆 💪 ⚙️ from starlette.responses import PlainTextResponse.

FastAPI 🚚 🎏 starlette.responses fastapi.responses 🏪 👆, 👩‍💻. 🌅 💪 📨 👟 🔗 ➡️ 💃.

///

⚙️ RequestValidationError 💪

RequestValidationError 🔌 body 📨 ⏮️ 💽.

👆 💪 ⚙️ 🛠️ 👆 📱 🕹 💪 & ℹ ️, 📨 👩‍💻, ️.

{* ../../docs_src/handling_errors/tutorial005.py hl[14] *}

🔜 🔄 📨 🏬 💖:

{
  "title": "towel",
  "size": "XL"
}

👆 🔜 📨 📨 💬 👆 👈 💽 📨 💪:

{
  "detail": [
    {
      "loc": [
        "body",
        "size"
      ],
      "msg": "value is not a valid integer",
      "type": "type_error.integer"
    }
  ],
  "body": {
    "title": "towel",
    "size": "XL"
  }
}

FastAPI HTTPException 🆚 💃 HTTPException

FastAPI ✔️ 🚮 👍 HTTPException.

& FastAPI'Ⓜ HTTPException 🎓 😖 ➡️ 💃 HTTPException 🎓.

🕴 🔺, 👈 FastAPI'Ⓜ HTTPException👆 🚮 🎚 🔌 📨.

👉 💪/⚙️ 🔘2️⃣.0️⃣ & 💂‍♂ 🚙.

, 👆 💪 🚧 🙋‍♀ FastAPI'Ⓜ HTTPException 🛎 👆 📟.

🕐 👆 ® ⚠ 🐕‍🦺, 👆 🔜 ® 💃 HTTPException.

👉 🌌, 🚥 🙆 🍕 💃 🔗 📟, ⚖️ 💃⚖️ 🔌 -, 🤚 💃 HTTPException, 👆 🐕‍🦺 🔜 💪 & 🍵 ️.

👉 🖼, 💪 ✔️ 👯‍♂️ HTTPException🎏 📟, 💃📁 StarletteHTTPException:

from starlette.exceptions import HTTPException as StarletteHTTPException

🏤-⚙️ FastAPI'Ⓜ ⚠ 🐕‍🦺

🚥 👆 💚 ⚙️⤴️ ⏮️ 🎏 🔢🐕‍🦺 ➡️ FastAPI, 👆 💪 🗄 & 🏤-⚙️ 🔢🐕‍🦺 ➡️ fastapi.exception_handlers:

{* ../../docs_src/handling_errors/tutorial006.py hl[2:5,15,21] *}

👉 🖼 👆 print😅 ⏮️ 📶 🎨 📧, 👆 🤚 💭. 👆 💪 ⚙️ ⚠ & ⤴️ 🏤-⚙️ 🔢🐕‍🦺.