The funny thing is that while I see many examples of people handling error
handling in their callback functions, I can't find any examples of people
actually writing any error handling in their systems. This is an example of
the pattern that I cribbed (a bit) from java.
class CallbackException
constructor: (@parameters, @message) ->
toString: ->
return "CallbackException message: #{@message}, parameters:
#{@parameters}"
##############################################################################
getUser = (userId, callback) ->
# We pretend we have an array of users for simplicity here, so that we can
# handwave over other error handling code here.
user = users[userId]
if user is undefined
err = new exceptions.CallbackException {"userId": userId}, "No User
Exists"
return callback err
callback null, user
##############################################################################
exports.getUser = (req, res) ->
id = req.params.id
user = getUser id, (exception, data) ->
if (exception)
res.render 'user.html',
locals:
exception: exception
else
res.render 'user.html',
locals:
user: data
Note that while I could use the error prototype here, there doesn't seem to
be a reason to, especially because I'm not conforming to the interface, and
this code isn't being thrown. Further, pretty much any object can be thrown
in JS. Now, the advantage to extending error is to get yourself a stack
trace, but again that doesn't seem to be relevant unless the code is
actually thrown, rather than returned.
What patterns do you use for error handling?
--
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