Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions provider/core/src/buf/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ where

#[cfg(feature = "deserialize_json")]
impl From<serde_json::error::Error> for DataError {
#[track_caller]
fn from(e: serde_json::error::Error) -> Self {
DataErrorKind::Deserialize
.with_str_context("serde_json")
Expand All @@ -241,6 +242,7 @@ impl From<serde_json::error::Error> for DataError {

#[cfg(feature = "deserialize_bincode_1")]
impl From<bincode::Error> for DataError {
#[track_caller]
fn from(e: bincode::Error) -> Self {
DataErrorKind::Deserialize
.with_str_context("bincode")
Expand All @@ -250,6 +252,7 @@ impl From<bincode::Error> for DataError {

#[cfg(feature = "deserialize_postcard_1")]
impl From<postcard::Error> for DataError {
#[track_caller]
fn from(e: postcard::Error) -> Self {
DataErrorKind::Deserialize
.with_str_context("postcard")
Expand Down
18 changes: 17 additions & 1 deletion provider/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,14 @@ pub struct DataError {

/// Whether this error was created in silent mode to not log.
pub silent: bool,

/// The source location where the error was created.
pub location: core::panic::Location<'static>,
}

impl fmt::Display for DataError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ICU4X data error")?;
write!(f, "ICU4X data error at {}", self.location)?;
if self.kind != DataErrorKind::Custom {
write!(f, ": {}", self.kind)?;
}
Expand All @@ -110,35 +113,41 @@ impl DataErrorKind {
///
/// If possible, you should attach context using a `with_` function.
#[inline]
#[track_caller]
pub const fn into_error(self) -> DataError {
DataError {
kind: self,
marker: None,
str_context: None,
silent: false,
location: *core::panic::Location::caller(),
}
}

/// Creates a [`DataError`] with a data marker context.
#[inline]
#[track_caller]
pub const fn with_marker(self, marker: DataMarkerInfo) -> DataError {
self.into_error().with_marker(marker)
}

/// Creates a [`DataError`] with a string context.
#[inline]
#[track_caller]
pub const fn with_str_context(self, context: &'static str) -> DataError {
self.into_error().with_str_context(context)
}

/// Creates a [`DataError`] with a type name context.
#[inline]
#[track_caller]
pub fn with_type_context<T>(self) -> DataError {
self.into_error().with_type_context::<T>()
}

/// Creates a [`DataError`] with a request context.
#[inline]
#[track_caller]
pub fn with_req(self, marker: DataMarkerInfo, req: DataRequest) -> DataError {
self.into_error().with_req(marker, req)
}
Expand All @@ -147,12 +156,14 @@ impl DataErrorKind {
impl DataError {
/// Returns a new, empty [`DataError`] with kind Custom and a string error message.
#[inline]
#[track_caller]
pub const fn custom(str_context: &'static str) -> Self {
Self {
kind: DataErrorKind::Custom,
marker: None,
str_context: Some(str_context),
silent: false,
location: *core::panic::Location::caller(),
}
}

Expand All @@ -164,6 +175,7 @@ impl DataError {
marker: Some(marker.id),
str_context: self.str_context,
silent: self.silent,
location: self.location,
}
}

Expand All @@ -175,6 +187,7 @@ impl DataError {
marker: self.marker,
str_context: Some(context),
silent: self.silent,
location: self.location,
}
}

Expand Down Expand Up @@ -241,12 +254,14 @@ impl DataError {
}

#[inline]
#[track_caller]
pub(crate) fn for_type<T>() -> DataError {
DataError {
kind: DataErrorKind::Downcast(core::any::type_name::<T>()),
marker: None,
str_context: None,
silent: false,
location: *core::panic::Location::caller(),
}
}
}
Expand All @@ -255,6 +270,7 @@ impl core::error::Error for DataError {}

#[cfg(feature = "std")]
impl From<std::io::Error> for DataError {
#[track_caller]
fn from(e: std::io::Error) -> Self {
log::warn!("I/O error: {e}");
DataErrorKind::Io(e.kind()).into_error()
Expand Down
Loading