Lodash _.isError() method
What you’ll learn
- How
_.isError(value)spots Error instances across common subclasses. - Why plain objects with
message/namekeys fail the check. - How lodash reasoning differs from bare
instanceof Errorin edge realms. - When DOM-style exceptions participate in the same utility.
Prerequisites
Basic familiarity with JavaScript exceptions (try/catch) and the Error type hierarchy.
- You understand constructors (
Error) versus instances (new Error()). - Try-it labs load lodash from the CDN.
Overview
Use _.isError inside lodash-heavy pipelines when APIs claim they reject with Errors but might leak strings, POJO payloads, or framework wrappers.
Tagged natives
Leverages internal object tags for reliable classification.
Reject impostors
Plain objects mimicking message/name rarely qualify.
Broader than manual checks
Centralizes quirks around DOMException-style hosts.
Syntax
_.isError(value) - value: any value to test.
- Returns:
trueifvaluelooks like an Error/DOMException-like host per lodash rules; otherwisefalse.
Error and TypeError instances
Built-in subclasses remain identifiable after construction.
import isError from "lodash/isError";
console.log(
"genericErr: " + isError(new Error("oops")) + "\n" + // true
"typeErr: " + isError(new TypeError("bad")) // true
); Plain objects and strings masquerading as errors
Serializable shapes and stack-looking strings do not adopt Error tagging.
import isError from "lodash/isError";
console.log(
"fakePlain: " + isError({ message: "fail", name: "Error" }) + "\n" + // false
"errString: " + isError("Error: fail") // false
); EvalError versus the Error constructor
Instances pass; the constructor function itself is not an error object.
import isError from "lodash/isError";
console.log(
"evalErr: " + isError(new EvalError("bad eval")) + "\n" + // true
"errorCtor: " + isError(Error) // false
); 📋 _.isError vs related checks
| API | Matches |
|---|---|
_.isError(x) | Error-shaped tagged objects (plus lodash fallback rules). |
x instanceof Error | Often equivalent, but can fail across realms or exotic prototypes. |
typeof x === "object" && x && "message" in x | Too permissive—accepts plain POJOs. |
_.isString(x) | Useful when APIs mistakenly reject string stacks instead of Errors. |
Pitfalls to avoid
JSON loses prototypes
Parsed API payloads become plain objects—even when fields mimic Errors.
Wrapped failures
Some libraries attach cause chains or aggregate arrays—still Errors, but inspect metadata separately.
Error constructor misuse
Never confuse Error (function) with new Error() (instance).
❓ FAQ
Summary
- Purpose: classify unknown rejection reasons as real Error hosts.
- Reject: strings and plain-object impersonators lacking Error tagging.
- Next: explore more on Lodash _.isFinite().
_.isError uses internal tags ([object Error], [object DOMException]) plus a guarded fallback for objects that expose string message and name fields yet cannot be classified as lodash plain objects—so copying keys onto {} is typically rejected.
6 people found this page helpful
