Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6aef01e
seed commit for fatality based errors
drahnr Dec 2, 2021
86cb205
fatality
drahnr Dec 2, 2021
61605f7
first draft of fatality
drahnr Dec 2, 2021
7789e08
cleanup
drahnr Dec 3, 2021
df37cbf
differnt approach
drahnr Dec 3, 2021
8254614
simplify
drahnr Dec 3, 2021
ec8564e
first working version for enums, with documentation
drahnr Dec 3, 2021
a59a2c4
add split
drahnr Jan 21, 2022
bef8549
fix simple split test case
drahnr Jan 21, 2022
226bbb4
extend README.md
drahnr Jan 21, 2022
fb14c73
update fatality impl
drahnr Jan 25, 2022
9a5e3ca
make tests passed
drahnr Jan 25, 2022
3e4dce0
apply fatality to first subsystem
drahnr Jan 26, 2022
ef4a734
fatality fixes
drahnr Jan 28, 2022
50b56ea
use fatality in a subsystem
drahnr Jan 30, 2022
664a1c9
fix subsystemg
drahnr Jan 30, 2022
0db9f15
fixup proc macro
drahnr Jan 30, 2022
a67484e
fix/test: log::*! do not execute when log handler is missing
drahnr Feb 1, 2022
eb6a205
fix spelling
drahnr Feb 1, 2022
d770480
rename Runtime2 to something sane
drahnr Feb 1, 2022
47b2335
allow nested split with `forward` annotations
drahnr Feb 4, 2022
e24e302
add free license
drahnr Feb 4, 2022
3fc6cd4
enable and fixup all tests
drahnr Feb 4, 2022
346fd5b
use external fatality
drahnr Feb 23, 2022
859922a
bump fatality dep
drahnr Feb 23, 2022
6b29441
migrate availability distribution
drahnr Feb 23, 2022
5b00044
more fatality usage
drahnr Feb 23, 2022
ffb66e3
chore: bump fatality to 0.0.6
drahnr Feb 23, 2022
df26cbf
fixup remaining subsystems
drahnr Feb 23, 2022
9466d1c
chore: fmt
drahnr Feb 23, 2022
6ca4079
make cargo spellcheck happy
drahnr Feb 23, 2022
721e1ec
remove single instance of `#[fatal(false)]`
drahnr Feb 24, 2022
3c3adba
last quality sweep
drahnr Feb 25, 2022
c5d7d10
fixup
drahnr Feb 25, 2022
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
Prev Previous commit
Next Next commit
update fatality impl
  • Loading branch information
drahnr committed Feb 25, 2022
commit fb14c73d823bb5ece7f468ef50e7f55e4fbf49a7
2 changes: 2 additions & 0 deletions node/fatality/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
description = "Fatality extension to crate thiserror"
readme = "README.md"

[dependencies]
fatality-proc-macro = { path = "./proc-macro" }
Expand All @@ -12,3 +13,4 @@ thiserror = "1.0"
[dev-dependencies]
trybuild = "1.0"
thiserror = "1.0"
log = "0.4"
15 changes: 8 additions & 7 deletions node/fatality/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and any unknown annotations will be passed to that.

`#[fatality]` currently provides a `trait Fatality` with a single `fn is_fatal(&self) -> bool` by default.

Annotating with `#[fatality(splitabl)]` (which disallows usage of `forward` variant annotations), allows to split the type into two sub-types, a `Jfyi*` and a `Fatal*` one via `fn split(self) -> Result<Self::Jfyi, Self::Fatal>`.
Annotating with `#[fatality(splitable)]` (which disallows usage of `forward` variant annotations), allows to split the type into two sub-types, a `Jfyi*` and a `Fatal*` one via `fn split(self) -> Result<Self::Jfyi, Self::Fatal>`.


The derive macro implements them, and can defer calls, based on `thiserror` annotations, specifically
Expand Down Expand Up @@ -56,19 +56,20 @@ enum Yikes {
Dead,
}

fn foo() -> Result<(), Yikes> {
fn foo() -> Result<[u8;32], Yikes> {
Err(Yikes::Dead)
}

fn i_call_foo() -> Result<(), FatalYikes> {
// TODO: not yet implemented
let x = foo().fatal_or_log(|jfyi| { log::warn!(..) })?;
// availble via a convenience trait `Nested` that is implemented
// for any `Result` whose error type implements `Split`.
let x: Result<[u8;32], Jfyi> = foo().into_nested()?;
}

fn i_call_foo_too() -> Result<(), FatalYikes> {
// implemented
if let Err(e) = foo() {
log::warn!("Jfyi: {:?}", e.split()?);
if let Err(jfyi_and_fatal_ones) = foo() {
// bail if bad, otherwise just log it
log::warn!("Jfyi: {:?}", jfyi_and_fatal_ones.split()?);
}
}
```
Expand Down
2 changes: 2 additions & 0 deletions node/fatality/proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ fn fatality_gen(attr: Attr, item: ItemEnum) -> Result<TokenStream, syn::Error> {
Ok(ts)
}

/// The declaration of `#[fatality(splitable)]` or `#[fatality]`
/// outside the `enum YourError`.
#[derive(Clone, Copy)]
enum Attr {
Splitable(kw::splitable),
Expand Down
24 changes: 23 additions & 1 deletion node/fatality/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ pub use thiserror;

/// Determine the fatality of an error.
pub trait Fatality: std::error::Error + std::fmt::Debug {
/// Returns `true` if the error variant is _fatal_
/// or `false` if it is more of a informational error.
fn is_fatal(&self) -> bool;
}

/// Spl
/// Allows to split an error into two types - a fatal
/// and a informational enum error type, that can be further consumed.
pub trait Split: std::error::Error + std::fmt::Debug {
type Jfyi: ::std::error::Error + Send + Sync + 'static;
type Fatal: ::std::error::Error + Send + Sync + 'static;
Expand All @@ -38,6 +41,25 @@ pub trait Split: std::error::Error + std::fmt::Debug {
fn split(&self) -> Result<Self::Jfyi, Self::Fatal>;
}

/// Converts a flat, yet `splitable` error into a nested `Result<Result<_,Jfyi>, Fatal>`
/// error type.
pub trait Nested<T, E: Split> {
/// Convert into a nested error rather than a flat one, commonly for direct handling.
fn into_nested(self) -> Result<Result<T, <E as Split>::Jfyi>, <E as Split>::Fatal> {
match self {
Ok(t) => Ok(t),
Err(e) => match e.split() {
Ok(jfyi) => Ok(Err(jfyi)),
Err(fatal) => Err(fatal),
},
}
}
}

impl<T, E: Split> Nested<T, E> for std::result::Result<T, E> {
// uses the default impl
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down