-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Consolidate errors for context missing #3441
Changes from 16 commits
eefa4f6
a7b29ee
5a45db8
68ce4af
ed42e6e
6f90e12
8b26582
bf2d2fc
61616ad
1f90f0e
eef23e6
9cf7fbe
98b50b1
3126cfd
b2d8eb6
18a6c13
5d67039
b1f4558
e62d4dd
c707db0
a1bbf25
bb9530b
a10b386
d6af014
37f613a
25a5894
37d8d8a
47ff6ef
7602e03
0e6f644
d0d7751
7abf22c
3c34b95
8adddb1
c0a7f63
d88b63d
9a9244e
f4598e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ cfg_rt! { | |
/// panicking. | ||
pub(crate) fn current() -> Self { | ||
crate::runtime::context::time_handle() | ||
.expect("there is no timer running, must be called from the context of Tokio runtime") | ||
.expect("time must be enabled on the Tokio 1.x context") | ||
aknuds1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
@@ -71,8 +71,7 @@ cfg_not_rt! { | |
/// lazy, and so outside executed inside the runtime successfuly without | ||
/// panicking. | ||
pub(crate) fn current() -> Self { | ||
panic!("there is no timer running, must be called from the context of Tokio runtime or \ | ||
`rt` is not enabled") | ||
panic!("`rt` must be enabled") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be using the standardized error message, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Darksonn Isn't the difference here that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, here the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Darksonn but you're suggesting this error message should change? I'm not following. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you provide a code change suggestion, if you have something in mind? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. panic!(context_missing_error(&["rt"])); In fact, you do not currently use the list argument anywhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Darksonn OK, I figured it was kind of pointless to produce an error saying that there's no reactor running since the runtime isn't compiled in in the first place (due to the "rt" feature being disabled). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are free to pick another wording, but "rt must be enabled" is too short. It needs to mention that this is a Tokio feature, including the Tokio version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Darksonn Please see my updated error string. |
||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::fmt::Write; | ||
|
||
/// Returns an error string explaining that the Tokio context hasn't been instantiated. | ||
pub(crate) fn context_missing_error(features: &[&str]) -> String { | ||
// TODO: Include Tokio version | ||
let sfx = if !features.is_empty() { | ||
let mut sfx = String::from(" with "); | ||
for (i, feat) in features.iter().enumerate() { | ||
if i == 0 { | ||
if features.len() > 1 { | ||
write!(&mut sfx, "either ").expect("failed to write to string"); | ||
} | ||
} else if i == features.len() - 1 { | ||
write!(&mut sfx, " or ").expect("failed to write to string"); | ||
} else { | ||
write!(&mut sfx, ", ").expect("failed to write to string"); | ||
} | ||
write!(&mut sfx, "{}", feat).expect("failed to write to string"); | ||
} | ||
write!(&mut sfx, " enabled").expect("failed to write to string"); | ||
sfx | ||
} else { | ||
String::new() | ||
}; | ||
format!( | ||
"there is no reactor running, must be called from the context of a Tokio 1.x runtime{}", | ||
sfx | ||
) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_context_missing_error_no_features() { | ||
assert_eq!( | ||
&context_missing_error(&[]), | ||
"there is no reactor running, must be called from the context of a Tokio 1.x runtime" | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_context_missing_error_one_feature() { | ||
assert_eq!(&context_missing_error(&["rt"]), | ||
"there is no reactor running, must be called from the context of a Tokio 1.x runtime with rt enabled"); | ||
} | ||
|
||
#[test] | ||
fn test_context_missing_error_two_features() { | ||
assert_eq!(&context_missing_error(&["rt", "signal"]), | ||
"there is no reactor running, must be called from the context of a Tokio 1.x runtime with either rt or signal enabled"); | ||
} | ||
|
||
#[test] | ||
fn test_context_missing_error_three_features() { | ||
assert_eq!(&context_missing_error(&["rt", "signal", "sync"]), | ||
"there is no reactor running, must be called from the context of a Tokio 1.x runtime with either rt, signal or sync enabled" | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error should check whether there is a runtime context or not. If there is, it should fail with an error saying that there is a runtime, but IO is disabled on it. Otherwise it should fail with the same error as the other panics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Darksonn I made
runtime::context::io_handle()
fail with the standard error if the Tokio context isn't instantiated, does this address your concern or should it be implemented differently?If
runtime::context::io_handle()
shouldn't panic in case of missing Tokio context, the return value has to differentiate between missing context and io being disabled.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't tell there being any technical drawback to have
runtime::context::io_handle()
panic if the Tokio context is missing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should panic in both cases, but the error messages should be different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Darksonn I'm wondering if there might be a misunderstanding; the way I've implemented it, there are different error messages depending on whether the Tokio context is missing or io isn't enabled on it.
I implemented it so that
runtime::context::io_handle
will panic if the context is missing. Otherwise,io::driver::Handle::current
will panic if the context doesn't have io enabled.