温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么处理程序错误

发布时间:2021-10-15 14:09:35 来源:亿速云 阅读:146 作者:iii 栏目:编程语言

本篇内容主要讲解“怎么处理程序错误”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么处理程序错误”吧!

1

程序错误类型

1.1

语法错误

语法错误是因为源程序中不正确的代码产生的,即在编写程序时没有遵守语法(或词法)规则,书写了错误的语法代码,从而导致编译器无法正确解释源代码而产生的错误,通常是由于录入的错误引起的,它在词法分析或语法分析时检测出来。如“非法字符”、“括号不匹配”、“缺少;”之类的错误。

1.2

语义错误

语义错误是指源程序中不符合语义规则的错误,即一条语句试图执行一条不可能执行的操作而产生的错误。语义错误有的在语义分析时检测处来,有的在运行时才能检测出来。如变量声明错误、作用域错误、数据存储区的溢出等错误。

1.3

逻辑错误

逻辑错误是指程序的运行结果和程序员的设想有出入时产生的错误。这类错误并不直接导致程序在编译期间和运行期间出现错误,但是程序未按预期方式执行,产生了不正确的运行结果,较难发现。这种错误只能通过分析结果,将结果与设计方案进行对比来发现。

2

HTTPException

我们用 HTTPException 模块返回带错误信息的 Response。HTTPException 是一个普通的 Python 异常,同时带有与 API 访问有关的附加数据。

from fastapi import FastAPI, HTTPException
app = FastAPI()
items = {"book": "Learn Python"}

@app.get("/items/{item_id}")async def read_item(item_id: str):    if item_id not in items:
       raise HTTPException(status_code=404, detail="Item not found")
   return {"item": items[item_id]}

3

添加自定义头信息

有时候针对 HTTP 错误,在一些场景下,我们需要添加自定义头信息

from fastapi import FastAPI, HTTPException
app = FastAPI()
items = {"book": "Learn Python"}

@app.get("/items-header/{item_id}")async def read_item_header(item_id: str):    if item_id not in items:        raise HTTPException(            status_code=404,            detail="Item not found",            headers={"X-Error": "error info"},
       )    return {"item": items[item_id]}

4

自定义异常处理器

在 fastapi 中借助 the same exception utilities from Starlette,我们可以添加自定义异常处理器。假设我们有个自定义异常 UnicornException,我们想在全局范围内处理这个异常。借助 @app.exception_handler(),就可以实现我们的目标。


from fastapi import FastAPI, Requestfrom fastapi.responses import JSONResponse
class UnicornException(Exception):    def __init__(self, name: str):        self.name = name
app = FastAPI()
@app.exception_handler(UnicornException)async def unicorn_exception_handler(request: Request, exc: UnicornException):
   return JSONResponse(        status_code=418,        content={"message": f"Oops! {exc.name} did something. error"},    )
@app.get("/get_name_info/{name}")async def read_unicorn(name: str):    if name == "haishiniu":        raise UnicornException(name=name)
   return {"name": name}

5

重写缺省异常处理器

fastapi 有一些缺省的异常处理器。当我们抛出 HTTPException 异常或者当请求有非法数据的时候,这些处理器负责返回默认的 JSON 结果。我们可以重写这些异常处理器。

5.1

重写请求校验异常处理器

当一个请求包含非法数据的时候,fastapi 内部会抛出 RequestValidationError 异常,并且有默认的异常处理器来处理。我们可以用 @app.exception_handler(RequestValidationError) 来重写这个异常处理器。


from fastapi import FastAPI, HTTPExceptionfrom fastapi.exceptions import RequestValidationErrorfrom fastapi.responses import PlainTextResponsefrom starlette.exceptions import HTTPException as StarletteHTTPException
app = FastAPI()
@app.exception_handler(StarletteHTTPException)async def http_exception_handler(request, exc):    return PlainTextResponse(str(exc.detail), status_code=exc.status_code)

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):    return PlainTextResponse(str(exc), status_code=400)

@app.get("/items/{item_id}")async def read_item(item_id: int):    if item_id == 3:        raise HTTPException(status_code=418, detail="Nope! I don't like 3.")    return {"item_id": item_id}

5.2

重写 HTTPException 异常处理器

同样的方法,我们可以重写 HTTPException 异常处理器。例如,你可能想返回纯文本格式而不是 JSON 格式的错误信息。

from fastapi import FastAPI, HTTPExceptionfrom fastapi.exceptions import RequestValidationErrorfrom fastapi.responses import PlainTextResponsefrom starlette.exceptions import HTTPException as StarletteHTTPException
app = FastAPI()
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request, exc):
   return PlainTextResponse(str(exc.detail), status_code=exc.status_code)
@app.exception_handler(RequestValidationError)async def validation_exception_handler(request, exc):    return PlainTextResponse(str(exc), status_code=400)

@app.get("/items/{item_id}")async def read_item(item_id: int):    if item_id == 3:
       raise HTTPException(status_code=418, detail="Nope! I don't like 3.")
   return {"item_id": item_id}

5.3

重用缺省异常处理器

我们可以导入并且重用缺省的异常处理器。我们从 fastapi.exception_handlers 导入缺省异常处理器。

from fastapi import FastAPI, HTTPExceptionfrom fastapi.exception_handlers import (    http_exception_handler,    request_validation_exception_handler,)
from fastapi.exceptions import RequestValidationErrorfrom starlette.exceptions import HTTPException as StarletteHTTPException
app = FastAPI()
@app.exception_handler(StarletteHTTPException)async def custom_http_exception_handler(request, exc):    print(f"OMG! An HTTP error!: {repr(exc)}")    return await http_exception_handler(request, exc)

@app.exception_handler(RequestValidationError)async def validation_exception_handler(request, exc):    print(f"OMG! The client sent invalid data!: {exc}")    return await request_validation_exception_handler(request, exc)


@app.get("/items/{item_id}")async def read_item(item_id: int):    if item_id == 3:        raise HTTPException(status_code=418, detail="Nope! I don't like 3.")    return {"item_id": item_id}

在示例中,我们在抛出异常之前添加了一条日志输出。我们可以根据业务需求灵活的重用缺省异常处理器。

6

fastapi HTTPException 对比 Starlette HTTPException

fastapi 中 HTTPException 继承自 Starlette 的 HTTPException。
唯一的区别 fastapi 中 HTTPException 允许你在 response 添加头信息。主要在内部用于 OAuth 2.0 以及一些安全相关的功能。
因此,通常我们在代码中抛出 fastapi 的 HTTPException 异常。但是,当我们注册异常处理器的时候,我们应该注册为 Starlette 的 HTTPException。这样,当 Starlette 的内部代码或者 Starlette 扩展插件抛出 Starlette HTTPException 时,我们的处理器才能正常捕获和处理这个异常。如果我们要在代码中同时使用这两个类,为了避免命名冲突,我们可以重命名其中一个类。

到此,相信大家对“怎么处理程序错误”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI