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

--
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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Search Discussions

  • Ben Noordhuis at Jul 2, 2012 at 2:34 pm

    On Mon, Jul 2, 2012 at 4:17 PM, Benjamin Schneider 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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupnodejs @
categoriesnodejs
postedJul 2, '12 at 2:24p
activeJul 2, '12 at 2:34p
posts2
users2
websitenodejs.org
irc#node.js

People

Translate

site design / logo © 2022 Grokbase