Skip to content

Commit

Permalink
Handle HTTP errors
Browse files Browse the repository at this point in the history
Handle and log http errors separately.
  • Loading branch information
linuskendall committed Apr 13, 2023
1 parent 2101e81 commit 72f94cf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
2 changes: 2 additions & 0 deletions nft_ingester/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub enum IngesterError {
UnrecoverableTaskError,
#[error("Cache Storage Write Error {0}")]
CacheStorageWriteError(String),
#[error("HttpError {status_code}")]
HttpError { status_code: String },
}

impl From<reqwest::Error> for IngesterError {
Expand Down
12 changes: 8 additions & 4 deletions nft_ingester/src/tasks/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ impl DownloadMetadataTask {
let client = ClientBuilder::new()
.timeout(Duration::from_secs(3))
.build()?;
let val: serde_json::Value = Client::get(&client, uri) // Need to check for malicious sites ?
let response = Client::get(&client, uri) // Need to check for malicious sites ?
.send()
.await?
.json()
.await?;
Ok(val)

if response.status() != reqwest::StatusCode::OK {
Err(IngesterError::HttpError{ status_code: response.status().as_str().to_string() })
} else {
let val: serde_json::Value = response.json().await?;
Ok(val)
}
}
}

Expand Down
33 changes: 22 additions & 11 deletions nft_ingester/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,29 @@ impl TaskManager {
task.errors = Set(Some(e.to_string()));
task.locked_until = Set(None);

if e == IngesterError::BatchInitNetworkingError {
// Network errors are common for off-chain JSONs.
// Logging these as errors is far too noisy.
metric! {
statsd_count!("ingester.bgtask.network_error", 1, "type" => task_name);
}
warn!("Task failed due to network error: {}", e);
} else {
metric! {
statsd_count!("ingester.bgtask.error", 1, "type" => task_name);
match e {
IngesterError::BatchInitNetworkingError => {
// Network errors are common for off-chain JSONs.
// Logging these as errors is far too noisy.
metric! {
statsd_count!("ingester.bgtask.network_error", 1, "type" => task_name);
}
warn!("Task failed due to network error: {}", e);
},
IngesterError::HttpError { ref status_code } => {
metric! {
statsd_count!("ingester.bgtask.http_error", 1,
"status" => &status_code,
"type" => task_name);
}
warn!("Task failed due to HTTP error: {}", e);
},
_ => {
metric! {
statsd_count!("ingester.bgtask.error", 1, "type" => task_name);
}
error!("Task Run Error: {}", e);
}
error!("Task Run Error: {}", e);
}
}
}
Expand Down

0 comments on commit 72f94cf

Please sign in to comment.