Expand description
Custom error library support for the openssl
crate.
OpenSSL allows third-party libraries to integrate with its error API. This crate provides a safe interface to that.
§Examples
use gmssl_errors::{gmssl_errors, put_error};
use gmssl::error::Error;
// Errors are organized at the top level into "libraries". The
// gmssl_errors! macro can define these.
//
// Libraries contain a set of functions and reasons. The library itself,
// its functions, and its definitions all all have an associated message
// string. This string is what's shown in OpenSSL errors.
//
// The macro creates a type for each library with associated constants for
// its functions and reasons.
gmssl_errors! {
pub library MyLib("my cool library") {
functions {
FIND_PRIVATE_KEY("find_private_key");
}
reasons {
IO_ERROR("IO error");
BAD_PASSWORD("invalid private key password");
}
}
}
// The put_error! macro pushes errors onto the OpenSSL error stack.
put_error!(MyLib::FIND_PRIVATE_KEY, MyLib::BAD_PASSWORD);
// Prints `error:80001002:my cool library:find_private_key:invalid private key password:src/lib.rs:27:`
println!("{}", Error::get().unwrap());
// You can also optionally attach an extra string of context using the
// standard Rust format syntax.
let tries = 2;
put_error!(MyLib::FIND_PRIVATE_KEY, MyLib::IO_ERROR, "tried {} times", tries);
// Prints `error:80001001:my cool library:find_private_key:IO error:src/lib.rs:34:tried 2 times`
println!("{}", Error::get().unwrap());
Macros§
- gmssl_
errors - Defines custom OpenSSL error libraries.
- put_
error - Pushes an error onto the OpenSSL error stack.
Structs§
- Function
- A function declaration, parameterized by its error library.
- Reason
- A reason declaration, parameterized by its error library.
Traits§
- Library
- An OpenSSL error library.