Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
Is it safe to use `instanceOf` when the Error is used and defined in different libraries?

Given say three libraries, where library A defines a new error:

class CustomError extends Error { }

and library B and C imports and throws this error, is it safe for my project to use instanceof to check for this error? E.g.

const { CustomError } = require('@so/errors');
const doSomething = require('@so/doSomething');   // imports `CustomError` from `@so/errors`
const doSomething2 = require('@so/doSomething2'); // imports `CustomError` from `@so/errors`

try {
  doSomething();  // can throw `CustomError`
  doSomething2(); // can throw `CustomError`
} catch (err) {
  // is this check reliable?
  if(err instanceof CustomError) {
    console.log('caught!');
  }

  throw err;
}

Or does it depend on npm to symlink both @so/doSomething and @so/doSomething2 to the same @so/errors package I am using? If so, does that mean @so/errors has to be at the same version that @so/doSomething and @so/doSomething2 depends on?

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda