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: CodecErrorKindThe error classification.
This field and kind expose the same value. The accessor
is convenient when working through a shared reference.
Implementations§
Source§impl CodecError
impl CodecError
Sourcepub const fn kind(&self) -> CodecErrorKind
pub const fn kind(&self) -> CodecErrorKind
Returns the error classification.
Sourcepub fn contexts(&self) -> &[CodecKind]
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.
Sourcepub const fn operation(&self) -> CodecOperation
pub const fn operation(&self) -> CodecOperation
Returns the operation being performed when the error occurred.
Sourcepub const fn bytes_processed(&self) -> usize
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.
Sourcepub fn io_error(&self) -> Option<&Error>
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.
Sourcepub fn with_context(self, context: CodecKind) -> Self
pub fn with_context(self, context: CodecKind) -> Self
Sourcepub fn from_read_error(
codec: CodecKind,
bytes_processed: usize,
source: Error,
) -> Self
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.
Sourcepub fn from_write_error(
codec: CodecKind,
bytes_processed: usize,
source: Error,
) -> Self
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.
Sourcepub const fn invalid_encoding(
codec: CodecKind,
bytes_processed: usize,
reason: InvalidEncodingReason,
) -> Self
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.
Sourcepub const fn invalid_encoding_for_operation(
codec: CodecKind,
operation: CodecOperation,
bytes_processed: usize,
reason: InvalidEncodingReason,
) -> Self
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.
Sourcepub fn invalid_encoding_for_operation_with_source(
codec: CodecKind,
operation: CodecOperation,
bytes_processed: usize,
reason: InvalidEncodingReason,
source: impl Error + Send + Sync + 'static,
) -> Self
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
impl Debug for CodecError
Source§impl Display for CodecError
impl Display for CodecError
Source§impl Error for CodecError
impl Error for CodecError
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()