Error Handling
All operations return Result<T, WebCryptoError>. The error type maps JavaScript DOMException names to typed Rust variants.
WebCryptoError
DOMException variants
| Variant | JS exception | When |
|---|---|---|
NotSupported(String) | NotSupportedError | Algorithm not supported by the runtime |
Syntax(String) | SyntaxError | Required parameter missing or malformed |
InvalidAccess(String) | InvalidAccessError | Key cannot be used for the requested operation |
Data(String) | DataError | Key data is invalid |
Operation(String) | OperationError | Cryptographic operation failed (e.g. decryption with wrong key) |
Type(String) | TypeError | Value has the wrong type |
Platform variants
| Variant | When |
|---|---|
Unknown(String) | JS error that does not match any known DOMException |
NonWasmBuild | Built for a non-wasm target |
RuntimeUnavailable | wasm32 build, but crypto object not found in the runtime |
NotImplemented | Method 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())
}