-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add ohno::Unimplemented error type #83
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
Draft
Vaiz
wants to merge
12
commits into
main
Choose a base branch
from
u/vaiz/2025/11/27/unimplemented
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5dd2cbc
add unimplemented error type
Vaiz 0fc375d
add example
Vaiz cd7dd6f
docs
Vaiz 1ed65f9
docs
Vaiz 25e39e3
Update crates/ohno/src/unimplemented.rs
Vaiz 489d196
clippy
Vaiz 60c7519
fmt
Vaiz e9f32b7
remove should_panic
Vaiz c40014c
fix code blocks
Vaiz 5592732
improve coverage
Vaiz 25eefa7
use u32 for line
Vaiz 5198e7d
missed one spot
Vaiz 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
Some comments aren't visible on the classic Files Changed page.
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
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,27 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #![expect(clippy::unwrap_used, reason = "example code")] | ||
|
|
||
| use ohno::{Unimplemented, unimplemented_error}; | ||
|
|
||
| #[ohno::error] | ||
| #[from(Unimplemented)] | ||
| pub struct MyError; | ||
|
|
||
| fn do_something(is_lucky: bool) -> Result<(), MyError> { | ||
| if is_lucky { | ||
| Ok(()) | ||
| } else { | ||
| unimplemented_error!("this feature is not yet implemented"); | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| let err = do_something(false).unwrap_err(); | ||
| println!("Error: {err}"); | ||
| } | ||
|
|
||
| // Output: | ||
| // Error: not implemented at crates\ohno\examples\unimplemented.rs:11 | ||
Vaiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // caused by: this feature is not yet implemented | ||
Vaiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,194 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //! `Unimplemented` error type. | ||
| use std::borrow::Cow; | ||
|
|
||
| use crate::OhnoCore; | ||
|
|
||
| /// Error type for unimplemented functionality. | ||
| /// | ||
| /// This type is designed to replace panicking macros like [`todo!`] and | ||
| /// [`unimplemented!`] with a proper error that can be handled gracefully. | ||
| /// | ||
| /// See the documentation for the [`unimplemented_error!`](crate::unimplemented_error!) macro for | ||
| /// more details. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// use ohno::{Unimplemented, unimplemented_error}; | ||
| /// | ||
| /// fn not_ready_yet() -> Result<(), Unimplemented> { | ||
| /// unimplemented_error!("this feature is coming soon") | ||
| /// } | ||
Vaiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// ``` | ||
| #[derive(crate::Error, Clone)] | ||
Vaiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #[no_constructors] | ||
| #[display("not implemented at {file}:{line}")] | ||
| pub struct Unimplemented { | ||
| file: Cow<'static, str>, | ||
| line: u32, | ||
| core: OhnoCore, | ||
| } | ||
|
|
||
| impl Unimplemented { | ||
| /// Creates a new `Unimplemented` error. | ||
| #[must_use] | ||
| pub fn new(file: Cow<'static, str>, line: u32) -> Self { | ||
| Self { | ||
| file, | ||
| line, | ||
| core: OhnoCore::new(), | ||
| } | ||
| } | ||
|
|
||
| /// Creates a new `Unimplemented` error with a custom message. | ||
| /// | ||
| /// The message provides additional context about why the functionality | ||
| /// is not yet implemented or what needs to be done. | ||
| #[must_use] | ||
| pub fn with_message(message: impl Into<Cow<'static, str>>, file: Cow<'static, str>, line: u32) -> Self { | ||
| Self { | ||
| file, | ||
| line, | ||
| core: OhnoCore::from(message.into()), | ||
| } | ||
| } | ||
|
|
||
| /// Returns the file path where this error was created. | ||
| #[must_use] | ||
| pub fn file(&self) -> &str { | ||
| &self.file | ||
| } | ||
|
|
||
| /// Returns the line number where this error was created. | ||
| #[must_use] | ||
| pub fn line(&self) -> u32 { | ||
Vaiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.line | ||
| } | ||
| } | ||
|
|
||
| /// Returns an [`Unimplemented`] error from the current function. | ||
| /// | ||
| /// This macro is designed to replace panicking macros like [`todo!`] and | ||
| /// [`unimplemented!`] with a proper error that can be handled gracefully. | ||
| /// It automatically captures the file and line information and returns early | ||
| /// with an `Unimplemented` error. | ||
| /// | ||
| /// Unlike the standard panicking macros, this allows your application to: | ||
| /// | ||
| /// - Continue running and handle the error appropriately | ||
| /// - Log the error with full context (file, line, message) | ||
| /// - Return meaningful error responses to users instead of crashing | ||
| /// - Test error paths without triggering panics | ||
| /// | ||
| /// To prevent accidental use of panicking macros, enable these clippy lints: | ||
| /// | ||
| /// ```toml | ||
| /// [workspace.lints.clippy] | ||
| /// todo = "deny" | ||
| /// unimplemented = "deny" | ||
| /// ``` | ||
| /// | ||
| /// The error can be automatically converted into any error type that implements | ||
| /// `From<Unimplemented>`, making it easy to use in functions with different | ||
| /// error types. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// Basic usage without a message: | ||
| /// | ||
| /// ``` | ||
| /// # use ohno::unimplemented_error; | ||
| /// fn future_feature() -> Result<String, ohno::Unimplemented> { | ||
| /// unimplemented_error!() | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// With a custom message: | ||
| /// | ||
| /// ``` | ||
| /// # use ohno::unimplemented_error; | ||
| /// fn experimental_api() -> Result<(), ohno::Unimplemented> { | ||
| /// unimplemented_error!("async runtime support not yet available") | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// Automatic conversion to custom error types: | ||
| /// | ||
Vaiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// ``` | ||
| /// # use ohno::{unimplemented_error, Unimplemented}; | ||
| /// #[ohno::error] | ||
| /// #[from(Unimplemented)] | ||
| /// struct AppError; | ||
| /// | ||
| /// fn app_function() -> Result<(), AppError> { | ||
| /// unimplemented_error!("feature coming in v2.0") | ||
| /// } | ||
| /// ``` | ||
| #[macro_export] | ||
| macro_rules! unimplemented_error { | ||
| () => { | ||
| return Err($crate::Unimplemented::new(::std::borrow::Cow::Borrowed(file!()), line!()).into()) | ||
| }; | ||
| ($ex:expr) => { | ||
| return Err($crate::Unimplemented::with_message($ex, ::std::borrow::Cow::Borrowed(file!()), line!()).into()) | ||
| }; | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use ohno::ErrorExt; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn basic() { | ||
| fn return_err() -> Result<(), Unimplemented> { | ||
| unimplemented_error!() | ||
| } | ||
| let err = return_err().unwrap_err(); | ||
| assert!(err.message().starts_with("not implemented at "), "{err}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn with_message() { | ||
| fn return_err() -> Result<(), Unimplemented> { | ||
| unimplemented_error!("custom message") | ||
| } | ||
|
|
||
| let err = return_err().unwrap_err(); | ||
| let message = err.message(); | ||
| assert!(message.starts_with("not implemented at "), "{message}"); | ||
| assert!(message.contains("custom message"), "{message}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn file_and_line() { | ||
| let err = Unimplemented::new("file.rs".into(), 111); | ||
| assert_eq!(err.file(), "file.rs"); | ||
| assert_eq!(err.line(), 111); | ||
| } | ||
|
|
||
| #[test] | ||
| fn automatic_conversion() { | ||
| #[derive(Debug)] | ||
| struct CustomError(Unimplemented); | ||
|
|
||
| impl From<Unimplemented> for CustomError { | ||
| fn from(err: Unimplemented) -> Self { | ||
| Self(err) | ||
| } | ||
| } | ||
|
|
||
| fn return_custom_err() -> Result<(), CustomError> { | ||
| unimplemented_error!() | ||
| } | ||
|
|
||
| let err = return_custom_err().unwrap_err(); | ||
| let message = err.0.message(); | ||
| assert!(message.starts_with("not implemented at "), "{message}"); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.