Creating your own errors

Chad Austin chad at
Tue Aug 6 14:59:06 PDT 2013

On Tue, Aug 6, 2013 at 1:02 PM, Allen Wirfs-Brock <allen at>wrote:

> Has anybody ever actually seen a JS exception handler that really needs to
> take conditional action depending upon whether as TypeError or RangeError
> was thrown?

For what it's worth, as someone using both promises and custom error types
in the field, we do switch on different types of errors.  We alternate
between instanceof and testing the .name property on the exception as a
stylistic choice.

Here's a common idiom (note, I typed this from memory, and haven't compiled

fetchServiceResult().then(function(result) {
    // use result
}).catch(function(error) {
    if (error instanceof NetworkError) {
        // perhaps retry a couple times
        // perhaps show a dialog box
    } else {
        throw error; // bubble anything else
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list