Python - pydantic(3)错误处理
2021-09-15 20:18
761 查看
常见触发错误的情况
- 如果传入的字段多了会自动过滤
- 如果传入的少了会报错,必填字段
- 如果传入的字段名称对不上也会报错
- 如果传入的类型不对会自动转换,如果不能转换则会报错
错误的触发
pydantic 会在它正在验证的数据中发现错误时引发 ValidationError
注意
- 验证代码不应该抛出 ValidationError 本身
- 而是应该抛出 ValueError、TypeError、AssertionError 或他们的子类
- ValidationError 会包含所有错误及其发生方式的信息
访问错误的方式
- e.errors():返回输入数据中发现的错误的列表
- e.json():以 JSON 格式返回错误(推荐)
- str(e):以人类可读的方式返回错误
简单栗子
# 一定要导入 ValidationError from pydantic import BaseModel, ValidationError class Person(BaseModel): id: int name: str try: # id是个int类型,如果不是int或者不能转换int会报错 p = Person(id="ss", name="hallen") except ValidationError as e: # 打印异常消息 print(e.errors())
e.errors() 的输出结果
[{'loc': ('id',), 'msg': 'value is not a valid integer', 'type': 'type_error.integer'}]
e.json() 的输出结果
[ { "loc": [ "id" ], "msg": "value is not a valid integer", "type": "type_error.integer" } ]
str(e) 的输出结果
1 validation error for Person id value is not a valid integer (type=type_error.integer)
复杂栗子
class Location(BaseModel): lat = 0.1 lng = 10.1 class Model(BaseModel): is_required: float gt_int: conint(gt=42) list_of_ints: List[int] = None a_float: float = None recursive_model: Location = None data = dict( list_of_ints=['1', 2, 'bad'], a_float='not a float', recursive_model={'lat': 4.2, 'lng': 'New York'}, gt_int=21 ) try: Model(**data) except ValidationError as e: print(e.json(indent=4))
输出结果
[ { "loc": [ "is_required" ], "msg": "field required", "type": "value_error.missing" }, { "loc": [ "gt_int" ], "msg": "ensure this value is greater than 42", "type": "value_error.number.not_gt", "ctx": { "limit_value": 42 } }, { "loc": [ "list_of_ints", 2 ], "msg": "value is not a valid integer", "type": "type_error.integer" }, { "loc": [ "a_float" ], "msg": "value is not a valid float", "type": "type_error.float" }, { "loc": [ "recursive_model", "lng" ], "msg": "value is not a valid float", "type": "type_error.float" } ]
- value_error.missing:必传字段缺失
- value_error.number.not_gt:字段值没有大于 42
- type_error.integer:字段类型错误,不是 integer
自定义错误
# 导入 validator from pydantic import BaseModel, ValidationError, validator class Model(BaseModel): foo: str # 验证器 @validator('foo') def name_must_contain_space(cls, v): if v != 'bar': # 自定义错误信息 raise ValueError('value must be bar') # 返回传进来的值 return v try: Model(foo="ber") except ValidationError as e: print(e.json())
输出结果
[ { "loc": [ "foo" ], "msg": "value must be bar", "type": "value_error" } ]
自定义错误模板类
from pydantic import BaseModel, PydanticValueError, ValidationError, validator class NotABarError(PydanticValueError): code = 'not_a_bar' msg_template = 'value is not "bar", got "{wrong_value}"' class Model(BaseModel): foo: str @validator('foo') def name_must_contain_space(cls, v): if v != 'bar': raise NotABarError(wrong_value=v) return v try: Model(foo='ber') except ValidationError as e: print(e.json())
输出结果
[ { "loc": [ "foo" ], "msg": "value is not \"bar\", got \"ber\"", "type": "value_error.not_a_bar", "ctx": { "wrong_value": "ber" } } ]
PydanticValueError
自定义错误类需要继承这个或者 PydanticTypeError
相关文章推荐
- Python3 基础:错误处理
- python错误处理
- python简单错误处理
- Python基础(十)------错误异常处理及测试
- python 错误异常处理
- 【随笔记录】python 处理中文时出现的错误'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)" 解决方法
- python的错误处理——try语句
- Python中遇到"UnicodeDecodeError类的编码或解码的错误时如何处理
- Python使用pip install psycopg2安装psycopg2包出现python setup.py egg_info failed with error code 1 in /tmp/pip-build-YtLeN3/psycopg2错误处理
- Python打开包含中文字符的url时返回错误的处理
- Python 6.1 错误处理
- python3错误处理“sre_constants.error: nothing to repeat”
- Python成长之路(四)——循环设计、循环对象、函数对象、错误处理、动态类型
- Python网络编程——处理套接字错误
- Python编码错误处理
- Python学习打卡--day09(错误处理)
- Python进阶08 错误处理
- python 错误处理
- python 处理中文时出现的错误'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)" 解决方法
- python错误处理记录完整的异常堆栈信息