Creating your own errors

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


On Tue, Aug 6, 2013 at 1:02 PM, Allen Wirfs-Brock <allen at wirfs-brock.com>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
it:

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: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130806/01c6af24/attachment.html>


More information about the es-discuss mailing list