Skip to content

Commit

Permalink
fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroShkvorets committed Mar 17, 2024
1 parent 8aca99d commit c32f9c1
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions antelope.trxstats/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use substreams::errors::Error;
use substreams::errors::Error as SubstreamsError;
use substreams_antelope::pb::Block;
use substreams_database_change::pb::database::DatabaseChanges;
use substreams_database_change::tables::Tables as DatabaseChangeTables;
Expand All @@ -16,7 +16,7 @@ struct Params {
action_count: u32,
}

fn parse_params(input: String) -> Result<Params, Error> {
fn parse_params(input: String) -> Result<Params, String> {
let mut result = Params::default();
if input.is_empty() {
return Ok(result);
Expand All @@ -25,27 +25,28 @@ fn parse_params(input: String) -> Result<Params, Error> {
for param in input.split('&') {
let (key, value) = param
.split_once('=')
.ok_or_else(|| Error::Unexpected(format!("Invalid parameter format: {}", param)))?;
let parsed_value = value
.parse::<u32>()
.map_err(|_| Error::Unexpected(format!("Invalid param value for key '{}'", key)))?;
.ok_or_else(|| format!("Invalid parameter format: {}", param))?;
let parsed_value = value.parse::<u32>().map_err(|_| format!("Invalid param value for key '{}'", key))?;

match key {
"cpu_elapsed" => result.cpu_elapsed = parsed_value,
"net_elapsed" => result.net_elapsed = parsed_value,
"cpu_usage" => result.cpu_usage = parsed_value,
"net_usage" => result.net_usage = parsed_value,
"action_count" => result.action_count = parsed_value,
_ => return Err(Error::Unexpected(format!("Unknown parameter: '{}'", key))),
_ => return Err(format!("Unknown parameter: '{}'", key)),
}
}

Ok(result)
}

#[substreams::handlers::map]
fn map_trxs(params: String, block: Block) -> Result<Transactions, Error> {
let params = parse_params(params)?;
fn map_trxs(params: String, block: Block) -> Result<Transactions, SubstreamsError> {
let params = match parse_params(params) {
Ok(params) => params,
Err(err) => panic!("Bad params: {}", err),
};
Ok(Transactions {
transactions: block
.all_transaction_traces()
Expand Down

0 comments on commit c32f9c1

Please sign in to comment.