`util.isError` uses duck typing
See original GitHub issueI’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:
- Created 7 years ago
- Reactions:1
- Comments:7
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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!
@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)