Default action when no subscriptions?

angular

37 观看

1回复

604 作者的声誉

We have a generic HTTP service to wrap some HTTP behaviors. When there is an error, we add the error to a BehaviorSubject.

I'm wondering if there's a way to somehow... show this error only if nothing is subscribed to the Subject.

return this.$http.get(url, jsonData).map((res) => res.json().response_data)
  .catch((err) => {
    this._errors.next(err);
});

Basically trying to implement a "final" handling of HTTP errors if nothing else is handling it.

作者: Charlie 的来源 发布者: 2017 年 12 月 27 日

回应 1


1

38776 作者的声誉

决定

You can pass an object with a handled flag to the subscribers, which should set the flag to true after handling the error. It the flag is not set after the subscribers have been notified, show the default error message.

export class ErrorHandling {
    public handled: boolean = false;
    constructor(public error: any) {
    }
}

return this.$http.get(url, jsonData).map((res) => res.json().response_data)
  .catch((err) => {
    let errorHandling = new ErrorHandling(err);
    this._errors.next(errorHandling);
    if (!errorHandling.handled) {
        // The error was not handled, show default message
        ...
    }
});
作者: ConnorsFan 发布者: 2017 年 12 月 27 日
32x32