Skip to content

Error Handling

All operations return Result<T, WebCryptoError>. The error type maps JavaScript DOMException names to typed Rust variants.

WebCryptoError

DOMException variants

VariantJS exceptionWhen
NotSupported(String)NotSupportedErrorAlgorithm not supported by the runtime
Syntax(String)SyntaxErrorRequired parameter missing or malformed
InvalidAccess(String)InvalidAccessErrorKey cannot be used for the requested operation
Data(String)DataErrorKey data is invalid
Operation(String)OperationErrorCryptographic operation failed (e.g. decryption with wrong key)
Type(String)TypeErrorValue has the wrong type

Platform variants

VariantWhen
Unknown(String)JS error that does not match any known DOMException
NonWasmBuildBuilt for a non-wasm target
RuntimeUnavailablewasm32 build, but crypto object not found in the runtime
NotImplementedMethod not yet implemented

Usage with ?

WebCryptoError implements std::error::Error, so it works with ? and anyhow:

rust
use anyhow::Result;

async fn hash(data: &[u8]) -> Result<String> {
    let subtle = SubtleCrypto::new()?;
    let output = subtle.digest(Hash::Sha256, data).await?;
    Ok(output.to_hex())
}