sindresorhus / ow

Function argument validation for humans
https://sindresorhus.com/ow/
MIT License
3.8k stars 105 forks source link

export `ArgumentError` from package #167

Closed antixrist closed 4 years ago

antixrist commented 4 years ago

Hi! Thank you for ow!

Could you export ArgumentError class from package?

Yes, I can check the error's type by err.name === 'ArgumentError' but this stands out from the approach adopted by our team. Schematically:

async function updateById(id, data) {
  try {
    ow(id, ow.number.integer.greaterThanOrEqual(0).finite);
    ow(data, ow.object);

    const result = await db.updateById(id, data);
    // ...
  } catch (err) {
    if (err instanceof DBError) {
      if (err instanceof NotFoundError) {
        // ...
      } else if (err instanceof ConstraintViolationError) {
        // ...
      } // else if (err instanceof ...) { ... }
    }
    // here:
    else if (err.name === 'ArgumentError') { /* ... */ }
  }
}

It would be cool to use ArgumentError like this:

import ow, { ArgumentError } from 'ow';

// ...

if (err instanceof ArgumentError) { /* ... */ }

In addition, we get the possibility of static analysis and instead of searching by all project by "ArgumentError" string, the editor/IDE will be able to show all the places where this error is catched/handled.

p.s. yes, I'm js-only user, not ts =)

sindresorhus commented 4 years ago

Yeah, we could do that. Out of curiosity, why are you catching ArgumentError?