Skip to content

Commit

Permalink
feat(throwError): removed deprecated throwError(error) call pattern
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `throwError(error)` call pattern is no longer available. Use `throwError(() => error)`
  • Loading branch information
demensky committed Dec 17, 2022
1 parent 987f0a6 commit 678b6ed
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 25 deletions.
13 changes: 4 additions & 9 deletions spec-dtslint/observables/throwError-spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { throwError, animationFrameScheduler } from 'rxjs';

it('should accept any type and return never observable', () => {
const a = throwError(1); // $ExpectType Observable<never>
const b = throwError('a'); // $ExpectType Observable<never>
const c = throwError({ a: 1 }); // $ExpectType Observable<never>
const d = throwError(() => ({ a: 2 })); // $ExpectType Observable<never>
});

it('should support an error value and a scheduler', () => {
const a = throwError(1, animationFrameScheduler); // $ExpectType Observable<never>
it('should error for incorrect errorFactory', () => {
const a = throwError(1); // $ExpectError
const b = throwError('a'); // $ExpectError
const c = throwError({ a: 1 }); // $ExpectError
});

it('should accept any type and return never observable with support of factory', () => {
Expand Down
2 changes: 1 addition & 1 deletion spec/observables/throwError-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('throwError', () => {

it('should accept scheduler', () => {
rxTest.run(({ expectObservable }) => {
const e = throwError('error', rxTest);
const e = throwError(() => 'error', rxTest);

expectObservable(e).toBe('#');
});
Expand Down
18 changes: 3 additions & 15 deletions src/internal/observable/throwError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Observable } from '../Observable';
import { Subscriber } from '../Subscriber';
import { SchedulerLike } from '../types';
import { isFunction } from '../util/isFunction';

/**
* Creates an observable that will create an error instance and push it to the consumer as an error
Expand Down Expand Up @@ -97,29 +96,18 @@ import { isFunction } from '../util/isFunction';
*/
export function throwError(errorFactory: () => any): Observable<never>;

/**
* Returns an observable that will error with the specified error immediately upon subscription.
*
* @param error The error instance to emit
* @deprecated Support for passing an error value will be removed in v8. Instead, pass a factory function to `throwError(() => new Error('test'))`. This is
* because it will create the error at the moment it should be created and capture a more appropriate stack trace. If
* for some reason you need to create the error ahead of time, you can still do that: `const err = new Error('test'); throwError(() => err);`.
*/
export function throwError(error: any): Observable<never>;

/**
* Notifies the consumer of an error using a given scheduler by scheduling it at delay `0` upon subscription.
*
* @param errorOrErrorFactory An error instance or error factory
* @param errorFactory An error instance or error factory
* @param scheduler A scheduler to use to schedule the error notification
* @deprecated The `scheduler` parameter will be removed in v8.
* Use `throwError` in combination with {@link observeOn}: `throwError(() => new Error('test')).pipe(observeOn(scheduler));`.
* Details: https://rxjs.dev/deprecations/scheduler-argument
*/
export function throwError(errorOrErrorFactory: any, scheduler: SchedulerLike): Observable<never>;
export function throwError(errorFactory: () => any, scheduler: SchedulerLike): Observable<never>;

export function throwError(errorOrErrorFactory: any, scheduler?: SchedulerLike): Observable<never> {
const errorFactory = isFunction(errorOrErrorFactory) ? errorOrErrorFactory : () => errorOrErrorFactory;
export function throwError(errorFactory: () => any, scheduler?: SchedulerLike): Observable<never> {
const init = (subscriber: Subscriber<never>) => subscriber.error(errorFactory());
return new Observable(scheduler ? (subscriber) => scheduler.schedule(init as any, 0, subscriber) : init);
}

0 comments on commit 678b6ed

Please sign in to comment.