nodejs error handling

  • https://www.joyent.com/node-js/production/design/errors
  • error vs. exception
    • An error is any instance of the Error class. Errors may be constructed and then passed directly to another function or thrown.
    • When you throw an error, it becomes an exception.2Here’s an example of using an error as an exception:
      throw new Error('something bad happened');

      but you can just as well use an Error without throwing it:

      callback(new Error('something bad happened'));
    • and this is much more common in Node because most errors are asynchronous
  • try/catch
    • the only commonly-used case where you’d use try/catch is JSON.parse and other user-input validation functions
  • operational error vs. program error
    • operational error
      • run-time problems experienced by correctly-written programs. These are not bugs in the program. In fact, these are usually problems with something else: the system itself (e.g., out of memory or too many open files), the system’s configuration (e.g., no route to a remote host), the network (e.g., socket hang-up), or a remote service (e.g., a 500 error, failure to connect, or the like). Examples include:
        • failed to connect to server
        • failed to resolve hostname
        • invalid user input
        • request timeout
        • server returned a 500 response
        • socket hang-up
        • system is out of memory
    • program error
      • bugs in the program. These are things that can always be avoided by changing the code
    • failure to handle an operational error is itself a programmer error.
      • For example, if a program tries to connect to a server but it gets an ECONNREFUSED error, and it hasn’t registered a handler for the socket’s 'error' event, then the program will crash, and that’s a programmer error
    • The distinction between operational errors and programmer errors is the foundation for figuring out how to deliver errors and how to handle them
Advertisements
This entry was posted in Nodejs. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s