Crates.io | brk_error |
lib.rs | brk_error |
version | 0.0.95 |
created_at | 2025-08-07 19:24:23.075518+00 |
updated_at | 2025-08-28 10:44:30.956625+00 |
description | Errors used throughout BRK |
homepage | https://bitcoinresearchkit.org |
repository | https://github.com/bitcoinresearchkit/brk |
max_upload_size | |
id | 1785673 |
size | 41,113 |
Centralized error handling for the Bitcoin Research Kit
brk_error
provides a unified error type and result system used throughout the BRK ecosystem. It consolidates error handling from multiple external dependencies and adds Bitcoin-specific error variants.
Error
enum that covers all error cases across BRK cratesResult<T, E = Error>
for consistent error handlingAutomatic From
implementations for errors from:
std::io::Error
bitcoincore_rpc::Error
fjall::Error
, vecdb::Error
serde_json::Error
jiff::Error
, SystemTimeError
minreq::Error
zerocopy
conversion errorsWrongAddressType
- Invalid address type for operationUnindexableDate
- Date before Bitcoin genesis (2009-01-03)WrongLength
- Invalid data length for Bitcoin structuresQuickCacheError
- Cache operation failuresuse brk_error::{Error, Result};
fn process_block() -> Result<Block> {
let rpc_client = get_rpc_client()?; // bitcoincore_rpc::Error -> Error
let block_data = rpc_client.get_block_info(&hash)?;
// Custom Bitcoin-specific validation
if block_data.height < 0 {
return Err(Error::Str("Invalid block height"));
}
Ok(block_data)
}
use brk_error::Result;
fn save_data(data: &[u8]) -> Result<()> {
// I/O error automatically converted
std::fs::write("data.bin", data)?;
// JSON serialization error automatically converted
let json = serde_json::to_string(&data)?;
// Database error automatically converted
database.insert("key", &json)?;
Ok(())
}
use brk_error::{Error, Result};
fn validate_date(date: &Date) -> Result<()> {
if *date < Date::new(2009, 1, 3) {
return Err(Error::UnindexableDate);
}
Ok(())
}
fn validate_address_type(output_type: OutputType) -> Result<()> {
if !output_type.is_address() {
return Err(Error::WrongAddressType);
}
Ok(())
}
// Static string errors (zero allocation)
Err(Error::Str("Invalid configuration"))
// Dynamic string errors
Err(Error::String(format!("Block {} not found", height)))
The crate provides a convenient Result
type alias:
pub type Result<T, E = Error> = std::result::Result<T, E>;
This allows for clean function signatures throughout BRK:
fn parse_block() -> Result<Block> { /* ... */ }
fn index_transactions() -> Result<Vec<Transaction>> { /* ... */ }
All errors implement Display
and std::error::Error
, providing:
anyhow
vecdb
- Vector database error typesbitcoincore-rpc
- Bitcoin Core RPC client errorsfjall
- Key-value store errorsjiff
- Date/time operation errorsminreq
- HTTP request errorsserde_json
- JSON serialization errorszerocopy
- Zero-copy conversion errorsThis README was generated by Claude Code