ngx-bootstrap icon indicating copy to clipboard operation
ngx-bootstrap copied to clipboard

fix(typeahead): Typeahead, Observable error callback. When API call fails, application stops working

Open astroanu opened this issue 9 years ago • 10 comments

How do i write an error callback when the typehead is bound to an http observable. Currently it just prints an error on to the console and the component stops working. My typehead is fetching options by calling a remote url, if the remote server fails i want to show a message to the user.

astroanu avatar May 04 '17 12:05 astroanu

I'm putting the code that is responsible for this here

TypeaheadDirective.prototype.asyncActions = function () {
        var _this = this;
        this.keyUpEventEmitter
            .debounceTime(this.typeaheadWaitMs)
            .mergeMap(function () { return _this.typeahead; })
            .subscribe(function (matches) {
            _this.finalizeAsyncCall(matches);
        }, function (err) {
            console.error(err); <- instead this, call my error handler, or a common ngx bootstrap error handler 
        });
    };

astroanu avatar May 05 '17 07:05 astroanu

Am also having the same issue, when API call fails, application breaks. Doesn't work till refresh the application.

dilumdarshana avatar May 08 '17 07:05 dilumdarshana

I have a simmilar issue. My API call fails and application breaks when I have set typeaheadMinLength="3" and typeaheadWaitMs="300" and the user starts deleting the input faster than the debounce time, the Observable sends input with length<3 and my API fails. I dont need to show the error to the user, I just need a way to somehow prevent the application from breaking or prevent sending the input with length<3 at all.

vlaco avatar Jun 30 '17 15:06 vlaco

Hi! Any progress on this issue?

krooshua avatar Sep 26 '17 16:09 krooshua

@krooshua I tried your solution and it didn't work for me :/ API still returns an error and the application breaks and won't send any more requests. I solved this by simply adding catch to Observable like this:

this.dataSource = Observable.create((observer: any) => {
      // Runs on every search
      observer.next(this.asyncSelected);
    }).mergeMap((token: string) => this.getStatesAsObservable(token))
.catch((error: any) => {
      // console.log(error);
      return [];
    });

This works for me but there must be a better solution.

vlaco avatar Sep 27 '17 13:09 vlaco

Typeahead doesn't do any http requests itself, it only takes an Observable as input. So all http errors should be handled outside if this component.

IlyaSurmay avatar Jan 16 '18 16:01 IlyaSurmay

Http error is not the point of this issue. The point is that it shouldn't do anything if input length is less than 3 if typeaheadMinLength="3", it shouldn't accept it as an input and it does. Works perfectly fine if you type slowly enough for the debounce to catch it but if you type any faster it fails. I think there should be some additional input length check.

vlaco avatar Jan 16 '18 17:01 vlaco

@IlyaSurmay you haven't let the developers exactly handle those errors outside your component. Check the code it's right in front of you.

astroanu avatar Jan 16 '18 18:01 astroanu

@astroanu my bad :( We'll come up with plunkr that shows how to handle errors or PR that fixes the issue

IlyaSurmay avatar Jan 19 '18 10:01 IlyaSurmay

@astroanu Hi! Do you still have this issue? Typeahead component has had a lot of changes since 2017. I added PR with a new demo for async typeahead. It's not merged yet but you can check how I handle http error.

https://github.com/valor-software/ngx-bootstrap/pull/5637/files#diff-f68a35042d5e6635f58cce1aea50df39

I'm going to close the issue because it's not the issue anymore. @vlaco your issue is not related to the initial topic of the issue. Could you please open a new issue?

IraErshova avatar Feb 04 '20 08:02 IraErshova