Skip to main content

Error

Enum Error 

Source
#[non_exhaustive]
pub enum Error {
Show 13 variants TypeMismatch(Cow<'static, str>), BufferOutOfBound(usize, usize, usize), EncodeError(Cow<'static, str>), InvalidData(Cow<'static, str>), InvalidRef(Cow<'static, str>), UnknownEnum(Cow<'static, str>), TypeError(Cow<'static, str>), EncodingError(Cow<'static, str>), DepthExceed(Cow<'static, str>), Unsupported(Cow<'static, str>), NotAllowed(Cow<'static, str>), Unknown(Cow<'static, str>), StructVersionMismatch(Cow<'static, str>),
}
Expand description

Error type for Fory serialization and deserialization operations.

§IMPORTANT: Always Use Static Constructor Functions

DO NOT construct error variants directly using the enum syntax. ALWAYS use the provided static constructor functions instead.

§Why Use Static Functions?

The static constructor functions provide:

  • Automatic type conversion via Into<Cow<'static, str>>
  • Consistent error creation across the codebase
  • Better ergonomics (no need for manual .into() calls)
  • Future-proof API if error construction logic needs to change

§Examples

use fory_core::error::Error;

// ✅ CORRECT: Use static functions
let err = Error::type_error("Expected string type");
let err = Error::invalid_data(format!("Invalid value: {}", 42));
let err = Error::type_mismatch(1, 2);

// ❌ WRONG: Do not construct directly
// let err = Error::TypeError("Expected string type".into());
// let err = Error::InvalidData(format!("Invalid value: {}", 42).into());

§Available Constructor Functions

§Debug Mode: FORY_PANIC_ON_ERROR

For easier debugging, you can set the FORY_PANIC_ON_ERROR environment variable to make the program panic at the exact location where an error is created. This helps identify the error source with a full stack trace.

RUST_BACKTRACE=1 FORY_PANIC_ON_ERROR=1 cargo run
# or
RUST_BACKTRACE=1 FORY_PANIC_ON_ERROR=true cargo test

When enabled, any error created via the static constructor functions will panic immediately with the error message, allowing you to see the exact call stack in your debugger or panic output. Use RUST_BACKTRACE=1 together with FORY_PANIC_ON_ERROR to get a full stack trace showing exactly where the error was created.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

