2

Following on from this post, I have the following

Observable.combineLatest(
        this.translate.get("key1"),
        this.translate.get(""),
        this.translate.get("key3"),
        this.translate.get("key4")
    )
    .subscribe(([result1, result2, result3, result4]) => {
        console.log(result1);
        console.log(result2);
        console.log(result3);
        console.log(result4);
    },
      error => {
        console.log(`${error}`);            
   });  

At line 2 I get an error, but this does not seem to go into the error handler above. Unfortunately the examples and doco I find don't seem to include how to catch errors (assume the above would work).

Does anyone have any ideas on how to do this?

Community
  • 1
  • 1
peterc
  • 6,921
  • 9
  • 65
  • 131

1 Answers1

1

It seems likely to me that this.translate.get("") is validating an argument and is throwing 'outside' of the observable (i.e. before it even creates its observable).

You can verify the error handling using code like this:

import "rxjs/add/observable/throw";

Observable.combineLatest(
  this.translate.get("key1"),
  Observable.throw(new Error("Boom!")),
  this.translate.get("key3"),
  this.translate.get("key4")
)
.subscribe(
  ([result1, result2, result3, result4]) => {
    console.log(result1);
    console.log(result2);
    console.log(result3);
    console.log(result4);
  },
  error => {
    console.log(`${error}`);
  }
);

And you can verify that this.translate.get("") is throwing the error 'outside' of the observable like this:

import "rxjs/add/observable/defer";

Observable.combineLatest(
  this.translate.get("key1"),
  Observable.defer(() => this.translate.get("")),
  this.translate.get("key3"),
  this.translate.get("key4")
)
.subscribe(
  ([result1, result2, result3, result4]) => {
    console.log(result1);
    console.log(result2);
    console.log(result3);
    console.log(result4);
  },
  error => {
    console.log(`${error}`);
  }
);

If this is what it is doing, I guess it's reasonable behaviour, as it's rather unlikely an empty key is valid. Errors that are more 'internal' are likely to be reported via the observable and should be handled by the error callback you've passed to subscribe.

cartant
  • 57,105
  • 17
  • 163
  • 197
  • Ok I see. I tried adding a normal (non rxjs related) try/catch block around the whole lot, and the error did enter the catch. So I assume this is all we can do here. – peterc Jan 20 '17 at 05:52
  • You could have a look at the source to make sure that's what's happening, but if the error is not thrown via the observable that's that and there's nothing you can do about it - apart from make sure you pass in valid keys. – cartant Jan 20 '17 at 07:23
  • That is a good idea (look at the source). Yes, indeed at the very top it throws an error if the key is not defined or has zero length. Also I found another method `instant()` (I didn't know about this one) that just returns the string - in the doco it just) explains need to be sure the async file load has completed if you use this one. – peterc Jan 21 '17 at 02:16