Skip to content

Q: Why ApiError is not type of Error? #1200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
drobnikj opened this issue Sep 23, 2020 · 4 comments
Closed

Q: Why ApiError is not type of Error? #1200

drobnikj opened this issue Sep 23, 2020 · 4 comments
Labels
error related to errors

Comments

@drobnikj
Copy link

drobnikj commented Sep 23, 2020

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.

@Haroenv
Copy link
Contributor

Haroenv commented Sep 23, 2020

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
Copy link
Contributor

Haroenv commented Sep 23, 2020

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

@drobnikj
Copy link
Author

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

You are right, my bad.

@Haroenv Haroenv added the error related to errors label Aug 26, 2021
@shortcuts
Copy link
Member

Hey, closing in favor of #1444

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error related to errors
Projects
None yet
Development

No branches or pull requests

3 participants