generated from fast/template
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: impl std::error::Error for Frame #29
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
Merged
+190
−0
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f407f57
feat: impl std::error::Error for Frame
andylokandy 148952e
fix
andylokandy 0133758
fix
andylokandy e74edc6
fix
andylokandy 13dc3ae
fix
andylokandy 3320628
fix
andylokandy c85d8d0
add example
andylokandy 1c08e36
Merge branch 'main' into into-error
tisonkun 81c693b
update changelog
andylokandy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright 2025 FastLabs Developers | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! # Anyhow Interoperate Example - Returning `anyhow::Result<_>` | ||
| //! | ||
| //! This example shows a common pattern: | ||
| //! - Using `exn::Result<T, E>` internally. | ||
| //! - At the boundary, convert `Exn<E>` into `anyhow::Error`. | ||
|
|
||
| use std::error::Error; | ||
|
|
||
| use derive_more::Display; | ||
| use exn::Result; | ||
| use exn::ResultExt; | ||
|
|
||
| fn main() -> anyhow::Result<()> { | ||
| app::run().map_err(convert_error)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn convert_error<E: Error + Send + Sync + 'static>(err: exn::Exn<E>) -> anyhow::Error { | ||
| anyhow::Error::from_boxed(err.into()) | ||
| } | ||
|
|
||
| mod app { | ||
| use super::*; | ||
|
|
||
| pub fn run() -> Result<(), AppError> { | ||
| let port = config::load_port().or_raise(|| AppError)?; | ||
| let _ = port; | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[derive(Debug, Display)] | ||
| #[display("failed to start app")] | ||
| pub struct AppError; | ||
| impl Error for AppError {} | ||
| } | ||
|
|
||
| mod config { | ||
| use super::*; | ||
|
|
||
| pub fn load_port() -> Result<u16, ConfigError> { | ||
| let raw = "not-a-number"; | ||
|
|
||
| let port = raw | ||
| .parse::<u16>() | ||
| .or_raise(|| ConfigError(format!("PORT must be a number; got {raw:?}")))?; | ||
|
|
||
| Ok(port) | ||
| } | ||
|
|
||
| #[derive(Debug, Display)] | ||
| pub struct ConfigError(String); | ||
| impl Error for ConfigError {} | ||
| } | ||
|
|
||
| // Output when running `cargo run -p examples --example into-anyhow`: | ||
| // | ||
| // Error: failed to start app | ||
| // | ||
| // Caused by: | ||
| // 0: PORT must be a number; got "not-a-number" | ||
| // 1: invalid digit found in string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright 2025 FastLabs Developers | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! # std::error::Error Interoperate Example - Returning `Result<_, Box<dyn std::error::Error>>` | ||
| //! | ||
| //! This example shows a common pattern: | ||
| //! - Using `exn::Result<T, E>` internally. | ||
| //! - At the boundary, convert `Exn<E>` into `Box<dyn std::error::Error>`. | ||
|
|
||
| use std::error::Error; | ||
|
|
||
| use derive_more::Display; | ||
| use exn::Result; | ||
| use exn::ResultExt; | ||
|
|
||
| fn main() -> std::result::Result<(), Box<dyn Error>> { | ||
| app::run()?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| mod app { | ||
| use super::*; | ||
|
|
||
| pub fn run() -> Result<(), AppError> { | ||
| let port = config::load_port().or_raise(|| AppError)?; | ||
| let _ = port; | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[derive(Debug, Display)] | ||
| #[display("failed to start app")] | ||
| pub struct AppError; | ||
| impl Error for AppError {} | ||
| } | ||
|
|
||
| mod config { | ||
| use super::*; | ||
|
|
||
| pub fn load_port() -> Result<u16, ConfigError> { | ||
| let raw = "not-a-number"; | ||
|
|
||
| let port = raw | ||
| .parse::<u16>() | ||
| .or_raise(|| ConfigError(format!("PORT must be a number; got {raw:?}")))?; | ||
|
|
||
| Ok(port) | ||
| } | ||
|
|
||
| #[derive(Debug, Display)] | ||
| pub struct ConfigError(String); | ||
| impl Error for ConfigError {} | ||
| } | ||
|
|
||
| // Output when running `cargo run -p examples --example into-std-error`: | ||
| // | ||
| // Error: failed to start app, at examples/src/into-std-error.rs:36:40 | ||
| // | | ||
| // |-> PORT must be a number; got "not-a-number", at examples/src/into-std-error.rs:55:14 | ||
| // | | ||
| // |-> invalid digit found in string, at examples/src/into-std-error.rs:55:14 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.