Skip to content

Commit

Permalink
fix: add ErrorLevel trait to specify error log levels (#405)
Browse files Browse the repository at this point in the history
Add ErrorLevel trait to specify error log levels
  • Loading branch information
Alexandcoats authored Jul 11, 2022
1 parent f35d103 commit 3cc1cac
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 19 deletions.
3 changes: 3 additions & 0 deletions bin/inx-chronicle/src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use axum::{
extract::rejection::{ExtensionRejection, QueryRejection, TypedHeaderRejection},
response::IntoResponse,
};
use chronicle::runtime::ErrorLevel;
use hyper::{header::InvalidHeaderValue, StatusCode};
use mongodb::bson::document::ValueAccessError;
use serde::Serialize;
Expand Down Expand Up @@ -89,6 +90,8 @@ impl ApiError {
}
}

impl ErrorLevel for ApiError {}

impl<T: Into<InternalApiError>> From<T> for ApiError {
fn from(err: T) -> Self {
ApiError::Internal(err.into())
Expand Down
11 changes: 10 additions & 1 deletion bin/inx-chronicle/src/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use async_trait::async_trait;
use chronicle::{
db::MongoDb,
runtime::{Actor, ActorContext, ActorError, HandleEvent, Report, RuntimeError},
runtime::{Actor, ActorContext, ActorError, ErrorLevel, HandleEvent, Report, RuntimeError},
};
use clap::Parser;
use thiserror::Error;
Expand All @@ -26,6 +26,15 @@ pub enum LauncherError {
Runtime(#[from] RuntimeError),
}

impl ErrorLevel for LauncherError {
fn level(&self) -> log::Level {
match self {
LauncherError::Config(_) | LauncherError::MongoDb(_) => log::Level::Error,
LauncherError::Runtime(_) => log::Level::Warn,
}
}
}

#[derive(Debug)]
/// Supervisor actor
pub struct Launcher;
Expand Down
15 changes: 14 additions & 1 deletion bin/inx-chronicle/src/stardust_inx/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use chronicle::types::tangle::MilestoneIndex;
use chronicle::{runtime::ErrorLevel, types::tangle::MilestoneIndex};
use thiserror::Error;

#[derive(Debug, Error)]
Expand All @@ -27,3 +27,16 @@ pub enum InxError {
#[error(transparent)]
Tonic(#[from] inx::tonic::Error),
}

impl ErrorLevel for InxError {
fn level(&self) -> log::Level {
match self {
Self::InvalidAddress(_)
| Self::InxTypeConversion(_)
| Self::MongoDb(_)
| Self::NetworkChanged(_, _)
| Self::ParsingAddressFailed(_) => log::Level::Error,
_ => log::Level::Warn,
}
}
}
5 changes: 3 additions & 2 deletions src/runtime/actor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ pub(crate) mod sender;
/// Module containing utilities.
pub(crate) mod util;

use std::{borrow::Cow, error::Error};
use std::borrow::Cow;

use async_trait::async_trait;
use futures::StreamExt;

use self::context::ActorContext;
use super::error::ErrorLevel;

/// The actor trait, which defines a task that is managed by the runtime.
#[async_trait]
pub trait Actor: Send + Sync + Sized {
/// Custom data that is passed to all actor methods.
type State: Send;
/// Custom error type that is returned by all actor methods.
type Error: Error + Send;
type Error: ErrorLevel + Send;

/// Set this actor's name, primarily for debugging purposes.
fn name(&self) -> Cow<'static, str> {
Expand Down
24 changes: 16 additions & 8 deletions src/runtime/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@ pub enum RuntimeError {
AbortedScope(ScopeId),
#[error("Actor exited with error: {0}")]
ActorError(String),
#[error("Dependency notification canceled")]
CanceledDepNotification,
#[error("Invalid scope")]
InvalidScope,
#[error("Missing dependency: {0}")]
MissingDependency(String),
#[error("Error launching scope: {0}")]
ScopeLaunchError(Box<dyn Error + Send + Sync>),
#[error(transparent)]
SendError(#[from] SendError),
#[error("Task exited with error: {0}")]
TaskError(Box<dyn Error + Send + Sync>),
}

/// Defines an error's log level.
pub trait ErrorLevel: Error {
/// Returns the log level for this error.
fn level(&self) -> log::Level {
log::Level::Error
}
}

impl ErrorLevel for RuntimeError {
fn level(&self) -> log::Level {
log::Level::Warn
}
}

impl ErrorLevel for std::convert::Infallible {}
2 changes: 1 addition & 1 deletion src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use self::{
Actor,
},
config::{ConfigureActor, SpawnConfig},
error::RuntimeError,
error::{ErrorLevel, RuntimeError},
scope::{RuntimeScope, ScopeView},
task::{error::TaskError, report::TaskReport, Task},
};
Expand Down
9 changes: 5 additions & 4 deletions src/runtime/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use super::{
},
Sender,
};
use crate::runtime::merge::Merge;
use crate::runtime::{error::ErrorLevel, merge::Merge};

/// A view into a particular scope which provides the user-facing API.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -236,7 +236,8 @@ impl RuntimeScope {
Ok(())
}
Err(e) => {
log::warn!(
log::log!(
e.level(),
"{} exited with error: {}",
format!("Task {:x}", child_scope.id().as_fields().0),
e
Expand Down Expand Up @@ -279,7 +280,7 @@ impl RuntimeScope {
Ok(())
}
Err(e) => {
log::warn!("{} exited with error: {}", actor.name(), e);
log::log!(e.level(), "{} exited with error: {}", actor.name(), e);
let err_str = e.to_string();
supervisor_addr.send(Report::Error(ErrorReport::new(
actor,
Expand Down Expand Up @@ -320,7 +321,7 @@ impl RuntimeScope {
Ok(res) => match res {
Ok(_) => Ok(()),
Err(e) => {
log::warn!("{} exited with error: {}", actor.name(), e);
log::log!(e.level(), "{} exited with error: {}", actor.name(), e);
Err(RuntimeError::ActorError(e.to_string()))
}
},
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
pub(super) mod error;
pub(super) mod report;

use std::{borrow::Cow, error::Error};
use std::borrow::Cow;

use async_trait::async_trait;

use super::error::ErrorLevel;

/// The task trait, which defines a task that is managed by the runtime and requires little to no
/// external comunication.
#[async_trait]
pub trait Task: Send + Sync + Sized {
/// Custom error type that is returned by all task methods.
type Error: Error + Send;
type Error: ErrorLevel + Send;

/// Set this task's name, primarily for debugging purposes.
fn name(&self) -> Cow<'static, str> {
Expand Down

0 comments on commit 3cc1cac

Please sign in to comment.