Skip to main content

ForgeError

Trait ForgeError 

Source
pub trait ForgeError:
    Error
    + Send
    + Sync
    + 'static {
    // Required methods
    fn kind(&self) -> &'static str;
    fn caption(&self) -> &'static str;

    // Provided methods
    fn is_retryable(&self) -> bool { ... }
    fn is_fatal(&self) -> bool { ... }
    fn status_code(&self) -> u16 { ... }
    fn exit_code(&self) -> i32 { ... }
    fn user_message(&self) -> String { ... }
    fn dev_message(&self) -> String { ... }
    fn backtrace(&self) -> Option<&Backtrace> { ... }
    fn register(&self) { ... }
}
Expand description

Base trait for all custom error variants.

Required Methods§

Source

fn kind(&self) -> &'static str

Returns the kind of error, typically matching the enum variant

Source

fn caption(&self) -> &'static str

Returns a human-readable caption for the error

Provided Methods§

Source

fn is_retryable(&self) -> bool

Returns true if the operation can be retried

Source

fn is_fatal(&self) -> bool

Returns true if the error is fatal and should terminate the program

Source

fn status_code(&self) -> u16

Returns an appropriate HTTP status code for the error

Source

fn exit_code(&self) -> i32

Returns an appropriate process exit code for the error

Source

fn user_message(&self) -> String

Returns a message that is safe to show to end users.

The default is the error’s caption: fixed text per kind that never carries the error’s data. Display, dev_message and Debug do carry it (file paths, endpoints with any credentials in them, source errors, field values), so keep those for logs. Override this method to show a message you have checked is safe, such as a validation hint.

Until 1.1.0 the default was the full Display text, which is not safe to show.

§Example
use error_forge::{AppError, ForgeError};

let error = AppError::network("https://user:secret@db.internal/prod", None);
assert!(!error.user_message().contains("secret"));
assert!(error.to_string().contains("db.internal"));
Source

fn dev_message(&self) -> String

Returns a detailed technical message for developers/logs

Source

fn backtrace(&self) -> Option<&Backtrace>

Returns a backtrace if available

Source

fn register(&self)

Fires the registered error hook (see try_register_error_hook) with this error’s caption, kind and flags.

Despite the name, nothing is stored: this does not touch the error-code ErrorRegistry. AppError and define_errors! constructors already fire the hook, so calling register on such an error fires it a second time. It is a no-op when no hook is registered.

§Example
use error_forge::{AppError, ForgeError};

let error = AppError::config("missing key");
// Reports the error to the hook again, if one is installed.
error.register();

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§