Last active
August 29, 2015 14:03
-
-
Save cwharris/8d1df4f05574f4dae5a3 to your computer and use it in GitHub Desktop.
Observable.tagError: Basically to help manually debug JavaScript applications by using locator tags. The locator tags are prepended to the error as it travel to the consumer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rx.Observable | |
.concat( | |
[ | |
Rx.Observable.return('{ "value": 0 }'), | |
Rx.Observable.return('{ "value": 5 }'), | |
Rx.Observable.return("{") | |
] | |
) | |
.tagError('Source') | |
.tagError('ParseJSON', function (source) { | |
return source.map(function (s) { return JSON.parse(s); }); | |
}) | |
.map(function (obj) { return 2 / obj.value; }) | |
.subscribe( | |
console.log.bind(console, ' onNext ->'), | |
console.log.bind(console, ' onError ->'), | |
console.log.bind(console, 'onCompleted ->') | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rx.Observable | |
.concat( | |
[ | |
Rx.Observable.return('{ "value": 0 }'), | |
Rx.Observable.return('{ "value": 5 }'), | |
Rx.Observable.return("{") | |
] | |
) | |
.tagError('Source') | |
.map(function (s) { return JSON.parse(s); }) | |
.tagError('ParseJSON') | |
.map(function (obj) { return 2 / obj.value; }) | |
.subscribe( | |
console.log.bind(console, ' onNext ->'), | |
console.log.bind(console, ' onError ->'), | |
console.log.bind(console, 'onCompleted ->') | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Error (message) { | |
this.message = message; | |
this.tags = []; | |
} | |
Rx.Observable.prototype.tagError = function (tag, selector) { | |
return selector | |
? selector(this).tagError(tag) | |
: this.catchException(function (error) { | |
var isError = error instanceof Error; | |
if (!isError) { | |
error = new Error(error); | |
} | |
error.tags.unshift(tag); | |
return Rx.Observable.throw(error); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment