bitwarden_error_macro

Attribute Macro bitwarden_error

Source
#[bitwarden_error]
Expand description

A procedural macro for generating error types with customizable serialization behavior.

§Attributes

§Error type

  • basic: The error is converted into a string using the ToString trait.
  • flat: The error is converted into a flat structure using the FlatError trait.
  • full: The entire error stack is made available using serde.

§Export as

export_as: The name of the exported TypeScript type. If not provided, the name of the Rust type is used. Note: This attribute is only available when using the basic and flat error types.

§Examples

§Basic

Using the basic error type:

use bitwarden_error::prelude::*;
use thiserror::Error;

#[derive(Debug, Error)]
#[bitwarden_error(basic)]
enum MyError {
    #[error("Not found")]
    NotFound,
    #[error("Permission denied")]
    PermissionDenied,
}

will generate the following TypeScript definition:

export interface MyError extends Error {
   name: "MyError";
}

§Flat

Using the flat error type:

use bitwarden_error::prelude::*;
use thiserror::Error;

#[derive(Debug, Error)]
#[bitwarden_error(basic)]
enum MyError {
    #[error("Not found")]
    NotFound,
    #[error("Permission denied")]
    PermissionDenied,
}

will generate the following TypeScript definition:

export interface MyError extends Error {
  name: "MyError";
 variant: "NotFound" | "PermissionDenied";
}

Using the full error type:

use bitwarden_error::prelude::*;
use serde::Serialize;
use thiserror::Error;

#[bitwarden_error(full)]
#[derive(Debug, Error)]
#[error("Vault is locked")]
struct VaultLocked;

#[derive(Debug, Serialize)]
struct ExternalError;

#[bitwarden_error(full)]
#[derive(Debug, Error)]
enum MyError {
    #[error(transparent)]
    VaultLocked(#[from] VaultLocked),
    #[error("External error")]
    ExternalError(ExternalError),
}

will use tsify_next::Tsify to generate roughly the following TypeScript definition:

export type CryptoError =
  | { MissingFieldError: MissingFieldError }
  | { VaultLocked: VaultLocked };

export interface VaultLocked { }

All the general interopability rules apply such as external types needing to be defined as custom types.