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

Q: Why ApiError is not type of Error?

Open drobnikj opened this issue 5 years ago • 3 comments

It looks all errors thrown from the algolia client are plain objects.

Was it made for a specific reason?

Because it can cause some issue in users application as many developers expect the thrown error as a type of Error.

Short example:

...
    const client = algoliasearch('test', 'test', )

    const index = client.initIndex('dev_PUBLIC_STORE');

    try {
        await index.saveObject({
            objectID: "aaaaaaa",
            title: 'Algolia 2019',
            contentType: 'events'
        })
    } catch (err) {
        console.log(typeof err); // -> object
        console.error(err);  // -> prints just the object, no stack trace is printed
    }

....

I was expecting that stack trace will be logged, but I got an object in the console for this example.

drobnikj avatar Sep 23 '20 08:09 drobnikj

I think this was done to prevent errors when subclassing Error (IIRC, this was an issue with older babel / browser versions). You're right that it's not ideal, and we should change this to be real errors, but make sure that all the properties we set are still correctly set (otherwise it's a breaking change)

Haroenv avatar Sep 23 '20 09:09 Haroenv

Note that a real error also has typeof being object, you check using instanceof normally

Haroenv avatar Sep 23 '20 09:09 Haroenv

real error also has typeof being object, you check using instanceof normally

You are right, my bad.

drobnikj avatar Sep 23 '20 11:09 drobnikj