Skip to content

Function: throwError()

throwError<T>(message): Operator<T, never>

Defined in: operators/throwError.ts:17

Creates a stream operator that immediately throws an error with the provided message.

This operator is a source operator that is used to create a stream that immediately fails. When a consumer requests a value by calling next(), the operator will throw an Error with the given message, without emitting any values.

This is useful for testing error handling logic in a stream pipeline or for explicitly modeling a failed asynchronous operation.

Type Parameters

T

T = any

The type of the values in the stream (this is a formality, as no values are emitted).

Parameters

message

string

The error message to be thrown.

Returns

Operator<T, never>

An Operator instance that creates a stream which errors upon its first request.

Examples

From throwError.spec.ts:4

typescript
const stream = from([1, 2, 3]).pipe(throwError('Boom!'));
stream.subscribe({
  next: () => {
    fail('Expected no values to be emitted');
  },
  error: (err) => {
    expect(err).toEqual(jasmine.any(Error));
    expect(err.message).toBe('Boom!');
    done();
  },
});

Released under the MIT License.