而是提高回报
有人可以帮助我理解为什么不提出httpexception
当状态为200时返回
时?
与FastApi一起工作
一个代码为例:
@app.delete("/delete")
def delete(id = Query(...,description="Delete ID to be deleted")):
if id not in dictionary:
raise HTTPException(status_code=404,detail="Delete Id doesn't exists.")
del dictionary[id]
return {"Success":"Delete deleted!"}
我想理解为什么不将其用作示例:
raise HTTPException(status_code=200,detail="Delete deleted!")
这是使用它的正确方法吗?
Can anybody help me to understand why not raise an HTTPException
when the status is 200 instead a return
?
working with fastApi
A code as example:
@app.delete("/delete")
def delete(id = Query(...,description="Delete ID to be deleted")):
if id not in dictionary:
raise HTTPException(status_code=404,detail="Delete Id doesn't exists.")
del dictionary[id]
return {"Success":"Delete deleted!"}
I want to understand why not to use as example:
raise HTTPException(status_code=200,detail="Delete deleted!")
Is this a correct way to use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先是因为语义是:一个例外是一种语言构造,其意思是除了返回函数的结果。它打破了FastAPI应用程序的正常流动,我猜它会/可能会打破大多数中间件处理(例如CORS标头),因为突然发生了例外。
其次:因为您可能希望返回其他内容,而不是仅在
详细信息
键下的信息。它将无法使用response_model
内置的FastAPI机制,并允许您声明性地调整和验证每种请求的响应模型(即通过配置视图装饰器配置)。First of all because of the semantics: an exception is a language construct that means something else than returning the result of a function. It breaks the normal flow of the FastAPI application, and I'm guessing it will/could break most middleware handling (such as CORS headers) because suddenly an exception has occurred instead.
Secondly: Because you probably want to return something else than just information under a
detail
key. It won't be able to use theresponse_model
mechanism that's built-in to FastAPI and allows you to tweak and validate the response model for each type of request declaratively (i.e. by configuring the view decorator).