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
Add hook for ko.onBindingError #1628
base: master
Are you sure you want to change the base?
Conversation
I like it, makes the error handlers you referenced in #1626 much easier to write. |
Thanks @voiceofwisdom ... My thinking too is that there are lots of folks using error reporters like (from https://plus.google.com/+PaulIrish/posts/12BVL5exFJn):
Plus any number of variants the in-house. Writing a custom binding provider is a somewhat ominous barrier to work with these services, especially if you're new to Knockout. I glean from their number that there is some popularity to them. |
@mbest, @rniemeyer @SteveSanderson — any thoughts on this PR? |
I think this is good functionality. A custom binding provider is harder to jump into than a hook like this one. |
A couple extra features:
Tested on Chrome, Safari, Firefox, and PhantomJS. |
|
||
try { |
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.
Is this "outer" try
necessary if we have the "inner" try
below inside the ko.dependentObservable
?
Thank you @rniemeyer ! @mbest, @SteveSanderson - I am tagging this for 3.3 - even though I expect it would not make it in. This PR seems a fairly straightforward patch, would be handy for lots of folks, and it would be good to get it into the "stream" for release (expecting it'll be 3.4 or later, but great if it were good enough for 3.3). The one concern I have is that errors will now be thrown when I would be very grateful for thoughts on how “mergeable” this looks. Cheers |
@brianmhunt, could you let us know what (if anything) you think we should still do here, in light of #1715 having been merged now? |
@SteveSanderson – I've been mulling it. I am guessing the easiest way to preserve the functionality would be to directly attach the metadata to the error instances. We just have to be wary of clobbering anything currently or possibly in the future native to the Error, but the spec for Errors is pretty bare. We could just change the ko.onBindingError = function(error, spec) {
var bindingText;
ko.utils.extend(error, spec);
if (spec.bindingKey) {
// During: 'init' or initial 'update'
bindingText = ko.bindingProvider['instance']['getBindingsString'](spec.element);
error.message = "Unable to process binding \"" + spec.bindingKey +
"\" in binding \"" + bindingText + "\"\nMessage: " + error.message;
} // else: During: 'apply'
if (ko['onError']) {
ko['onError'](error);
} else {
throw error;
}
} Does that sound – roughly – like a reasonable tact? Incidentally, I noted that |
This PR should exposes a function (
ko.onBindingError
) to hook into binding errors.This should give greater control to users handling cases such as #1626
The
function onBindingError(spec)
takes one object,spec
containing the following properties:init
orupdate
orapply
)init
orupdate
only)init
orupdate
only)init
orupdate
only)$data
,$rawData
,$root
, ...)Tested on PhantomJS, Chrome, Safari, and Firefox.