Skip to main content

CodecError

Struct CodecError 

Source
pub struct CodecError {
    pub kind: CodecErrorKind,
    /* private fields */
}
Expand description

An error produced while reading or writing protocol data.

The error records the originating CodecKind, the CodecOperation, the progress within that codec, and optional enclosing codec contexts. I/O and parser errors are retained as an error source.

Error enums are non-exhaustive, so downstream matches must include a wildcard arm.

§Example

use mcproto_codec::{
    error::{CodecErrorKind, CodecKind, CodecOperation},
    varint::VarIntRead,
};

let mut input = [0x80].as_slice();
let error = input
    .read_varint()
    .unwrap_err()
    .with_context(CodecKind::String);

assert_eq!(error.codec(), CodecKind::VarInt);
assert_eq!(error.operation(), CodecOperation::Read);
assert_eq!(error.bytes_processed(), 1);
assert_eq!(error.contexts(), &[CodecKind::String]);

match error.kind() {
    CodecErrorKind::UnexpectedEof => {}
    _ => panic!("unexpected error: {error}"),
}

Fields§

§kind: CodecErrorKind

The error classification.

This field and kind expose the same value. The accessor is convenient when working through a shared reference.

Implementations§

Source§

impl CodecError

Source

pub const fn kind(&self) -> CodecErrorKind

Returns the error classification.

Source

pub const fn codec(&self) -> CodecKind

Returns the codec that originally reported the error.

Source

pub fn context(&self) -> Option<CodecKind>

Returns the outermost enclosing codec context, if one was added.

This is the last element of contexts, not the originating codec returned by codec.

Source

pub fn contexts(&self) -> &[CodecKind]

Returns all enclosing codec contexts, ordered from nearest to outermost.

The originating codec is not included. Each call to with_context appends one element.

Source

pub const fn operation(&self) -> CodecOperation

Returns the operation being performed when the error occurred.

Source

pub const fn bytes_processed(&self) -> usize

Returns the byte progress reported by the originating codec.

Built-in codecs count bytes from the start of their encoded value. Bytes successfully read or written before an I/O failure are included. A byte that was read and then found to be invalid is also included. For a length-prefixed value, the originating codec determines whether its prefix is part of the count.

Adding an outer context does not translate this value into an offset within the enclosing codec.

Source

pub fn io_error(&self) -> Option<&Error>

Returns the underlying io::Error, if the source is an I/O error.

Invalid NBT or JSON errors may have a non-I/O source; access those through Error::source instead.

Source

pub fn with_context(self, context: CodecKind) -> Self

Adds an enclosing codec to the error’s context chain.

Contexts should be added as the error propagates outward. Repeated calls therefore order contexts from nearest to outermost, and context returns the most recently added context.

Source

pub fn from_read_error( codec: CodecKind, bytes_processed: usize, source: Error, ) -> Self

Creates an error from an I/O failure that occurred while reading.

io::ErrorKind::UnexpectedEof maps to CodecErrorKind::UnexpectedEof; every other error kind maps to CodecErrorKind::Io. The source error is retained.

bytes_processed is the number of bytes read before source occurred.

Source

pub fn from_write_error( codec: CodecKind, bytes_processed: usize, source: Error, ) -> Self

Creates an error from an I/O failure that occurred while writing.

All write errors map to CodecErrorKind::Io, and the source error is retained. bytes_processed is the number of bytes written before source occurred.

Source

pub const fn invalid_encoding( codec: CodecKind, bytes_processed: usize, reason: InvalidEncodingReason, ) -> Self

Creates an invalid encoding error for a read operation.

Use invalid_encoding_for_operation when the operation is not necessarily CodecOperation::Read.

Source

pub const fn invalid_encoding_for_operation( codec: CodecKind, operation: CodecOperation, bytes_processed: usize, reason: InvalidEncodingReason, ) -> Self

Creates an invalid encoding error for the specified operation.

Unlike invalid_encoding, this constructor does not assume that the error occurred while reading.

Source

pub fn invalid_encoding_for_operation_with_source( codec: CodecKind, operation: CodecOperation, bytes_processed: usize, reason: InvalidEncodingReason, source: impl Error + Send + Sync + 'static, ) -> Self

Creates an invalid encoding error with an underlying source error.

operation may be either reading or writing. The supplied error is available through Error::source; if it is an io::Error, it is also available through io_error.

Trait Implementations§

Source§

impl Debug for CodecError

Source§

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

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

impl Display for CodecError

Source§

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

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

impl Error for CodecError

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§

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.