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

Implement ErrorWrapper on generic ErrorRaiser providers #54

Merged
merged 3 commits into from
Jan 7, 2025
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## v0.3.0 (pre-release)

- Implement `ErrorWrapper` on generic `ErrorRaiser` providers - [#54](https://github.com/contextgeneric/cgp/pull/54)
- Implement `ErrorWrapper` for the following providers: `DebugError`, `DisplayError`,
`DebugAnyhowError`, `DisplayAnyhowError`, `RaiseAnyhowError`,
`DebugEyreError`, `DisplayEyreError`, `RaiseEyreError`,
`DebugBoxedStdError`, `DisplayBoxedStdError`.

- Reorganize crate exports - [#53](https://github.com/contextgeneric/cgp/pull/53)
- Move generic error providers to the `cgp-error-extra` crate.
- Add an `alloc` feature to `cgp-error-extra` to enable use of `alloc` in providers.
Expand Down
13 changes: 12 additions & 1 deletion crates/cgp-error-anyhow/src/impls/debug_error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use core::fmt::Debug;

use alloc::format;
use anyhow::{anyhow, Error};
use cgp_core::error::ErrorRaiser;
use cgp_core::error::{ErrorRaiser, ErrorWrapper};
use cgp_core::prelude::*;

pub struct DebugAnyhowError;
Expand All @@ -15,3 +16,13 @@ where
anyhow!("{:?}", e)
}
}

impl<Context, Detail> ErrorWrapper<Context, Detail> for DebugAnyhowError
where
Context: HasErrorType<Error = Error>,
Detail: Debug,
{
fn wrap_error(error: Error, detail: Detail) -> Error {
error.context(format!("{detail:?}"))
}
}
13 changes: 12 additions & 1 deletion crates/cgp-error-anyhow/src/impls/display_error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use alloc::string::ToString;
use core::fmt::Display;

use anyhow::{anyhow, Error};
use cgp_core::error::ErrorRaiser;
use cgp_core::error::{ErrorRaiser, ErrorWrapper};
use cgp_core::prelude::*;

pub struct DisplayAnyhowError;
Expand All @@ -15,3 +16,13 @@ where
anyhow!("{e}")
}
}

impl<Context, Detail> ErrorWrapper<Context, Detail> for DisplayAnyhowError
where
Context: HasErrorType<Error = Error>,
Detail: Display,
{
fn wrap_error(error: Error, detail: Detail) -> Error {
error.context(detail.to_string())
}
}
13 changes: 12 additions & 1 deletion crates/cgp-error-anyhow/src/impls/raise_anyhow_error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use core::error::Error as StdError;
use core::fmt::Display;

use anyhow::Error;
use cgp_core::error::ErrorRaiser;
use cgp_core::error::{ErrorRaiser, ErrorWrapper};
use cgp_core::prelude::*;

pub struct RaiseAnyhowError;
Expand All @@ -15,3 +16,13 @@ where
e.into()
}
}

impl<Context, Detail> ErrorWrapper<Context, Detail> for RaiseAnyhowError
where
Context: HasErrorType<Error = Error>,
Detail: Display + Send + Sync + 'static,
{
fn wrap_error(error: Error, detail: Detail) -> Error {
error.context(detail)
}
}
3 changes: 3 additions & 0 deletions crates/cgp-error-anyhow/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![no_std]

extern crate alloc;

mod impls;

pub use anyhow::Error;
pub use impls::{DebugAnyhowError, DisplayAnyhowError, RaiseAnyhowError, UseAnyhowError};
12 changes: 11 additions & 1 deletion crates/cgp-error-extra/src/impls/alloc/debug_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::format;
use alloc::string::String;
use core::fmt::Debug;

use cgp_error::{CanRaiseError, ErrorRaiser};
use cgp_error::{CanRaiseError, CanWrapError, ErrorRaiser, ErrorWrapper};

pub struct DebugError;

