而是提高回报

发布于 2025-01-25 11:17:01 字数 576 浏览 3 评论 0原文

有人可以帮助我理解为什么不提出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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风追烟花雨 2025-02-01 11:17:01

首先是因为语义是:一个例外是一种语言构造,其意思是除了返回函数的结果。它打破了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 the response_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).

救赎№ 2025-02-01 11:17:01
  1. 返回
  2. 很多次,结果应该是平等的
  3. 加薪休息时间,所有下一个中间件(前,中,帖子)
  4. 重定向登录页面中间件

class Middleware(APIRoute):
    def get_route_handler(self) -> Callable:
        original_route_handler = super().get_route_handler()

    async def custom_route_handler(req: Request) -> Response:
        try:
            res: Response = await original_route_handler(req) #original request
        except HTTPException as e: #catch login exception
            if e.status_code == status.HTTP_403_FORBIDDEN:
                return RedirectResponse(url = Const.DEFAULT_PAGE) #redirect to home page
            else:
                raise e #other exception process normally 
            
        return res

    return custom_route_handler

  1. return is better
  2. A lot of times the result should be equal
  3. raise breaks all next middlewares (pre, mid, post)
  4. Here is redirect login page middleware

class Middleware(APIRoute):
    def get_route_handler(self) -> Callable:
        original_route_handler = super().get_route_handler()

    async def custom_route_handler(req: Request) -> Response:
        try:
            res: Response = await original_route_handler(req) #original request
        except HTTPException as e: #catch login exception
            if e.status_code == status.HTTP_403_FORBIDDEN:
                return RedirectResponse(url = Const.DEFAULT_PAGE) #redirect to home page
            else:
                raise e #other exception process normally 
            
        return res

    return custom_route_handler

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文