Skip to content
This repository has been archived by the owner on Mar 25, 2018. It is now read-only.

clarity on asynchronous methods throwing exceptions #124

Closed
agauniyal opened this issue Feb 13, 2017 · 2 comments
Closed

clarity on asynchronous methods throwing exceptions #124

agauniyal opened this issue Feb 13, 2017 · 2 comments

Comments

@agauniyal
Copy link

Under the Node.js v7.5.0 Documentation Errors page, it is mentioned -

A handful of typically asynchronous methods in the Node.js API may still use the throw mechanism to raise exceptions that must be handled using try / catch. There is no comprehensive list of such methods; please refer to the documentation of each method to determine the appropriate error handling mechanism required.

To be honest, I wasn't aware of the following and assumed errors were passed as described just above it via err parameter to the callback function. Would you mind adding such function or its use case via try/catch block since it might create some confusion over its usage.

Reference - StackOverflow.

@bnoordhuis
Copy link
Member

The rule of thumb is this: validation errors (bad inputs) throw exceptions, runtime errors are events.

Say you pass null to a function that expects a string, that's a validation error. Trying to look up the IP address of a non-existing domain name? A runtime error.

In general, when an otherwise asynchronous function throws an exception, it means you need to fix your application because it calls node functions the wrong way.

I'm reasonably sure the documentation mentions the above in a few places but perhaps a more centralized overview is called for.

@sam-github
Copy link

For example, if you call a function, and pass an object as the callback... how could we call the object to pass a TypeError to the callback?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants