-
Notifications
You must be signed in to change notification settings - Fork 821
Improve callback error handling #211
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
Conversation
- Add 'original' parameter to the 'cordovacallbackerror' event so that users can view the actual original error.
- This is extremely important so that users can get the stack trace for logging and debugging.
- Removed the console logs.
- They were redundant since the error is printed when it is thrown (if not caught).
- console.log's without an option to turn them off can be annoying.
… corresponds to pull request apache/cordova-js#211
|
Created corresponding pull request with documentation here: |
|
@Lindsay-Needs-Sleep Thanks a lot for your contribution and sorry for the late response! This does look like a good addition to me. I'll try to give this PR a thorough review in the following days. If I take too long, please remind me. Maybe in the meantime @janpio can say something about the mobile spec failures. He might know a bit about that, IIRC. |
- Maintain mirrored module structure between src/ and test/ - Simplify test case
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 took the liberty to make a few adjustments to the test. I hope you are OK with them. Thanks again!
IMO, this would be good to merge. I'd like a second opinion though, if possible.
src/cordova.js
Outdated
| console && console.log && console.log(msg); | ||
| console && console.log && err.stack && console.log(err.stack); | ||
| cordova.fireWindowEvent('cordovacallbackerror', { 'message': msg }); | ||
| cordova.fireWindowEvent('cordovacallbackerror', { 'message': msg, 'original': err }); |
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.
Would the property name original sound better if it is error, stack, or errorStack.
Once the property name is set and used, I am not sure if it would be changed it in the future if we find the name to be confusing.
People could build custom tooling that relays on this new field name and changing would break their tools.
Just want to make sure the meaning of the property carries over.
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've had similar concerns. I think error would be a more meaningful property name.
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.
Good point.
This got me thinking. What do you think of this instead?
err.message = msg;
corodva.fireWindowEvent(“cordovacallbackerror”, err);
This maintains backwards compatibility, but makes the error the top-level response.
I think this makes a lot more sense because most people will probably think the object they receive in cordovacallbackerror will be an error. (At least that is what I originally thought.)
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.
That might be even better. What do you think @erisu?
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'm assuming in this context err is an Error object (since it comes from the catch block), assigning err.message would overwrite the original error message yes? If so, I don't think that's a good idea as that could be hiding important information for debugging. Or am I missing something?
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.
@breautek Yes, you are correct. Though it is technically only prepending the error message with additional information.
In the main comment stream, @raphinesse and I have been discussing this a bit. (It is still up in the air). If you would like to, please take a look and share your thoughts on the proposals.
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.
Though it is technically only prepending the error message with additional information.
Ahh I see, that's where the + err comes from. I think this is fine.
On the topic of using error vs original. +1 on error
Using original might make sense in the context of cordovacallbackerror but when you're debugging or something you just have a reference of the object (not necessary where it is coming from), the property name original isn't too meaningful of what the property is. I think using error as a property name is much more clear.
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.
(Using github from mobile kind of messed me up, ignore this!)
|
Actually, I think something like this would be the nicest/cleanest option: Pro’s:
Cons
Reasons to do this despite the con:
Thoughts? @erisu @raphinesse |
|
Well, the message is not exactly clobbered. We only prepend a string. Which might be helpful for someone who doesn't know what's going on. Adding |
True, fair point. My first thought was that we shouldn’t prepend the message incase some one was depending on the error message having an exact message. (But that it almost the exact same thing I was saying we shouldn’t worry too much about :p) But, the modified message is slightly more likely to cause an inconvenience to future people who expect an unmodified error message. This is possibly a slight concern because this error is picked by window.onerror (which quite a few more people use I assume). But this is still minor. Since we have the stack trace included in the error, the first file reference will take you directly to where the error came from (which will be inside your success/error callback). (You can see that in my attached screenshots). This makes the majority of the prepended message redundant.
But I am fine with this as well if my points didn’t sway you. :p
To further decrease our odds of accidentally clobbering someone’s custom error property (unlikely, but better to be a bit safer I think), we could do this instead as well:
|
|
@Lindsay-Needs-Sleep Your points about messing with user's objects (in this case their errors) are good ones. I think with Cordova being a library, we should be as defensive as possible here. To me, that would mean:
|
|
I changed the "original" parameter name to "error" as suggested. |
|
@Lindsay-Needs-Sleep Thanks again for this PR. 👍 |
|
And thank you all for the feedback! |
… corresponds to pull request apache/cordova-js#211
… corresponds to pull request apache/cordova-js#211
Platforms affected
All
Motivation and Context
Problems solved:
cordovacallbackerroreventDescription
Improve error handling.
Testing
npm testRan mobile spec before and after.
Manual testing.
window.addEventListener("cordovacallbackerror", function (err) {before:

after:

mobilespec:

Checklist
(platform)if this change only applies to one platform (e.g.(android))