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
Implement Error and DOMException. Add patterns for error handling. #89
Conversation
This looks great! A few comments:
|
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.
So far this is looking really solid!
Thanks a lot!
Err(CreationError::SyntaxError(_)) => (), | ||
v => panic!("expected SyntaxError, got {:?}", v), | ||
} | ||
} |
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.
These two tests should be web_test
s. (That is - they should be in a mod
protected by #[cfg(all(test, feature = "web_test"))]
so that the tests don't fail when they're being run under nodejs.)
|
||
// To safely cast from a reference to a specific DomException, we must check that the object is an | ||
// instance of DOMException, and that the error name matches. | ||
impl<T: ConcreteException + ::webcore::value::FromReferenceUnchecked> ::webcore::value::FromReference for T { |
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.
Very minor stylistic nitpick: use
-ing these two would probably be nicer.
($name:ident) => { | ||
impl ::std::fmt::Display for $name { | ||
fn fmt(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { | ||
write!(formatter, "{}: {}", stringify!($name), self.message()) |
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.
Instead of stringify!($name)
wouldn't it be better to print out self.name()
? (That way errors upcast into a base type would still show the actual type.)
assert_eq!(error.message(), "foo"); | ||
|
||
let mut text = String::new(); | ||
write!(&mut text, "{}", error).unwrap(); |
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.
Can't you just do let text = format!("{}", error);
here? (:
Since the comments I've made are pretty minor and this in general looks pretty solid (and I want to release a new Thanks a lot @Herschel! |
I've had this floating around on my hard drive for a bit -- there is room for improvement, feedback requested!
Addresses #28. Methods that throw an error were using an ad-hoc
js!
block to catch the error and transfer it back. However, this gets difficult when a method returns multiple error types. This PR adds macros to make error handling more consistent and easy.js_try!
macro that works likejs!
, but also catches errors and convert them to aResult
type.TryFrom<Value>
being implemented for both the value and error type.DOMException
, these have the same type in JS, but different names. For the concrete exception types in Rust,FromReference
andTryFrom
is implemented to check that the name matches.js_try!
isn't optimal, with lots of calls back and forth to JS. I expect this could be done much better, perhaps by adding serialization support for a Result type. I was more concerned with establishing the pattern of how the errors should be caught on the Rust side; the macro can be improved.js!
macro,js_try!
does atry_into
internally.error_enum_boilerplate!
macro is used to implementTryFrom<Value>
for the enum.js_try!
macro is used to return errors from these APIs:Node::replace_child
,Node::insert_before
WebSocket::new
andWebSocket::close_with_status
(thanks @Diggsey!)Things left for a future PR:
IError
trait?).Result<_, failure::Error>
instead of defining an enum-per-throwing-function. But I think it's nice to know explicitly what errors a function might throw.