algoliasearch-client-javascript icon indicating copy to clipboard operation
algoliasearch-client-javascript copied to clipboard

Non-error thrown at `createRetryError`

Open holm opened this issue 4 years ago • 2 comments

At https://github.com/algolia/algoliasearch-client-javascript/blob/master/packages/transporter/src/concerns/retryableRequest.ts#L65 a plain object is thrown instead of a proper Error instance.

I am not sure why this is the case, but it results in hard-to-handle exceptions, that gives poor stacktraces and error reporting.

holm avatar Aug 25 '21 18:08 holm

This was done to be able to easily add other properties (like debug information) to the error, and avoiding Error (as this isn't consistent between browsers, babel etc.). I agree that it should be changed for all errors, however I'm not sure what makes this error harder to handle, could you clarify?

Haroenv avatar Aug 26 '21 09:08 Haroenv

I'm not sure why Error isn't consistent. It's part of the core language and has always been. It's certainly possible to add additional properties to an error using normal prototypical inheritance from the base Error. Lot's more information is at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#custom_error_types.

We use Sentry to handle errors occurring in systems and report them centrally. These will generally verify that a thrown or rejected value is always an Error. Errors are special in that they capture the stacktrace when created, which is essential for tracking down sources of errors.

This is also why a core eslint rule exists for this, which also has some background: https://eslint.org/docs/rules/no-throw-literal.

holm avatar Aug 26 '21 09:08 holm