algolia / angular-instantsearch

⚡️Lightning-fast search for Angular apps, by Algolia
https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/angular/
MIT License
259 stars 73 forks source link

Be able to catch InstantSearch error #264

Open samouss opened 5 years ago

samouss commented 5 years ago

Right now there is no built-in way to catch error that occurs at the InstantSearch level. Users have to create an custom component scoped under the Provider to get access to the instance and then bind an listener on the error event. Here is an example.

A possible solution is to attach a listener on the error event by default, then re-throw the error inside Angular to be able to catch it with a custom ErrorHandler.

Haroenv commented 5 years ago

https://github.com/algolia/angular-instantsearch/blob/2c931b30d298a0987cbdbd32a0a328fcc77865f7/src/instantsearch/instantsearch.ts#L265-L267

probably should be after here, where we this.instantSearchInstance.on('error', error => throw error)

Jefiozie commented 5 years ago

Maybe you can just implement it like this:


import { ErrorHandler } from '@angular/core';
 constructor(private error: ErrorHandler) {}
    this.instantSearchParent.instantSearchInstance.on('error', error => {
      this.error.handleError(error);
    });
  }
Jefiozie commented 5 years ago

And probably it is best to use the @Output() change with a sort of Error State. With this people can listen to this error event and do something with it.

Haroenv commented 5 years ago

This makes lots of sense, and indeed seems to be "the angular" way of rethrowing errors. Would you be interested in doing a PR for that?

Jefiozie commented 5 years ago

Sure, would you like me to implement the ErrorHandler and the Output eventemitter? Btw is this the only place where this is happening or are there more places where the Errorhandler should be implemented?

Haroenv commented 5 years ago

I think any solution where a user will be able to catch the errors in a ErrorHandler around InstantSearch is good :)