question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`util.isError` uses duck typing

See original GitHub issue

I’m working with Bluebird 3.4.0.

I’ve noticed that util.isError uses various property checks on the error to decide whether the thing you rejected the promise with is an error.

js/release/util.js, lines 225-230:

function isError(obj) {
    return obj !== null &&
           typeof obj === "object" &&
           typeof obj.message === "string" &&
           typeof obj.name === "string";
}

Is there any reason why it doesn’t allow for obj being an instance of Error with alternative property types? The reason I ask is that we use a custom error type that extends Error, and we often use an object as the message, and as things currently are, we get a warning: (node:9482) Warning: a promise was rejected with a non-error: [object Object]

Perhaps the following would be a reasonable change:

function isError(obj) {
    return obj instanceof Error || (
           obj !== null &&
           typeof obj === "object" &&
           typeof obj.message === "string" &&
           typeof obj.name === "string"
    );
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

1reaction
ktraincommented, Jun 7, 2016

@benjamingr In this case, the custom error type being thrown signifies an input/missing data error, not something unexpected that should end up logged in New Relic. It’s used for communication between modules and middleware. If something really crazy were to happen, I’d throw a regular Error.

@spion Yes - that’s going to be my next approach since it looks like the util.isError is written with good purpose. Just wanted to float the idea first and see what the decision was behind its implementation.

Thanks for the great feedback!

0reactions
benjamingrcommented, Jun 7, 2016

@ktrain ECMAScript errors have string .message properties - it’s on Error#prototype and in the spec. If I made my own array subclass that defined .length as an object I don’t think it would work in a lot of places.

I don’t think tooling that recognizes and monitors errors (like NewRelic, app insights etc) would recognize your errors.

(Also, my opinion is that stack traces are very important for writing debuggable code, but that’s just my personal opinion)

Read more comments on GitHub >

github_iconTop Results From Across the Web

%Status Values | InterSystems IRIS Data Platform 2022.2
IsError () methods, which are especially convenient in the Terminal (where you cannot use macros). To display the error details, use $SYSTEM.OBJ.
Read more >
checking for typeof error in JS - javascript - Stack Overflow
However, duck-typing may produce false positives if you have non-error objects that contain stack and message properties. Share.
Read more >
How do you use duck typing in javascript without always ...
I know javascript uses duck typing and at first I thought this would make polymorphism easy compared to strongly typed languages like C#....
Read more >
Creating custom Error classes in Node.js - gists · GitHub
The best solution is just to duck-type. See http://gunargessner.com/subclassing-exception/.
Read more >
JavaScript Data Type Check Cheatsheet - Code with Hugo
The moral of the story is: don't check that something is an object, check that it has the properties you need (that's what's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found