Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You can do it using monadic augmentation. Or aspect oriented programming.

In plain english, make a function that takes two functions makeADoThingFunctionThatHandlesErrors(doThing, handleError)

and returns a new version of the function that is internally wrapped in a try catch block with the handleError function getting called from the "catch" block. This is relatively straightforward to accomplish using closures/high order functions. This is one utility function you write once.

Finally you can do this with a whole "monadic" api such as jquery- create a function that loops through all the methods on an object prototype and wraps them in an error handler, returns a new version of the prototype you can inherit from.

Or instead of doing all that, you just use some library with promises (such as Q, or jQuery) that implements all the above, and all you need to do is write a single error handler function, and any error that gets thrown by any function in a promise chain gets caught and sent to that function.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: