Skip to main content

Error

Struct Error 

Source
pub struct Error {
    pub code: i32,
    pub category: ErrorCategory,
    pub fatal: bool,
    pub message: String,
}
Expand description

Error is the main error type returned by fallible NWEP operations.

An Error is constructed from a non-zero integer error code returned by the C library. The constructor calls back into the C library to resolve the human-readable message, category, and fatality flag, so no further lookups are needed by callers.

§Fatal errors

When Error::fatal is true the connection or server context that produced the error is no longer usable and must be dropped. Non-fatal errors (e.g., ERR_NETWORK_TIMEOUT on a single request) may be retried without tearing down the connection.

§Display format

[network:1003] connection timed out

The format is [<category>:<code>] <message>, which makes it easy to grep logs by subsystem or numeric code.

§Example

use nwep::error::{Error, ERR_CRYPTO_VERIFY_FAILED};

let err = Error::from_code(ERR_CRYPTO_VERIFY_FAILED);
assert_eq!(err.category, nwep::error::ErrorCategory::Crypto);
eprintln!("{err}");

Fields§

§code: i32

code is the raw integer error code as returned by the C library. Zero is never a valid error code; Error values are only constructed for non-zero codes.

§category: ErrorCategory

category identifies the NWEP subsystem that produced this error. Use it for coarse-grained error handling (e.g., distinguishing network transients from crypto failures).

§fatal: bool

fatal is true when the C library considers the connection or context unrecoverable. Callers must drop the associated crate::Client or crate::Server when this flag is set.

§message: String

message is the English-language description of the error retrieved from the C library via nwep_strerror. Falls back to "unknown error" if the C library returns a null pointer.

Implementations§

Source§

impl Error

Source

pub fn from_code(code: i32) -> Self

from_code constructs an Error by querying the C library for the human-readable message, fatality status, and category associated with code.

This is the primary constructor and is called automatically by the internal [check] and [check_ssize] helpers whenever a C function returns a non-zero error code.

§Example
use nwep::error::{Error, ERR_NETWORK_TIMEOUT};

let err = Error::from_code(ERR_NETWORK_TIMEOUT);
println!("{}", err.message);
Source

pub fn to_status(&self) -> &'static str

to_status maps this error’s code to the corresponding NWEP protocol status string (e.g., "unauthorized", "not_found").

The returned string is a 'static reference into the C library’s string table, so it is valid for the lifetime of the process. If the C library returns a null pointer the fallback value "internal_error" is returned.

This is useful when an error needs to be communicated back to a remote client as a protocol-level status code in a response header.

§Example
use nwep::error::{Error, ERR_NETWORK_CONN_FAILED};

let err = Error::from_code(ERR_NETWORK_CONN_FAILED);
assert_eq!(err.to_status(), "internal_error");

Trait Implementations§

Source§

impl Clone for Error

Source§

fn clone(&self) -> Error

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
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, f: &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 UnsafeUnpin 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.