On Mon, Jul 2, 2012 at 4:17 PM, Benjamin Schneider
<[email protected]> wrote:
> I am currently writing a node js library. Now I came up with the question
> what would be the best approach to indicate a usage error by the programmer.
>
> For example if I got the following function in my public API:
>
>     function doSomething(aNumber) {
>         // ...
>     }
>
> and I need to make sure that aNumber actually is a number, what way of error
> handling should I go if the API user passes a parameter that is not.
>
> By convetion functions in node should return an Error object if an error
> occurs. But in my case this is no error the programmer should catch.
>
> So my question is what would be the most appropriate way to maybe simply
> "end" the program with an error message, telling the programmer that he did
> not use my API in the correct way.

The rule of thumb is to throw an exception when it's a programmer
error and emit an error event when it's a run-time error (like a
network outage).

In your example, you'd throw a TypeError because the programmer forgot
to check her inputs.

-- 
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

Reply via email to