global: improve errors

This commit is contained in:
nym21
2025-12-16 20:49:19 +01:00
parent 3ca83a2289
commit 1ad8d8a631
36 changed files with 232 additions and 334 deletions

View File

@@ -37,7 +37,7 @@ impl Query {
let outputtype = OutputType::from(&script);
dbg!(outputtype);
let Ok(bytes) = AddressBytes::try_from((&script, outputtype)) else {
return Err(Error::Str("Failed to convert the address to bytes"));
return Err(Error::InvalidAddress);
};
let addresstype = outputtype;
let hash = AddressHash::from(&bytes);
@@ -116,8 +116,8 @@ impl Query {
let txindex = stores
.txidprefix_to_txindex
.get(&after_txid.into())
.map_err(|_| Error::Str("Failed to look up after_txid"))?
.ok_or(Error::Str("after_txid not found"))?
.map_err(|_| Error::UnknownTxid)?
.ok_or(Error::UnknownTxid)?
.into_owned();
Some(txindex)
} else {
@@ -202,7 +202,7 @@ impl Query {
}
pub fn address_mempool_txids(&self, address: Address) -> Result<Vec<Txid>> {
let mempool = self.mempool().ok_or(Error::Str("Mempool not available"))?;
let mempool = self.mempool().ok_or(Error::MempoolNotAvailable)?;
let bytes = AddressBytes::from_str(&address)?;
let addresses = mempool.get_addresses();

View File

@@ -17,7 +17,7 @@ impl Query {
let max_height = self.max_height();
if height > max_height {
return Err(Error::Str("Block height out of range"));
return Err(Error::OutOfRange("Block height out of range".into()));
}
let blockhash = indexer.vecs.block.height_to_blockhash.read_once(height)?;
@@ -68,7 +68,7 @@ impl Query {
.blockhashprefix_to_height
.get(&prefix)?
.map(|h| *h)
.ok_or(Error::Str("Block not found"))
.ok_or(Error::NotFound("Block not found".into()))
}
fn max_height(&self) -> Height {

View File

@@ -24,7 +24,7 @@ impl Query {
.saturating_sub(1),
);
if height > max_height {
return Err(Error::Str("Block height out of range"));
return Err(Error::OutOfRange("Block height out of range".into()));
}
let position = computer.blks.height_to_position.read_once(height)?;

View File

@@ -14,7 +14,7 @@ impl Query {
let max_height_usize: usize = max_height.into();
if max_height_usize == 0 {
return Err(Error::Str("No blocks indexed"));
return Err(Error::NotFound("No blocks indexed".into()));
}
let target = timestamp;

View File

@@ -28,7 +28,7 @@ impl Query {
let max_height = self.height();
if height > max_height {
return Err(Error::Str("Block height out of range"));
return Err(Error::OutOfRange("Block height out of range".into()));
}
let first_txindex = indexer.vecs.tx.height_to_first_txindex.read_once(height)?;
@@ -64,7 +64,7 @@ impl Query {
let max_height = self.height();
if height > max_height {
return Err(Error::Str("Block height out of range"));
return Err(Error::OutOfRange("Block height out of range".into()));
}
let first_txindex = indexer.vecs.tx.height_to_first_txindex.read_once(height)?;
@@ -101,7 +101,7 @@ impl Query {
let max_height = self.height();
if height > max_height {
return Err(Error::Str("Block height out of range"));
return Err(Error::OutOfRange("Block height out of range".into()));
}
let first_txindex = indexer.vecs.tx.height_to_first_txindex.read_once(height)?;
@@ -117,7 +117,7 @@ impl Query {
let tx_count = next - first;
if index >= tx_count {
return Err(Error::Str("Transaction index out of range"));
return Err(Error::OutOfRange("Transaction index out of range".into()));
}
let txindex = TxIndex::from(first + index);

View File

@@ -5,12 +5,12 @@ use crate::Query;
impl Query {
pub fn mempool_info(&self) -> Result<MempoolInfo> {
let mempool = self.mempool().ok_or(Error::Str("Mempool not available"))?;
let mempool = self.mempool().ok_or(Error::MempoolNotAvailable)?;
Ok(mempool.get_info())
}
pub fn mempool_txids(&self) -> Result<Vec<Txid>> {
let mempool = self.mempool().ok_or(Error::Str("Mempool not available"))?;
let mempool = self.mempool().ok_or(Error::MempoolNotAvailable)?;
let txs = mempool.get_txs();
Ok(txs.keys().cloned().collect())
}
@@ -22,7 +22,7 @@ impl Query {
}
pub fn mempool_blocks(&self) -> Result<Vec<MempoolBlock>> {
let mempool = self.mempool().ok_or(Error::Str("Mempool not available"))?;
let mempool = self.mempool().ok_or(Error::MempoolNotAvailable)?;
let block_stats = mempool.get_block_stats();

View File

@@ -25,17 +25,16 @@ impl Query {
// Check if metric exists but with different indexes
if let Some(indexes) = self.vecs().metric_to_indexes(metric.clone()) {
let index_list: Vec<_> = indexes.iter().map(|i| i.to_string()).collect();
return Error::String(format!(
"'{metric}' doesn't support the requested index. Supported indexes: {}",
index_list.join(", ")
));
return Error::MetricUnsupportedIndex {
metric: metric.to_string(),
supported: index_list.join(", "),
};
}
// Metric doesn't exist, suggest alternatives
if let Some(first) = self.match_metric(metric, Limit::MIN).first() {
Error::String(format!("Could not find '{metric}', did you mean '{first}'?"))
} else {
Error::String(format!("Could not find '{metric}'."))
Error::MetricNotFound {
metric: metric.to_string(),
suggestion: self.match_metric(metric, Limit::MIN).first().map(|s| s.to_string()),
}
}
@@ -161,7 +160,7 @@ impl Query {
/// Returns error if no metrics requested or any requested metric is not found.
pub fn search(&self, params: &MetricSelection) -> Result<Vec<&'static dyn AnyExportableVec>> {
if params.metrics.is_empty() {
return Err(Error::String("No metrics specified".to_string()));
return Err(Error::NoMetrics);
}
let mut vecs = Vec::with_capacity(params.metrics.len());
for metric in params.metrics.iter() {
@@ -195,9 +194,10 @@ impl Query {
let weight = Self::weight(&vecs, params.from(), params.to_for_len(metric.len()));
if weight > max_weight {
return Err(Error::String(format!(
"Request too heavy: {weight} bytes exceeds limit of {max_weight} bytes"
)));
return Err(Error::WeightExceeded {
requested: weight,
max: max_weight,
});
}
self.format(*metric, &params.range)
@@ -219,9 +219,10 @@ impl Query {
let min_len = vecs.iter().map(|v| v.len()).min().expect("search guarantees non-empty");
let weight = Self::weight(&vecs, params.from(), params.to_for_len(min_len));
if weight > max_weight {
return Err(Error::String(format!(
"Request too heavy: {weight} bytes exceeds limit of {max_weight} bytes"
)));
return Err(Error::WeightExceeded {
requested: weight,
max: max_weight,
});
}
self.format_bulk(&vecs, &params.range)

View File

@@ -64,9 +64,10 @@ impl Query {
let min_len = vecs.iter().map(|v| v.len()).min().expect("search guarantees non-empty");
let weight = Self::weight(&vecs, params.from(), params.to_for_len(min_len));
if weight > max_weight {
return Err(Error::String(format!(
"Request too heavy: {weight} bytes exceeds limit of {max_weight} bytes"
)));
return Err(Error::WeightExceeded {
requested: weight,
max: max_weight,
});
}
self.format_legacy(&vecs, &params.range)

View File

@@ -8,10 +8,14 @@
#![allow(dead_code)]
use brk_error::Result;
use brk_types::{BlockFeeRatesEntry, FeeRatePercentiles, TimePeriod};
use vecdb::{IterableVec, VecIndex};
use brk_types::{
BlockFeeRatesEntry,
// FeeRatePercentiles,
TimePeriod,
};
// use vecdb::{IterableVec, VecIndex};
use super::dateindex_iter::DateIndexIter;
// use super::dateindex_iter::DateIndexIter;
use crate::Query;
impl Query {

View File

@@ -98,7 +98,7 @@ impl Query {
.pools
.vecs
.get(&slug)
.ok_or_else(|| Error::Str("Pool data not found"))?;
.ok_or_else(|| Error::NotFound("Pool data not found".into()))?;
let mut cumulative = pool_vecs
.indexes_to_blocks_mined

View File

@@ -213,7 +213,7 @@ impl Query {
let buffer = reader.read_raw_bytes(position, *total_size as usize)?;
let mut cursor = Cursor::new(buffer);
let tx = bitcoin::Transaction::consensus_decode(&mut cursor)
.map_err(|_| Error::Str("Failed to decode transaction"))?;
.map_err(|_| Error::Parse("Failed to decode transaction".into()))?;
// For iterating through inputs, we need iterators (multiple lookups)
let mut txindex_to_txid_iter = indexer.vecs.tx.txindex_to_txid.iter()?;