-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathvalue_exception.rs
189 lines (159 loc) · 5.28 KB
/
value_exception.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use pyo3::exceptions::{PyException, PyValueError};
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyString};
use crate::input::InputType;
use crate::tools::extract_i64;
use super::line_error::ToErrorValue;
use super::{ErrorType, ValError};
#[pyclass(extends=PyException, module="pydantic_core._pydantic_core")]
#[derive(Debug, Clone)]
pub struct PydanticOmit {}
impl PydanticOmit {
pub(crate) fn new_err() -> PyErr {
PyErr::new::<Self, ()>(())
}
}
#[pymethods]
impl PydanticOmit {
#[new]
pub fn py_new() -> Self {
Self {}
}
fn __str__(&self) -> &'static str {
self.__repr__()
}
fn __repr__(&self) -> &'static str {
"PydanticOmit()"
}
}
#[pyclass(extends=PyException, module="pydantic_core._pydantic_core")]
#[derive(Debug, Clone)]
pub struct PydanticUseDefault {}
#[pymethods]
impl PydanticUseDefault {
#[new]
pub fn py_new() -> Self {
Self {}
}
fn __str__(&self) -> &'static str {
self.__repr__()
}
fn __repr__(&self) -> &'static str {
"PydanticUseDefault()"
}
}
#[pyclass(extends=PyValueError, module="pydantic_core._pydantic_core", subclass)]
#[derive(Debug, Clone, Default)]
pub struct PydanticCustomError {
error_type: String,
message_template: String,
context: Option<Py<PyDict>>,
}
#[pymethods]
impl PydanticCustomError {
#[new]
#[pyo3(signature = (error_type, message_template, context = None, /))]
pub fn py_new(error_type: String, message_template: String, context: Option<Bound<'_, PyDict>>) -> Self {
Self {
error_type,
message_template,
context: context.map(Bound::unbind),
}
}
#[getter(r#type)]
pub fn error_type(&self) -> &str {
&self.error_type
}
#[getter]
pub fn message_template(&self) -> &str {
&self.message_template
}
#[getter]
pub fn context(&self, py: Python) -> Option<Py<PyDict>> {
self.context.as_ref().map(|c| c.clone_ref(py))
}
pub fn message(&self, py: Python) -> PyResult<String> {
Self::format_message(&self.message_template, self.context.as_ref().map(|c| c.bind(py)))
}
fn __str__(&self, py: Python) -> PyResult<String> {
self.message(py)
}
fn __repr__(&self, py: Python) -> PyResult<String> {
let msg = self.message(py)?;
match self.context.as_ref() {
Some(ctx) => Ok(format!("{msg} [type={}, context={}]", self.error_type, ctx.bind(py))),
None => Ok(format!("{msg} [type={}, context=None]", self.error_type)),
}
}
}
impl PydanticCustomError {
pub fn into_val_error(self, input: impl ToErrorValue) -> ValError {
let error_type = ErrorType::CustomError {
error_type: self.error_type,
message_template: self.message_template,
context: self.context,
};
ValError::new(error_type, input)
}
pub fn format_message(message_template: &str, context: Option<&Bound<'_, PyDict>>) -> PyResult<String> {
let mut message = message_template.to_string();
if let Some(ctx) = context {
for (key, value) in ctx.iter() {
let key = key.downcast::<PyString>()?;
if let Ok(py_str) = value.downcast::<PyString>() {
message = message.replace(&format!("{{{}}}", key.to_str()?), py_str.to_str()?);
} else if let Some(value_int) = extract_i64(&value) {
message = message.replace(&format!("{{{}}}", key.to_str()?), &value_int.to_string());
} else {
// fallback for anything else just in case
message = message.replace(&format!("{{{}}}", key.to_str()?), &value.to_string());
}
}
}
Ok(message)
}
}
#[pyclass(extends=PyValueError, module="pydantic_core._pydantic_core")]
#[derive(Debug, Clone)]
pub struct PydanticKnownError {
error_type: ErrorType,
}
#[pymethods]
impl PydanticKnownError {
#[new]
#[pyo3(signature = (error_type, context=None, /))]
pub fn py_new(py: Python, error_type: &str, context: Option<Bound<'_, PyDict>>) -> PyResult<Self> {
let error_type = ErrorType::new(py, error_type, context)?;
Ok(Self { error_type })
}
#[getter(r#type)]
pub fn error_type(&self) -> String {
self.error_type.to_string()
}
#[getter]
pub fn message_template(&self) -> &'static str {
self.error_type.message_template_python()
}
#[getter]
pub fn context(&self, py: Python) -> PyResult<Option<Py<PyDict>>> {
self.error_type.py_dict(py)
}
pub fn message(&self, py: Python) -> PyResult<String> {
self.error_type.render_message(py, InputType::Python)
}
fn __str__(&self, py: Python) -> PyResult<String> {
self.message(py)
}
fn __repr__(&self, py: Python) -> PyResult<String> {
let msg = self.message(py)?;
match self.context(py)?.as_ref() {
Some(ctx) => Ok(format!("{msg} [type={}, context={}]", self.error_type(), ctx.bind(py))),
None => Ok(format!("{msg} [type={}, context=None]", self.error_type())),
}
}
}
impl PydanticKnownError {
pub fn into_val_error(self, input: impl ToErrorValue) -> ValError {
ValError::new(self.error_type, input)
}
}