From 8c66b98bca6ed0a2990903fe8e0ea72def5c7be8 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 21 Aug 2022 14:19:07 +0200 Subject: refactor!: change errors to be more sane BREAKING CHANGE: Major improvements have been made to error types and the error_stack crate is no longer used --- src/errors/di_container.rs | 75 +++++++++++++++++++++++++++++++--------------- src/errors/injectable.rs | 41 ++++++++++++++++--------- 2 files changed, 78 insertions(+), 38 deletions(-) (limited to 'src/errors') diff --git a/src/errors/di_container.rs b/src/errors/di_container.rs index 127676f..ed05a5e 100644 --- a/src/errors/di_container.rs +++ b/src/errors/di_container.rs @@ -1,34 +1,61 @@ -//! Error types for the DI container. +//! Error types for [`DIContainer`] and it's related structs. +//! +//! [`DIContainer`]: crate::di_container::DIContainer -use std::fmt; -use std::fmt::{Display, Formatter}; +use crate::errors::injectable::InjectableError; -use error_stack::Context; - -/// Error for when the DI container fails to do something. -#[derive(Debug)] -pub struct DIContainerError; - -impl Display for DIContainerError +/// Error type for [`DIContainer`]. +/// +/// [`DIContainer`]: crate::di_container::DIContainer +#[derive(thiserror::Error, Debug)] +pub enum DIContainerError { - fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result + /// Unable to cast a binding for a interface. + #[error("Unable to cast binding for interface '{0}'")] + CastFailed(&'static str), + + /// Wrong binding type. + #[error("Wrong binding type for interface '{interface}'. Expected a {expected}. Found a {found}")] + WrongBindingType { - fmt.write_str("A DI container error has occurred") - } -} + /// The affected bound interface. + interface: &'static str, -impl Context for DIContainerError {} + /// The expected binding type. + expected: &'static str, -/// Error for when the binding builder fails to do something. -#[derive(Debug)] -pub struct BindingBuilderError; + /// The found binding type. + found: String, + }, -impl Display for BindingBuilderError -{ - fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result + /// Failed to resolve a binding for a interface. + #[error("Failed to resolve binding for interface '{interface}'")] + BindingResolveFailed { - fmt.write_str("A binding builder error has occurred") - } + /// The reason for the problem. + #[source] + reason: InjectableError, + + /// The affected bound interface. + interface: &'static str, + }, + + /// No binding exists for a interface. + #[error("No binding exists for interface '{0}'")] + BindingNotFound(&'static str), } -impl Context for BindingBuilderError {} +/// Error type for [`BindingBuilder`]. +/// +/// [`BindingBuilder`]: crate::di_container::BindingBuilder +#[derive(thiserror::Error, Debug)] +pub enum BindingBuilderError +{ + /// A binding already exists for a interface. + #[error("Binding already exists for interface '{0}'")] + BindingAlreadyExists(&'static str), + + /// Resolving a singleton failed. + #[error("Resolving the given singleton failed")] + SingletonResolveFailed(#[from] InjectableError), +} diff --git a/src/errors/injectable.rs b/src/errors/injectable.rs index 63afa11..4b9af96 100644 --- a/src/errors/injectable.rs +++ b/src/errors/injectable.rs @@ -1,20 +1,33 @@ -//! Error types for structs implementing Injectable. +#![allow(clippy::module_name_repetitions)] +//! Error types for structs that implement [`Injectable`]. +//! +//! [`Injectable`]: crate::interfaces::injectable::Injectable -use core::fmt; -use std::fmt::{Display, Formatter}; +use super::di_container::DIContainerError; -use error_stack::Context; +/// Error type for structs that implement [`Injectable`]. +/// +/// [`Injectable`]: crate::interfaces::injectable::Injectable +#[derive(thiserror::Error, Debug)] +pub enum InjectableError +{ + /// Failed to resolve dependencies. + #[error("Failed to resolve a dependency of '{affected}'")] + ResolveFailed + { + /// The reason for the problem. + #[source] + reason: Box, -/// Error for when a injectable struct fails to be resolved. -#[derive(Debug)] -pub struct ResolveError; + /// The affected injectable type. + affected: &'static str, + }, -impl Display for ResolveError -{ - fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result + /// Detected circular dependencies. + #[error("Detected circular dependencies. {dependency_trace}")] + DetectedCircular { - fmt.write_str("Failed to resolve injectable struct") - } + /// A visual trace of dependencies. + dependency_trace: String, + }, } - -impl Context for ResolveError {} -- cgit v1.2.3-18-g5258