Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add context to errors #101

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ env:

jobs:
verify-rust-version:
runs-on: buildjet-4vcpu-ubuntu-2204
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# Ensure CI is using the same minimum toolchain specified in fuels Cargo.toml
Expand Down Expand Up @@ -49,7 +49,7 @@ jobs:
cargo-verifications:
needs:
- verify-rust-version
runs-on: buildjet-4vcpu-ubuntu-2204
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand Down Expand Up @@ -79,7 +79,7 @@ jobs:
run: ./run_tests.sh

publish-crates-check:
runs-on: buildjet-4vcpu-ubuntu-2204
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand All @@ -101,7 +101,7 @@ jobs:
- publish-crates-check
# Only do this job if publishing a release
if: github.event_name == 'release' && github.event.action == 'published'
runs-on: buildjet-4vcpu-ubuntu-2204
runs-on: ubuntu-latest

steps:
- name: Checkout repository
Expand All @@ -125,7 +125,7 @@ jobs:
publish-docker-image:
needs:
- cargo-verifications
runs-on: buildjet-4vcpu-ubuntu-2204
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
Expand Down
28 changes: 28 additions & 0 deletions committer/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use actix_web::ResponseError;
use tokio::task::JoinError;

Expand All @@ -11,6 +13,13 @@ pub enum Error {
Storage(String),
}

pub trait WithContext<T> {
fn with_context<C, F>(self, context: F) -> Result<T>
where
C: Display + Send + Sync + 'static,
F: FnOnce() -> C;
}

impl From<serde_json::Error> for Error {
fn from(error: serde_json::Error) -> Self {
Self::Other(error.to_string())
Expand Down Expand Up @@ -71,3 +80,22 @@ impl From<config::ConfigError> for Error {
impl ResponseError for Error {}

pub type Result<T> = std::result::Result<T, Error>;

impl<T> WithContext<T> for Result<T> {
fn with_context<C, F>(self, context: F) -> Result<T>
where
C: Display + Send + Sync + 'static,
F: FnOnce() -> C,
{
if let Err(err) = self {
let new_err = match err {
Error::Other(e) => Error::Other(format!("{}: {}", context(), e)),
Error::Network(e) => Error::Network(format!("{}: {}", context(), e)),
Error::Storage(e) => Error::Storage(format!("{}: {}", context(), e)),
};
Err(new_err)
} else {
self
}
}
}
19 changes: 13 additions & 6 deletions committer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod errors;
mod setup;

use api::launch_api_server;
use errors::Result;
use errors::{Result, WithContext};
use metrics::prometheus::Registry;
use tokio_util::sync::CancellationToken;

Expand All @@ -21,10 +21,14 @@ pub type Validator = validator::BlockValidator;
async fn main() -> Result<()> {
setup::logger();

let config = config::parse()?;
config.validate()?;
let config = config::parse().with_context(|| "failed to parse config")?;
config
.validate()
.with_context(|| "config validation failed")?;

let storage = setup::storage(&config).await?;
let storage = setup::storage(&config)
.await
.with_context(|| "failed to connect to database")?;

let internal_config = config::Internal::default();
let cancel_token = CancellationToken::new();
Expand All @@ -35,7 +39,9 @@ async fn main() -> Result<()> {
setup::fuel_adapter(&config, &internal_config, &metrics_registry);

let (ethereum_rpc, eth_health_check) =
setup::l1_adapter(&config, &internal_config, &metrics_registry).await?;
setup::l1_adapter(&config, &internal_config, &metrics_registry)
.await
.with_context(|| "could not setup l1 adapter")?;

let commit_interval = ethereum_rpc.commit_interval();

Expand Down Expand Up @@ -104,7 +110,8 @@ async fn main() -> Result<()> {
fuel_health_check,
eth_health_check,
)
.await?;
.await
.with_context(|| "api server")?;

shut_down(cancel_token, handles, storage).await
}
Expand Down
Loading