You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This crate contains an error handling anti-pattern, specifically in reference to the Handler trait's on_error method. The method is not an associated function, but does not use self and accepts an error type value. This method should be removed, and instead the Result type in theater_types should be a newtype instead of an alias, with more robust error handling methods implemented on it.
For example:
use thiserror;// 1.0.57use tracing;// 0.1.40use std::{error::Error, fmt::Debug};#[derive(thiserror::Error,Debug)]enumMyError{#[error("failed to do... something")]Broken,}#[derive(Debug)]structMyType;/// Similar to `Result` or `Option` for returning values from a handler./// Useful for exiting a handler gracefully on error types that/// would otherwise stop a program.#[derive(Debug)]enumRecover<T>{Value(T),State(ActorState),}/// This type already exists in the Theater crate#[derive(Debug)]enumActorState{Running,// ...}/// This type will replace `theater::Result`structTheaterResult<T,E:Error + Debug>(Result<T,E>);impl<T,E:Error + Debug>TheaterResult<T,E>{/// Logs error types with extra context, converting the error to a recoverable state,/// otherwise forwarding the contained value.////// Based on the `.inspect_err` method in `std::result::Result`, with the/// focus on recovering from handler execution failure.////// ```rust, ignore/// // res represents the returned type of some handler function/// let res: TheaterResult<MyType, MyError> = TheaterResult(Err(MyError::Broken));/// let err = res.with_context("broken for... reasons");/// println!("{err:?}");/// ```fnwith_context(self,context:&str) -> Recover<T>{ifletErr(ref err) = self.0{if !context.is_empty(){
tracing::error!("{context}: {err:?}");}else{
tracing::error!("{err:?}");}Recover::State(ActorState::Running)}else{Recover::Value(self.0.unwrap())}}/// Shorthand for `.with_context()` when no extra context is needed.////// Logs error types, converting the error to a recoverable state,/// otherwise forwarding the contained value.////// Based on the `.inspect_err` method in `std::result::Result`, with the/// focus on recovering from handler execution failure.////// ```rust, ignore/// // res represents the returned type of some handler function/// let res: TheaterResult<MyType, MyError> = TheaterResult(Err(MyError::Broken));/// let err = res.on_err();/// println!("{err:?}");/// ```fnon_err(self) -> Recover<T>{self.with_context("")}}fnmain(){// res represents the returned type of some handler functionlet res:TheaterResult<MyType,MyError> = TheaterResult(Err(MyError::Broken));let err = res.with_context("broken for... reasons");println!("{err:?}");let res:TheaterResult<MyType,MyError> = TheaterResult(Ok(MyType));let value = res.on_err();println!("{value:?}")}
The text was updated successfully, but these errors were encountered:
This crate contains an error handling anti-pattern, specifically in reference to the
Handler
trait'son_error
method. The method is not an associated function, but does not useself
and accepts an error type value. This method should be removed, and instead theResult
type intheater_types
should be a newtype instead of an alias, with more robust error handling methods implemented on it.For example:
The text was updated successfully, but these errors were encountered: