Q:

Reactive design: throw vs. publish error

The following code example has (Rx)Swift smell to it, but the question is generic to any language with reactive features and throwing capability.
Consider a function which returns an observable sequence, but it does some sanity checking prior to creating the sequence. Check failure means that the sequence can't produce values.
func yieldFoos() -> Observable<Foo> {
  guard isValid(internalFoo) else {
    // throw or return one shot observable?
  }
  return createValidObservable(from: internalFoo)
}

In case of state validity check failure, should the function throw or return one shot observable, which will ever produce just an error? My coder's guts came up with these pros and cons:
Throwing feels logically cleaner (it is a failure preventing observable creation), but results in cumbersome calling code - catch block, multiple error handling points in different execution scopes.
One shot observable results in shorter and cleaner calling code, but somehow feels wrong. The observable is forced to be a carrier for non sequential error state, for the sake of brevity.
Anyone having a strong opinion worth following? Or another overlooked, elegant solution?

A:

I wonder about your feeling that it's wrong for an Observable to emit an error. That's part of it's job.
When you think about it, your createValidObservable(from:) function might emit an error despite being handed a valid internalFoo so the code that calls yieldFoos() has to be prepared to handle an emitted error anyway. You might as well roll all your error handling code together. I would go further and make your create function able to handle invalid foos itself by emitting an error and doing away with this yieldFoos function.
Now, if you want yieldFoos() to return a Driver rather than an observable, then you have to handle the error through throwing or a precondition (because Drivers don't emit errors.)
func yieldFoos() -> Observable<Foo> {
    guard isValid(internalFoo) else {
        return Observable.error(myError)
    }
    return createValidObservable(from: internalFoo)
}

I think you need to get over your feeling that having an Observable that immediately returns an Error is wrong. That is a perfectly valid thing for an Observable to do and something all code using that Observable needs to be ready to handle.