Expand All @@ -15,3 +15,13 @@ where
Context::raise_error(format!("{e:?}"))
}
}

impl<Context, Detail> ErrorWrapper<Context, Detail> for DebugError
where
Context: CanWrapError<String>,
Detail: Debug,
{
fn wrap_error(error: Context::Error, detail: Detail) -> Context::Error {
Context::wrap_error(error, format!("{detail:?}"))
}
}
17 changes: 13 additions & 4 deletions crates/cgp-error-extra/src/impls/alloc/display_error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use alloc::format;
use alloc::string::String;
use alloc::string::{String, ToString};
use core::fmt::Display;

use cgp_error::{CanRaiseError, ErrorRaiser};
use cgp_error::{CanRaiseError, CanWrapError, ErrorRaiser, ErrorWrapper};

pub struct DisplayError;

Expand All @@ -12,6 +11,16 @@ where
E: Display,
{
fn raise_error(e: E) -> Context::Error {
Context::raise_error(format!("{e}"))
Context::raise_error(e.to_string())
}
}

impl<Context, Detail> ErrorWrapper<Context, Detail> for DisplayError
where
Context: CanWrapError<String>,
Detail: Display,
{
fn wrap_error(error: Context::Error, detail: Detail) -> Context::Error {
Context::wrap_error(error, detail.to_string())
}
}
13 changes: 12 additions & 1 deletion crates/cgp-error-eyre/src/impls/debug_error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::fmt::Debug;

use cgp_core::error::ErrorRaiser;
use alloc::format;
use cgp_core::error::{ErrorRaiser, ErrorWrapper};
use cgp_core::prelude::*;
use eyre::{eyre, Error};

Expand All @@ -15,3 +16,13 @@ where
eyre!("{:?}", e)
}
}

impl<Context, Detail> ErrorWrapper<Context, Detail> for DebugEyreError
where
Context: HasErrorType<Error = Error>,
Detail: Debug,
{
fn wrap_error(error: Error, detail: Detail) -> Error {
error.wrap_err(format!("{detail:?}"))
}
}
13 changes: 12 additions & 1 deletion crates/cgp-error-eyre/src/impls/display_error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::string::ToString;
use core::fmt::Display;

use cgp_core::error::ErrorRaiser;
use cgp_core::error::{ErrorRaiser, ErrorWrapper};
use cgp_core::prelude::*;
use eyre::{eyre, Error};

Expand All @@ -15,3 +16,13 @@ where
eyre!("{e}")
}
}

impl<Context, Detail> ErrorWrapper<Context, Detail> for DisplayEyreError
where
Context: HasErrorType<Error = Error>,
Detail: Display,
{
fn wrap_error(error: Error, detail: Detail) -> Error {
error.wrap_err(detail.to_string())
}
}
13 changes: 12 additions & 1 deletion crates/cgp-error-eyre/src/impls/raise_eyre_error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::error::Error as StdError;
use core::fmt::Display;

use cgp_core::error::ErrorRaiser;
use cgp_core::error::{ErrorRaiser, ErrorWrapper};
use cgp_core::prelude::*;
use eyre::Error;

Expand All @@ -15,3 +16,13 @@ where
e.into()
}
}

impl<Context, Detail> ErrorWrapper<Context, Detail> for RaiseEyreError
where
Context: HasErrorType<Error = Error>,
Detail: Display + Send + Sync + 'static,
{
fn wrap_error(error: Error, detail: Detail) -> Error {
error.wrap_err(detail)
}
}
3 changes: 3 additions & 0 deletions crates/cgp-error-eyre/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![no_std]

extern crate alloc;

mod impls;

pub use eyre::Error;
pub use impls::{DebugEyreError, DisplayEyreError, RaiseEyreError, UseEyreError};
16 changes: 15 additions & 1 deletion crates/cgp-error-std/src/impls/debug_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use alloc::boxed::Box;
use alloc::format;
use core::fmt::Debug;