TypeMismatch(Cow<'static, str>)

Type mismatch between local and remote type IDs.

Do not construct this variant directly; use Error::type_mismatch instead.

§

BufferOutOfBound(usize, usize, usize)

Buffer boundary violation during read/write operations.

Do not construct this variant directly; use Error::buffer_out_of_bound instead.

§

EncodeError(Cow<'static, str>)

Error during data encoding.

Do not construct this variant directly; use Error::encode_error instead.

§

InvalidData(Cow<'static, str>)

Invalid or corrupted data encountered.

Do not construct this variant directly; use Error::invalid_data instead.

§

InvalidRef(Cow<'static, str>)

Invalid reference ID encountered.

Do not construct this variant directly; use Error::invalid_ref instead.

§

UnknownEnum(Cow<'static, str>)

Unknown enum variant encountered.

Do not construct this variant directly; use Error::unknown_enum instead.

§

TypeError(Cow<'static, str>)

General type-related error.

Do not construct this variant directly; use Error::type_error instead.

§

EncodingError(Cow<'static, str>)

Error in encoding format or conversion.

Do not construct this variant directly; use Error::encoding_error instead.

§

DepthExceed(Cow<'static, str>)

Maximum nesting depth exceeded.

Do not construct this variant directly; use Error::depth_exceed instead.

§

Unsupported(Cow<'static, str>)

Unsupported operation or feature.

Do not construct this variant directly; use Error::unsupported instead.

§

NotAllowed(Cow<'static, str>)

Operation not allowed in current context.

Do not construct this variant directly; use Error::not_allowed instead.

§

Unknown(Cow<'static, str>)

Generic unknown error.

Do not construct this variant directly; use Error::unknown instead.

§

StructVersionMismatch(Cow<'static, str>)

Struct version mismatch between local and remote schemas.

Do not construct this variant directly; use Error::struct_version_mismatch instead.

Implementations§

Source§

impl Error

Source

pub fn type_mismatch(type_a: u32, type_b: u32) -> Self

Creates a new Error::TypeMismatch with the given type IDs.

The error message will display both the original registered ID and the internal type name for user-registered types, making debugging easier.

If FORY_PANIC_ON_ERROR environment variable is set, this will panic with the error message.

§Example
use fory_core::error::Error;

let err = Error::type_mismatch(1, 2);
Source

pub fn buffer_out_of_bound( offset: usize, length: usize, capacity: usize, ) -> Self

Creates a new Error::BufferOutOfBound with the given bounds.

If FORY_PANIC_ON_ERROR environment variable is set, this will panic with the error message.

§Example
use fory_core::error::Error;

let err = Error::buffer_out_of_bound(10, 20, 25);
Source

pub fn encode_error<S: Into<Cow<'static, str>>>(s: S) -> Self

Creates a new Error::EncodeError from a string or static message.

If FORY_PANIC_ON_ERROR environment variable is set, this will panic with the error message.

§Example
use fory_core::error::Error;

let err = Error::encode_error("Failed to encode");
let err = Error::encode_error(format!("Failed to encode field {}", "name"));
Source

pub fn invalid_data<S: Into<Cow<'static, str>>>(s: S) -> Self

Creates a new Error::InvalidData from a string or static message.

If FORY_PANIC_ON_ERROR environment variable is set, this will panic with the error message.

§Example
use fory_core::error::Error;

let err = Error::invalid_data("Invalid data format");
let err = Error::invalid_data(format!("Invalid data at position {}", 42));
Source

pub fn invalid_ref<S: Into<Cow<'static, str>>>(s: S) -> Self

Creates a new Error::InvalidRef from a string or static message.

If FORY_PANIC_ON_ERROR environment variable is set, this will panic with the error message.

§Example
use fory_core::error::Error;

let err = Error::invalid_ref("Invalid reference");
let err = Error::invalid_ref(format!("Invalid ref id {}", 123));
Source

pub fn unknown_enum<S: Into<Cow<'static, str>>>(s: S) -> Self

Creates a new Error::UnknownEnum from a string or static message.

If FORY_PANIC_ON_ERROR environment variable is set, this will panic with the error message.

§Example
use fory_core::error::Error;

let err = Error::unknown_enum("Unknown enum variant");
let err = Error::unknown_enum(format!("Unknown variant {}", 5));
Source

pub fn type_error<S: Into<Cow<'static, str>>>(s: S) -> Self

Creates a new Error::TypeError from a string or static message.

If FORY_PANIC_ON_ERROR environment variable is set, this will panic with the error message.

§Example
use fory_core::error::Error;

let err = Error::type_error("Type error");
let err = Error::type_error(format!("Expected type {}", "String"));
Source

pub fn encoding_error<S: Into<Cow<'static, str>>>(s: S) -> Self

Creates a new Error::EncodingError from a string or static message.

If FORY_PANIC_ON_ERROR environment variable is set, this will panic with the error message.

§Example
use fory_core::error::Error;

let err = Error::encoding_error("Encoding failed");
let err = Error::encoding_error(format!("Failed to encode as {}", "UTF-8"));
Source

pub fn depth_exceed<S: Into<Cow<'static, str>>>(s: S) -> Self

Creates a new Error::DepthExceed from a string or static message.

If FORY_PANIC_ON_ERROR environment variable is set, this will panic with the error message.

§Example
use fory_core::error::Error;

let err = Error::depth_exceed("Max depth exceeded");
let err = Error::depth_exceed(format!("Depth {} exceeds max {}", 100, 64));
Source

pub fn unsupported<S: Into<Cow<'static, str>>>(s: S) -> Self

Creates a new Error::Unsupported from a string or static message.

If FORY_PANIC_ON_ERROR environment variable is set, this will panic with the error message.

§Example
use fory_core::error::Error;

let err = Error::unsupported("Unsupported operation");
let err = Error::unsupported(format!("Type {} not supported", "MyType"));
Source

pub fn not_allowed<S: Into<Cow<'static, str>>>(s: S) -> Self

Creates a new Error::NotAllowed from a string or static message.

If FORY_PANIC_ON_ERROR environment variable is set, this will panic with the error message.

§Example
use fory_core::error::Error;

let err = Error::not_allowed("Operation not allowed");
let err = Error::not_allowed(format!("Cannot perform {}", "delete"));
Source

pub fn struct_version_mismatch<S: Into<Cow<'static, str>>>(s: S) -> Self

Creates a new Error::StructVersionMismatch from a string or static message.

If FORY_PANIC_ON_ERROR environment variable is set, this will panic with the error message.

§Example
use fory_core::error::Error;

let err = Error::struct_version_mismatch("Version mismatch");
let err = Error::struct_version_mismatch(format!("Class {} version mismatch", "Foo"));
Source

pub fn unknown<S: Into<Cow<'static, str>>>(s: S) -> Self

Creates a new Error::Unknown from a string or static message.

This function is a convenient way to produce an error message from a literal, String, or any type that can be converted into a Cow<'static, str>.

If FORY_PANIC_ON_ERROR environment variable is set, this will panic with the error message.

§Example
use fory_core::error::Error;

let err = Error::unknown("Something went wrong");
let err = Error::unknown(format!("ID:{} not found", 1));
Source

pub fn enhance_type_error<T: ?Sized + 'static>(err: Error) -> Error

Enhances a Error::TypeError with additional type name information.

If the error is a TypeError, appends the type name to the message. Otherwise, returns the error unchanged.

§Example
use fory_core::error::Error;

let err = Error::type_error("Type not registered");
let enhanced = Error::enhance_type_error::<String>(err);
// Result: "Type not registered (type: alloc::string::String)"

Trait Implementations§

Source§

impl Debug for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Error

Source§

fn fmt(&self, __formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for Error

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more

Auto Trait Implementations§

§

impl Freeze for Error

§

impl RefUnwindSafe for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl UnwindSafe for Error

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.