Skip to content

Commit 49812f6

Browse files
committed
Fix error wrapping chapter
1 parent 5b3197b commit 49812f6

File tree

1 file changed

+11
-64
lines changed

1 file changed

+11
-64
lines changed

content/error-wrapping.md

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,6 @@ To see how `CanWrapError` works in practice, we can redefine `LoadJsonConfig` to
350350
# fn config_path(&self) -> &PathBuf;
351351
# }
352352
#
353-
# #[cgp_component {
354-
# provider: ErrorWrapper,
355-
# }]
356-
# pub trait CanWrapError<Detail>: HasErrorType {
357-
# fn wrap_error(error: Self::Error, detail: Detail) -> Self::Error;
358-
# }
359-
#
360353
pub struct LoadJsonConfig;
361354

362355
impl<Context> ConfigLoader<Context> for LoadJsonConfig
@@ -430,13 +423,7 @@ So we can implement an error wrapper provider for `anyhow::Error` as follows:
430423
# use core::fmt::Display;
431424
#
432425
# use cgp::prelude::*;
433-
#
434-
# #[cgp_component {
435-
# provider: ErrorWrapper,
436-
# }]
437-
# pub trait CanWrapError<Detail>: HasErrorType {
438-
# fn wrap_error(error: Self::Error, detail: Detail) -> Self::Error;
439-
# }
426+
# use cgp::core::error::ErrorWrapper;
440427
#
441428
pub struct WrapWithAnyhowContext;
442429

@@ -524,13 +511,6 @@ type as follows:
524511
# fn config_path(&self) -> &PathBuf;
525512
# }
526513
#
527-
# #[cgp_component {
528-
# provider: ErrorWrapper,
529-
# }]
530-
# pub trait CanWrapError<Detail>: HasErrorType {
531-
# fn wrap_error(error: Self::Error, detail: Detail) -> Self::Error;
532-
# }
533-
#
534514
pub struct LoadJsonConfig;
535515

536516
pub struct ErrLoadJsonConfig<'a, Context> {
@@ -627,13 +607,7 @@ as follows:
627607
# use core::fmt::Debug;
628608
#
629609
# use cgp::prelude::*;
630-
#
631-
# #[cgp_component {
632-
# provider: ErrorWrapper,
633-
# }]
634-
# pub trait CanWrapError<Detail>: HasErrorType {
635-
# fn wrap_error(error: Self::Error, detail: Detail) -> Self::Error;
636-
# }
610+
# use cgp::core::error::ErrorWrapper;
637611
#
638612
pub struct WrapWithAnyhowDebug;
639613

@@ -691,21 +665,14 @@ pub mod traits {
691665
pub trait HasConfigPath {
692666
fn config_path(&self) -> &PathBuf;
693667
}
694-
695-
#[cgp_component {
696-
provider: ErrorWrapper,
697-
}]
698-
pub trait CanWrapError<Detail>: HasErrorType {
699-
fn wrap_error(error: Self::Error, detail: Detail) -> Self::Error;
700-
}
701668
}
702669

703670
pub mod impls {
704671
use core::fmt::{Debug, Display};
705672
use std::path::PathBuf;
706673
use std::{fs, io};
707674

708-
use cgp::core::error::{ErrorRaiser, ProvideErrorType};
675+
use cgp::core::error::{ErrorRaiser, ErrorWrapper,ProvideErrorType};
709676
use cgp::prelude::*;
710677
use serde::Deserialize;
711678

@@ -819,7 +786,7 @@ pub mod contexts {
819786
use std::path::PathBuf;
820787

821788
use cgp::core::component::UseDelegate;
822-
use cgp::core::error::{ErrorRaiserComponent, ErrorTypeComponent};
789+
use cgp::core::error::{ErrorRaiserComponent, ErrorWrapperComponent, ErrorTypeComponent};
823790
use cgp::prelude::*;
824791
use serde::Deserialize;
825792

@@ -888,15 +855,12 @@ we can also make use of the `UseDelegate` pattern to implement delegated error w
888855
```rust
889856
# extern crate cgp;
890857
#
858+
# use core::marker::PhantomData;
859+
#
891860
# use cgp::prelude::*;
892-
# use cgp::core::component::UseDelegate;
861+
# use cgp::core::error::ErrorWrapper;
893862
#
894-
# #[cgp_component {
895-
# provider: ErrorWrapper,
896-
# }]
897-
# pub trait CanWrapError<Detail>: HasErrorType {
898-
# fn wrap_error(error: Self::Error, detail: Detail) -> Self::Error;
899-
# }
863+
# pub struct UseDelegate<Components>(pub PhantomData<Components>);
900864
#
901865
impl<Context, Detail, Components> ErrorWrapper<Context, Detail> for UseDelegate<Components>
902866
where
@@ -925,6 +889,7 @@ to different error wrappers, similar to how we dispatch the error raisers based
925889
# use std::path::PathBuf;
926890
#
927891
# use cgp::core::component::UseDelegate;
892+
# use cgp::core::error::ErrorWrapper;
928893
# use cgp::prelude::*;
929894
#
930895
# #[cgp_component {
@@ -948,32 +913,14 @@ to different error wrappers, similar to how we dispatch the error raisers based
948913
# pub trait HasConfigPath {
949914
# fn config_path(&self) -> &PathBuf;
950915
# }
951-
#
952-
# #[cgp_component {
953-
# provider: ErrorWrapper,
954-
# }]
955-
# pub trait CanWrapError<Detail>: HasErrorType {
956-
# fn wrap_error(error: Self::Error, detail: Detail) -> Self::Error;
957-
# }
958-
#
959-
# impl<Context, Detail, Components> ErrorWrapper<Context, Detail> for UseDelegate<Components>
960-
# where
961-
# Context: HasErrorType,
962-
# Components: DelegateComponent<Detail>,
963-
# Components::Delegate: ErrorWrapper<Context, Detail>,
964-
# {
965-
# fn wrap_error(error: Context::Error, detail: Detail) -> Context::Error {
966-
# Components::Delegate::wrap_error(error, detail)
967-
# }
968-
# }
969916
# }
970917
#
971918
# pub mod impls {
972919
# use core::fmt::{Debug, Display};
973920
# use std::path::PathBuf;
974921
# use std::{fs, io};
975922
#
976-
# use cgp::core::error::{ErrorRaiser, ProvideErrorType};
923+
# use cgp::core::error::{ErrorRaiser, ErrorWrapper, ProvideErrorType};
977924
# use cgp::prelude::*;
978925
# use serde::Deserialize;
979926
#
@@ -1099,7 +1046,7 @@ to different error wrappers, similar to how we dispatch the error raisers based
10991046
# use std::path::PathBuf;
11001047
#
11011048
# use cgp::core::component::UseDelegate;
1102-
# use cgp::core::error::{ErrorRaiserComponent, ErrorTypeComponent};
1049+
# use cgp::core::error::{ErrorRaiserComponent, ErrorWrapperComponent, ErrorTypeComponent};
11031050
# use cgp::prelude::*;
11041051
# use serde::Deserialize;
11051052
#

0 commit comments

Comments
 (0)