I usually have have something like this at the top of my callbacks.
function mycallback (err, data) {
if (err) return onerror(err)
dosomestuffwith(data)
}
`onerror` is usually a callback passed by express, or something I write in a
utilities file / at the top of the file.
Tim.
On Thu, Jan 10, 2013 at 02:26:00AM -0800, Stuart P. Bentley wrote:
> Hey guys, new(ish) to Node, first time posting. I made an npm
> account shortly after I started for programming, but I haven't
> written anything that really called for a reusable npm package until
> now.
>
> So, the code I'm writing doesn't use any flow-control/promise-chain
> libraries like seq or step or q or first or async or queue-async
> (although I have used the last one a little). However, it still has
> a few callbacks, and it rarely (if ever) cares about what to do if
> there's an error (other than panic, of course).
>
> Anyway, to avoid endless "if(err){console.error(err)} else {"
> typing, I found myself copy-pasting something like this at the top
> of all my scripts to wrap all my callbacks in:
>
> function errorHandlingCb(errCb){
> return function onSuccess(successCb){
> return function(err) {
> if (err) {
> errCb(err);
> } else {
> successCb.apply(this,Array.prototype.slice.call(arguments,1));
> }
> }
> }
>
> (or some variant on the onSuccess function with console.error or
> "throw err")
>
> Eventually I started writing something with its functionality spread
> across files, got tired of having the same 7-9 lines in multiple
> files in one project (let alone across multiple projects, and
> decided I'd turn it into a module:
>
> https://github.com/stuartpb/catcher/blob/master/index.js
>
> I reset my npm password (which I'd unsurprisingly forgotten) and
> published it:
>
> https://npmjs.org/package/catcher
>
> What do you think? I don't know if I'm replicating some existing
> thing, or if I've overlooked something, or made some assumption I
> shouldn't.
>
> Here are some ideas I have for 0.2:
>
> - "errorize" any non-Error strings / objects (to amerliorate the
> problems detailed in
> http://www.devthought.com/2011/12/22/a-string-is-not-an-error/) by
> replacing line 5 with this:
>
> if (err instanceof Error) ecb(err);
> else ecb(new Error(err));
>
> - dropp throwNewErr (in light of the above change)
> - New functions oriented toward different combinations of wrapping
> the method and error handler for calling the method with the success
> callback, rather than just wrapping the error handler, then the
> success callback, then using that in the method
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en