Skip to content

Commit 8c1f7fc

Browse files
committed
Improved error messages
1 parent fa71d9e commit 8c1f7fc

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

epserde/src/deser/mod.rs

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ pub fn check_header<T: SerInner<SerType: TypeHash + AlignHash>>(
400400
backend: &mut impl ReadWithPos,
401401
) -> Result<()> {
402402
let self_type_name = core::any::type_name::<T>().to_string();
403+
let self_ser_type_name = core::any::type_name::<T::SerType>().to_string();
403404
let mut type_hasher = xxhash_rust::xxh3::Xxh3::new();
404405
T::SerType::type_hash(&mut type_hasher);
405406
let self_type_hash = type_hasher.finish();
@@ -438,18 +439,20 @@ pub fn check_header<T: SerInner<SerType: TypeHash + AlignHash>>(
438439

439440
if ser_type_hash != self_type_hash {
440441
return Err(Error::WrongTypeHash {
441-
self_type_name,
442442
ser_type_name,
443-
self_type_hash,
444443
ser_type_hash,
444+
self_type_name,
445+
self_ser_type_name,
446+
self_type_hash,
445447
});
446448
}
447449
if ser_align_hash != self_align_hash {
448450
return Err(Error::WrongAlignHash {
449-
self_type_name,
450451
ser_type_name,
451-
self_align_hash,
452452
ser_align_hash,
453+
self_type_name,
454+
self_ser_type_name,
455+
self_align_hash,
453456
});
454457
}
455458

@@ -521,13 +524,21 @@ pub enum Error {
521524
/// A tag is wrong (e.g., for [`Option`]).
522525
InvalidTag(usize),
523526
#[error(
524-
r#"Wrong type hash: actual = 0x{ser_type_hash:016x}, expected = 0x{self_type_hash:016x}.
525-
You are trying to deserialize a file with the wrong type. You might also be trying to deserialize
526-
a tuple of mixed zero-copy types, which is no longer supported since 0.8.0,
527-
an instance containing tuples, whose type hash was fixed in 0.9.0,
528-
or an instance containing a vector or a string that was serialized before 0.10.0.
529-
The serialized type is '{ser_type_name}',
530-
but the deserializable type on which the deserialization method was invoked is '{self_type_name}'."#
527+
r#"Wrong type hash
528+
Actual: 0x{ser_type_hash:016x}; expected: 0x{self_type_hash:016x}.
529+
530+
The serialized type is
531+
'{ser_type_name}',
532+
but the deserializable type on which the deserialization method was invoked is
533+
'{self_type_name}',
534+
which has serialization type
535+
{self_ser_type_name}.
536+
537+
You are trying to deserialize a file with the wrong type. You might also be
538+
trying to deserialize a tuple of mixed zero-copy types, which is no longer
539+
supported since 0.8.0, an instance containing tuples, whose type hash was fixed
540+
in 0.9.0, or an instance containing a vector or a string that was serialized
541+
before 0.10.0."#
531542
)]
532543
/// The type hash is wrong. Probably the user is trying to deserialize a
533544
/// file with the wrong type.
@@ -538,19 +549,28 @@ but the deserializable type on which the deserialization method was invoked is '
538549
ser_type_hash: u64,
539550
// The name of the type on which the deserialization method was called.
540551
self_type_name: String,
552+
// The name of the serialization type of `self_type_name`.
553+
self_ser_type_name: String,
541554
// The [`TypeHash`] of the type on which the deserialization method was called.
542555
self_type_hash: u64,
543556
},
544557
#[error(
545-
r#"Wrong alignment hash: actual = 0x{ser_align_hash:016x}, expected = 0x{self_align_hash:016x}.
546-
You might be trying to deserialize a file that was serialized on an architecture
547-
with different alignment requirements, or some of the fields of the type
548-
might have changed their copy type (zero or deep). You might also be trying to deserialize
549-
an array, whose alignment hash has been fixed in 0.8.0. It is also
550-
possible that you are trying to deserialize a file serialized before version 0.10.0
551-
in which repr attributes were not sorted lexicographically.
552-
The serialized type is '{ser_type_name}', but the deserializable type on which the the deserialization
553-
method was invoked is '{self_type_name}'."#
558+
r#"Wrong alignment hash
559+
Actual: 0x{ser_align_hash:016x}; expected: 0x{self_align_hash:016x}.
560+
561+
The serialized type is
562+
'{ser_type_name}',
563+
but the deserializable type on which the deserialization method was invoked is
564+
'{self_type_name}',
565+
which has serialization type
566+
{self_ser_type_name}.
567+
568+
You might be trying to deserialize a file that was serialized on an
569+
architecture with different alignment requirements, or some of the fields of
570+
the type might have changed their copy type (zero or deep). You might also be
571+
trying to deserialize an array, whose alignment hash has been fixed in 0.8.0.
572+
It is also possible that you are trying to deserialize a file serialized before
573+
version 0.10.0 in which repr attributes were not sorted lexicographically."#
554574
)]
555575
/// The type representation hash is wrong. Probably the user is trying to
556576
/// deserialize a file with some zero-copy type that has different
@@ -564,6 +584,8 @@ method was invoked is '{self_type_name}'."#
564584
ser_align_hash: u64,
565585
// The name of the type on which the deserialization method was called.
566586
self_type_name: String,
587+
// The name of the serialization type of `self_type_name`.
588+
self_ser_type_name: String,
567589
// The [`AlignHash`] of the type on which the deserialization method was called.
568590
self_align_hash: u64,
569591
},

epserde/src/ser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ pub fn write_header<S: TypeHash + AlignHash>(backend: &mut impl WriteWithNames)
206206

207207
backend.write("TYPE_HASH", &type_hasher.finish())?;
208208
backend.write("REPR_HASH", &align_hasher.finish())?;
209-
backend.write("TYPE_NAME", &core::any::type_name::<S>().to_string())
209+
backend.write("TYPE_NAME", &core::any::type_name::<S>())
210210
}
211211

212212
/// A helper trait that makes it possible to implement differently serialization

0 commit comments

Comments
 (0)