use cgp_core::error::{ErrorRaiser, HasErrorType};
use cgp_core::error::{ErrorRaiser, ErrorWrapper, HasErrorType};

use crate::types::{Error, StringError};
use crate::WrapError;

pub struct DebugBoxedStdError;

Expand All @@ -17,3 +18,16 @@ where
Box::new(StringError::from(format!("{e:?}")))
}
}

impl<Context, Detail> ErrorWrapper<Context, Detail> for DebugBoxedStdError
where
Context: HasErrorType<Error = Error>,
Detail: Debug,
{
fn wrap_error(error: Error, detail: Detail) -> Error {
Box::new(WrapError {
detail: format!("{detail:?}"),
source: error,
})
}
}
20 changes: 17 additions & 3 deletions crates/cgp-error-std/src/impls/display_error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use alloc::boxed::Box;
use alloc::format;
use alloc::string::ToString;
use core::fmt::Display;

use cgp_core::error::{ErrorRaiser, HasErrorType};
use cgp_core::error::{ErrorRaiser, ErrorWrapper, HasErrorType};

use crate::types::{Error, StringError};
use crate::WrapError;

pub struct DisplayBoxedStdError;

Expand All @@ -14,6 +15,19 @@ where
E: Display,
{
fn raise_error(e: E) -> Error {
Box::new(StringError::from(format!("{e}")))
Box::new(StringError::from(e.to_string()))
}
}

impl<Context, Detail> ErrorWrapper<Context, Detail> for DisplayBoxedStdError
where
Context: HasErrorType<Error = Error>,
Detail: Display,
{
fn wrap_error(error: Error, detail: Detail) -> Error {
Box::new(WrapError {
detail: detail.to_string(),
source: error,
})
}
}
2 changes: 1 addition & 1 deletion crates/cgp-error-std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ mod impls;
mod types;

pub use impls::{DebugBoxedStdError, DisplayBoxedStdError, RaiseBoxedStdError, UseBoxedStdError};
pub use types::{Error, StringError};
pub use types::{Error, StringError, WrapError};
2 changes: 2 additions & 0 deletions crates/cgp-error-std/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod error;
mod string;
mod wrap;

pub use error::Error;
pub use string::StringError;
pub use wrap::WrapError;
29 changes: 29 additions & 0 deletions crates/cgp-error-std/src/types/wrap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use core::error::Error as StdError;
use core::fmt::{Debug, Display};

use alloc::string::String;

use crate::Error;

pub struct WrapError {
pub detail: String,
pub source: Error,
}

impl Display for WrapError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}: {}", self.detail, self.source)
}
}

impl Debug for WrapError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Display::fmt(self, f)
}
}

impl StdError for WrapError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(self.source.as_ref())
}
}
13 changes: 7 additions & 6 deletions crates/cgp-error/src/traits/can_raise_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ use crate::traits::has_error_type::HasErrorType;
#[cgp_component {
provider: ErrorRaiser
}]
pub trait CanRaiseError<E>: HasErrorType {
fn raise_error(e: E) -> Self::Error;
pub trait CanRaiseError<SourceError>: HasErrorType {
fn raise_error(error: SourceError) -> Self::Error;
}

impl<Context, Error, Components, Delegate> ErrorRaiser<Context, Error> for UseDelegate<Components>
impl<Context, SourceError, Components, Delegate> ErrorRaiser<Context, SourceError>
for UseDelegate<Components>
where
Context: HasErrorType,
Components: DelegateComponent<Error, Delegate = Delegate>,
Delegate: ErrorRaiser<Context, Error>,
Components: DelegateComponent<SourceError, Delegate = Delegate>,
Delegate: ErrorRaiser<Context, SourceError>,
{
fn raise_error(e: Error) -> Context::Error {
fn raise_error(e: SourceError) -> Context::Error {
Delegate::raise_error(e)
}
}
Loading