网站这么绑定到域名wordpress幻灯片 设置
news/
2025/9/24 0:20:23/
文章来源:
网站这么绑定到域名,wordpress幻灯片 设置,开源app开发工具,如何提交网站给百度FASTAPI系列 20-异常处理器exception_handler 文章目录 FASTAPI系列 20-异常处理器exception_handler前言一、HTTPException 异常#xff1f;二、覆盖默认的HTTPException 异常三、覆盖请求验证异常RequestValidationError 源码分析 总结更多内容#xff0c;请关注公众号 前言…FASTAPI系列 20-异常处理器exception_handler 文章目录 FASTAPI系列 20-异常处理器exception_handler前言一、HTTPException 异常二、覆盖默认的HTTPException 异常三、覆盖请求验证异常RequestValidationError 源码分析 总结更多内容请关注公众号 前言
通常我们可以通过 raise 抛出一个 HTTPException 异常请求参数不合法会抛出RequestValidationError 异常这是最常见的2种异常。 一、HTTPException 异常
向客户端返回 HTTP 错误响应可以使用 raise 触发 HTTPException。
from fastapi import FastAPI, HTTPExceptionapp FastAPI()app.get(/path/{name})
async def get_name(name: str): if name ! Teacher Li: raise HTTPException(404, detailfname: {name} not found) return {name: name}
二、覆盖默认的HTTPException 异常
查看HTTPException 异常相关源码
from starlette.exceptions import HTTPException as StarletteHTTPException class HTTPException(StarletteHTTPException): def __init__( self, status_code: int, detail: Any None, headers: Optional[Dict[str, Any]] None, ) - None: super().__init__(status_codestatus_code, detaildetail, headersheaders)
HTTPException 异常是继承的 starlette 包里面的 HTTPException覆盖默认异常处理器时需要导入 from starlette.exceptions import HTTPException as StarletteHTTPException并用 app.excption_handler(StarletteHTTPException) 装饰异常处理器。
from fastapi import FastAPI, Request
from fastapi.exceptions import HTTPException
from fastapi.responses import PlainTextResponse, JSONResponse
from starlette.exceptions import HTTPException as StarletteHTTPException app FastAPI() # # 捕获 HTTPException 异常
app.exception_handler(StarletteHTTPException)
def http_error(request, exc): print(exc.status_code) print(exc.detail) # return JSONResponse({error_msg: exc.detail}, status_codeexc.status_code) return PlainTextResponse(contentexc.detail, status_codeexc.status_code) app.get(/path/{name})
async def get_name(name: str): if name ! Teacher Li: raise HTTPException(404, detailfname: {name} not found) return {name: name}
三、覆盖请求验证异常
请求中包含无效数据时FastAPI 内部会触发 RequestValidationError。该异常也内置了默认异常处理器。覆盖默认异常处理器时需要导入 RequestValidationError并用 app.excption_handler(RequestValidationError) 装饰异常处理器。这样异常处理器就可以接收 Request 与异常。
from fastapi import FastAPI, HTTPException
from fastapi.exceptions import RequestValidationError
from fastapi.responses import PlainTextResponse
from starlette.exceptions import HTTPException as StarletteHTTPExceptionapp FastAPI()app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):return PlainTextResponse(str(exc), status_code400)app.get(/user/{user_id})
async def get_user(user_id: int):if user_id 3:raise HTTPException(status_code418, detailNope! I dont like 3.)return {user_id: user_id}
RequestValidationError 源码分析
RequestValidationError 相关源码
class RequestValidationError(ValidationError): def __init__(self, errors: Sequence[ErrorList], *, body: Any None) - None: self.body body super().__init__(errors, RequestErrorModel)使用示例
from fastapi import FastAPI, Request, status
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from pydantic import BaseModelapp FastAPI()app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError): print(exc.json()) print(exc.errors()) print(exc.body) # 请求body return JSONResponse( status_code400, contentjsonable_encoder({detail: exc.errors(), body: exc.body}), ) class Book(BaseModel): name: str price: floatapp.post(/books/)
async def create_book(book: Book): return book总结
FastAPI 调用的就是 RequestValidationError 类因此如果在 response_model 中使用 Pydantic 模型且数据有错误时在日志中就会看到这个错误。但客户端或用户看不到这个错误。反之客户端接收到的是 HTTP 状态码为 500 的「内部服务器错误」。这是因为在_响应_或代码不是在客户端的请求里中出现的 Pydantic ValidationError 是代码的 bug。修复错误时客户端或用户不能访问错误的内部信息否则会造成安全隐患。
更多内容请关注公众号
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/914341.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!