Throw real Errors instead of objects

Abierto
#1,444 3 comentarios 0 reacciones 0 asignados Ver en GitHub

@psavan1655 ya está trabajando en esto.

Desde el 3/4/2023.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
typescript
Área
api

Línea de trabajo

Comienza con packages/client-account/src/methods/accountCopyIndex.ts y packages/client-account/src/errors/createDestinationIndiceExistsError.ts; después, inspecciona los otros helpers de errores referenciados por el issue. Decide cuál es la representación coherente de Error y verifica que los fallos lanzados conserven sus nombres, mensajes y stack traces reales para consumidores como Sentry.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

enhancement

This package has a non standard behavior of throwing objects instead of real JS Errors. As it works approximately in the same way in most cases and these objects are similar to Error (at least, TS is ok with that), there are some edge cases where it messes everything up.

My specific use case is with Sentry, which is not able to handle properly these "errors". Instead of having a proper stack trace, I have the Sentry error Non-Error exception captured and all the informations about the real error are lost.

We do this kind of thing all over the package:

accountCopyIndex.ts

throw createDestinationIndiceExistsError()

createDestinationIndiceExistsError.ts

export function createDestinationIndiceExistsError(): Error {
  return {
    name: 'DestinationIndiceAlreadyExistsError',
    message: 'Destination indice already exists.',
  };
}

I propose instead to either throw a basic Error with the message...

export function createDestinationIndiceExistsError(): Error {
  return new Error('DestinationIndiceAlreadyExistsError');
}

Or even to create custom error objects

class AlgoliaDestinationIndiceAlreadyExistsError extends Error {
  constructor(message) {
    super(message);
    this.name = 'AlgoliaDestinationIndiceAlreadyExistsError';
  }
}

export function createDestinationIndiceExistsError(): Error {
  return new AlgoliaDestinationIndiceAlreadyExistsError('Destination indice already exists.');
}

I even think that this helper methods are not necessary and that custom errors could be thrown directly. For instance, the custom message is useless in this case.

I can open a PR with a proof of concept if you think it's a good idea. I would also be glad to hear your ideas about the right implementation.

Lenguaje dominante
TypeScript
Estrellas
1.4k
Forks
227
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de algolia/algoliasearch-client-javascript

Todos los issues de algolia/algoliasearch-client-javascript

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.