Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does lodash.isError need to exist?

Lodash offers a method called _.isError. Why do we need to use that method instead of val instanceof Error?

If you look at the source, you'll see:

    function isError(value) {
      if (!isObjectLike(value)) {
        return false;
      }
      var tag = baseGetTag(value);
      return tag == errorTag || tag == domExcTag ||
        (typeof value.message == 'string' && typeof value.name == 'string' && !isPlainObject(value));
    }

I am not sure what cases this complexity handles that val instanceof Error does not.

like image 661
Nick Heiner Avatar asked Sep 19 '25 06:09

Nick Heiner


1 Answers

This is a bit contrived, but here is where _.isError(val) and val instanceof Error diverge:

function Foo() {
    this.message="a";
    this.name="a";
}

console.log( _.isError(new Foo) ) // true
console.log( new Foo instanceof Error ) // false
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

I cannot say if this is intended behavior on their part, but this is where the lodash method handles extra complexity.

like image 190
swagrov Avatar answered Sep 21 '25 18:09

swagrov