Skip to content

Commit

Permalink
fix(errors): Better error message when you return non-observable thin…
Browse files Browse the repository at this point in the history
…gs, (#2152)

like `undefined`

fixes #215
  • Loading branch information
jayphelps authored and benlesh committed Dec 5, 2016
1 parent bd56b3c commit 86a909c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
24 changes: 22 additions & 2 deletions spec/util/subscribeToResult-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ describe('subscribeToResult', () => {
const subscriber = new OuterSubscriber(x => {
done(new Error('should not be called'));
}, (x) => {
expect(x).to.be.an('error', 'invalid observable');
expect(x).to.be.an('error');
expect(x.message).to.be.equal('Provided object does not correctly implement Symbol.observable');
done();
}, () => {
done(new Error('should not be called'));
Expand All @@ -138,12 +139,31 @@ describe('subscribeToResult', () => {
const subscriber = new OuterSubscriber(x => {
done(new Error('should not be called'));
}, (x) => {
expect(x).to.be.an('error', 'unknown type returned');
expect(x).to.be.an('error');
const msg = 'You provided an invalid object where a stream was expected.'
+ ' You can provide an Observable, Promise, Array, or Iterable.';
expect(x.message).to.be.equal(msg);
done();
}, () => {
done(new Error('should not be called'));
});

subscribeToResult(subscriber, {});
});

it('should emit an error when trying to subscribe to a non-object', (done: MochaDone) => {
const subscriber = new OuterSubscriber(x => {
done(new Error('should not be called'));
}, (x) => {
expect(x).to.be.an('error');
const msg = 'You provided \'null\' where a stream was expected.'
+ ' You can provide an Observable, Promise, Array, or Iterable.';
expect(x.message).to.be.equal(msg);
done();
}, () => {
done(new Error('should not be called'));
});

subscribeToResult(subscriber, null);
});
});
16 changes: 9 additions & 7 deletions src/util/subscribeToResult.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { root } from './root';
import { isArray } from './isArray';
import { isPromise } from './isPromise';
import { isObject } from './isObject';
import { Subscriber } from '../Subscriber';
import { Observable, ObservableInput } from '../Observable';
import { $$iterator } from '../symbol/iterator';
Expand Down Expand Up @@ -31,9 +32,7 @@ export function subscribeToResult<T>(outerSubscriber: OuterSubscriber<any, any>,
} else {
return result.subscribe(destination);
}
}

if (isArray(result)) {
} else if (isArray(result)) {
for (let i = 0, len = result.length; i < len && !destination.closed; i++) {
destination.next(result[i]);
}
Expand All @@ -55,7 +54,7 @@ export function subscribeToResult<T>(outerSubscriber: OuterSubscriber<any, any>,
root.setTimeout(() => { throw err; });
});
return destination;
} else if (typeof result[$$iterator] === 'function') {
} else if (result && typeof result[$$iterator] === 'function') {
const iterator = <any>result[$$iterator]();
do {
let item = iterator.next();
Expand All @@ -68,15 +67,18 @@ export function subscribeToResult<T>(outerSubscriber: OuterSubscriber<any, any>,
break;
}
} while (true);
} else if (typeof result[$$observable] === 'function') {
} else if (result && typeof result[$$observable] === 'function') {
const obs = result[$$observable]();
if (typeof obs.subscribe !== 'function') {
destination.error(new Error('invalid observable'));
destination.error(new TypeError('Provided object does not correctly implement Symbol.observable'));
} else {
return obs.subscribe(new InnerSubscriber(outerSubscriber, outerValue, outerIndex));
}
} else {
destination.error(new TypeError('unknown type returned'));
const value = isObject(result) ? 'an invalid object' : `'${result}'`;
const msg = `You provided ${value} where a stream was expected.`
+ ' You can provide an Observable, Promise, Array, or Iterable.';
destination.error(new TypeError(msg));
}
return null;
}

0 comments on commit 86a909c

Please sign in to comment.