Node: Error convenience subclasses

Created on 31 Aug 2017  路  3Comments  路  Source: nodejs/node

The string new errors.TypeError('ERR_INVALID_OPT_VALUE', repeats almost 30 times in our code base.

I think it would be nice if we had an InvalidOptionValue subclass to deal with these specifically.

I'm afraid it might have impact on users explicitly checking the name string of errors - but that seems a lot less common than a instanceof TypeError that would still pass and we can override .name explicitly. We can also make it a factory and not a class so it doesn't affect it:

function InvalidOptionValue(name, value) {
  return new errors.TypeError('ERR_INVALID_OPT_VALUE', name, value); 
}

What do you think?

discuss errors

Most helpful comment

As much as I dislike duplicated code, I'd have to agree with @BridgeAR and @mscdex. The added layer of abstraction just ends up hiding the details. (It also ends up adding an unnecessary frame to the stack trace, fwiw).

All 3 comments

I personally do not see much benefit by introducing another abstraction.

I agree with @BridgeAR

As much as I dislike duplicated code, I'd have to agree with @BridgeAR and @mscdex. The added layer of abstraction just ends up hiding the details. (It also ends up adding an unnecessary frame to the stack trace, fwiw).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stevenvachon picture stevenvachon  路  3Comments

Icemic picture Icemic  路  3Comments

jmichae3 picture jmichae3  路  3Comments

mcollina picture mcollina  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments