-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Creates ko.onError handler #1715
Conversation
Many thanks @harry8525 for the patch. I really think this deserves some attention since error handling is something that could use some ❤️. Just a side note, @harry8525 I noticed a number of places in the PR that are purely stylistic/whitespace and they make it harder to see whether the changes are substantive. Is there an easy way on your end to back out the non-semantic (i.e. purely-stylistic/whitespace) changes? Also, I am noting #1628 and #1659, since they might make it in before this PR and are affected/related. Cheers. |
I'd like to merge this one soon. I'll take care of squashing it into a single commit and fixing the whitespace issues. Any objections? @brianmhunt - if you think anything about this doesn't fit with #1628 or #1659, please let me know. |
Thanks @SteveSanderson ; this would be great to have. I have one concern, being if The first reason is to have a single error handler for the framework, so that it becomes simple for users to capture errors with a predictable convention from every place Knockout might throw them. The second is to future-proof the error handler, in case someone wishes to expand it in future (i.e. by passing more properties). The difference to accommodate this concern is trivial, and would be at https://github.com/knockout/knockout/pull/1715/files#diff-2b4ca49d4bb0a774c4d4c1672d7aa781R343 where it would become: ko.onError && ko.onError({errorCaptured: e, during: "setTimeout"}); Users may have to watch the The #1659 patch should not end up requiring much workaround (though the Does that make sense? Of course, I leave it to you to go either way, but wanted to cover the bases. 😄 |
It's definitely worth thinking through the futureproofness. In this case it looks to me like it will already be OK. The argument passed to I don't have a big problem with wrapping the |
I'm not sure how well the all browsers handle rethrowing custom errors so we should do some tests if we want to go down that path. Aka not sure if the stack is still preserved on rethrow (it should work but you know browsers :)). I agree with Steve that adding extra metadata as additional args seems cleaner to me. RequireJs appends things to the error object but that complicates serialization because sometimes you get weird things like elements and other stuff you were not expecting depending on the error. |
Thanks for the comments. I have nigglies, but I am not sure they ought to be persuasive. Let me write them out, and we will see if there is any substance to them. Instead of wrapping errors one might have custom Error constructors, and maintain the pattern of only-one argument (more on that below). For ease of reference, custom error constructors would probably look a bit like this: function BindingError(spec) {
// Error mucking – see http://stackoverflow.com/a/17891099/19212
this.during = "binding";
this.node = spec.node;
this.bindingContext = spec.bindingContext;
// ...
}
BindingError.prototype = Error.prototype; The problem with a I have a niggly about adding an additional argument. I cannot put my finger on it, and perhaps it is only because I have built up a personal bias towards the Promises pattern of I am not sure these are persuasive or compelling reasons to consider a custom wrapper or otherwise, and I am completely content with any outcome here, but having written it out my sense is that a wrapper has the best “smell” of all the options. |
So are we waiting on other commits or should I fix up the whitespace now? I just want to get this in so we can catch all our errors :) |
😁 @harry8525 – It's in the hands of @SteveSanderson now — I hope I have not derailed this with my chatter above:grey_exclamation: |
OK, I've got this into a shape where I'd be happy to merge it: See #1751 (which supersedes this pull request we are commenting on here) @brianmhunt, about the API and whether errors should be wrapped, this is not something I'm personally concerned about since this approach passes through the original I'd like to merge this soon if possible. |
@SteveSanderson – Let's merge it. As long as the error argument is an object or Thank you! |
Thanks - merged! |
Whoops - unmerged. I see there's still code review feedback to account for. |
... and now re-merged, since I see the code review feedback concludes that there's nothing to change. |
:) Excellent. |
This creates a optional ko.onError callback property that will be called on new async callstacks when errors are hit. This allows us to get usable callstacks in browsers that do not have the stack param on the window.onerror event (e.g. IE 11 and lower).