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 outThe 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: i32code 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: ErrorCategorycategory identifies the NWEP subsystem that produced this error.
Use it for coarse-grained error handling (e.g., distinguishing network
transients from crypto failures).
fatal: boolfatal 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: Stringmessage 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
impl Error
Sourcepub fn from_code(code: i32) -> Self
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);Sourcepub fn to_status(&self) -> &'static str
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");