Skip to content

Commit c1e6402

Browse files
committed
Make example error messages more realistic
1 parent 9c08a00 commit c1e6402

File tree

7 files changed

+12
-12
lines changed

7 files changed

+12
-12
lines changed

src/errors/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl ErrorType {
519519
Self::BytesType {..} => "Input should be a valid bytes",
520520
Self::BytesTooShort {..} => "Data should have at least {min_length} byte{expected_plural}",
521521
Self::BytesTooLong {..} => "Data should have at most {max_length} byte{expected_plural}",
522-
Self::BytesInvalidEncoding { .. } => "Data should be valid {encoding}, {encoding_error}",
522+
Self::BytesInvalidEncoding { .. } => "Data should be valid {encoding}: {encoding_error}",
523523
Self::ValueError {..} => "Value error, {error}",
524524
Self::AssertionError {..} => "Assertion failed, {error}",
525525
Self::CustomError {..} => "", // custom errors are handled separately

src/input/input_json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use strum::EnumMessage;
99

1010
use crate::errors::{ErrorType, ErrorTypeDefaults, InputValue, LocItem, ValError, ValResult};
1111
use crate::lookup_key::{LookupKey, LookupPath};
12-
use crate::validators::ValBytesMode;
1312
use crate::validators::decimal::create_decimal;
13+
use crate::validators::ValBytesMode;
1414

1515
use super::datetime::{
1616
bytes_as_date, bytes_as_datetime, bytes_as_time, bytes_as_timedelta, float_as_datetime, float_as_duration,

src/input/input_python.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use speedate::MicrosecondsPrecisionOverflowBehavior;
1515

1616
use crate::errors::{ErrorType, ErrorTypeDefaults, InputValue, LocItem, ValError, ValResult};
1717
use crate::tools::{extract_i64, safe_repr};
18-
use crate::validators::ValBytesMode;
1918
use crate::validators::decimal::{create_decimal, get_decimal_type};
2019
use crate::validators::Exactness;
20+
use crate::validators::ValBytesMode;
2121
use crate::ArgsKwargs;
2222

2323
use super::datetime::{

src/input/input_string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::errors::{ErrorTypeDefaults, InputValue, LocItem, ValError, ValResult}
77
use crate::input::py_string_str;
88
use crate::lookup_key::{LookupKey, LookupPath};
99
use crate::tools::safe_repr;
10-
use crate::validators::ValBytesMode;
1110
use crate::validators::decimal::create_decimal;
11+
use crate::validators::ValBytesMode;
1212

1313
use super::datetime::{
1414
bytes_as_date, bytes_as_datetime, bytes_as_time, bytes_as_timedelta, EitherDate, EitherDateTime, EitherTime,

src/validators/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ use pyo3::types::{PyAny, PyDict, PyString, PyTuple, PyType};
1010
use pyo3::{intern, PyTraverseError, PyVisit};
1111

1212
use crate::build_tools::{py_schema_err, py_schema_error_type, SchemaError};
13-
pub(crate) use config::ValBytesMode;
1413
use crate::definitions::{Definitions, DefinitionsBuilder};
1514
use crate::errors::{LocItem, ValError, ValResult, ValidationError};
1615
use crate::input::{Input, InputType, StringMapping};
1716
use crate::py_gc::PyGcTraverse;
1817
use crate::recursion_guard::RecursionState;
1918
use crate::tools::SchemaDict;
19+
pub(crate) use config::ValBytesMode;
2020

2121
mod any;
2222
mod arguments;

tests/test_errors.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,13 @@ def f(input_value, info):
322322
('bytes_too_long', 'Data should have at most 1 byte', {'max_length': 1}),
323323
(
324324
'bytes_invalid_encoding',
325-
'Data should be valid base64, base64 error',
326-
{'encoding': 'base64', 'encoding_error': 'base64 error'},
325+
'Data should be valid base64: Invalid byte 1, offset 1',
326+
{'encoding': 'base64', 'encoding_error': 'Invalid byte 1, offset 1'},
327327
),
328328
(
329329
'bytes_invalid_encoding',
330-
'Data should be valid hex, hex error',
331-
{'encoding': 'hex', 'encoding_error': 'hex error'},
330+
'Data should be valid hex: Odd number of digits',
331+
{'encoding': 'hex', 'encoding_error': 'Odd number of digits'},
332332
),
333333
('value_error', 'Value error, foobar', {'error': ValueError('foobar')}),
334334
('assertion_error', 'Assertion failed, foobar', {'error': AssertionError('foobar')}),

tests/test_json.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def test_json_bytes_base64_invalid():
403403
{
404404
'type': 'bytes_invalid_encoding',
405405
'loc': (),
406-
'msg': f'Data should be valid base64, Invalid byte {ord("!")}, offset {len(wrong_input)-1}.',
406+
'msg': f'Data should be valid base64: Invalid byte {ord("!")}, offset {len(wrong_input)-1}.',
407407
'input': wrong_input,
408408
}
409409
]
@@ -435,7 +435,7 @@ def test_json_bytes_hex_invalid():
435435
{
436436
'type': 'bytes_invalid_encoding',
437437
'loc': (),
438-
'msg': 'Data should be valid hex, Odd number of digits',
438+
'msg': 'Data should be valid hex: Odd number of digits',
439439
'input': wrong_input,
440440
}
441441
]
@@ -447,7 +447,7 @@ def test_json_bytes_hex_invalid():
447447
{
448448
'type': 'bytes_invalid_encoding',
449449
'loc': (),
450-
'msg': "Data should be valid hex, Invalid character 'g' at position 1",
450+
'msg': "Data should be valid hex: Invalid character 'g' at position 1",
451451
'input': wrong_input,
452452
}
453453
]

0 commit comments

Comments
 (0)