throwError
throwError(errorOrErrorFactory: (() => any) | any): Observable<never>Creates an Observable that immediately emits an error notification upon subscription.
💡 When should you use throwError vs. just throwing an error?
In most cases within operator callbacks (like map, tap, mergeMap), you can simply use JavaScript's native throw statement since RxJS wraps these in try-catch blocks. Use throwError() specifically when you need to return an Observable that errors - particularly in operators like switchMap, mergeMap, or concatMap where an Observable is expected as the return value.
💡 Factory function recommended
As of RxJS 7+, pass a factory function () => error rather than the error directly. This creates the error at the moment of subscription, providing better stack traces: throwError(() => new Error('message')).
Why use throwError?
Think of throwError as your "error signal generator" - it creates an Observable that does nothing but immediately send out an error signal. It's like having a specialized alarm button: when you press it, it doesn't emit any values or complete normally, it just triggers the error path.
You'll reach for throwError when you're working in Observable pipelines where you need to return an Observable, but something has gone wrong and you want to propagate that error downstream. For instance, in a conditional API call where you validate input before making a request, or when implementing retry logic where you want to signal specific failures.
Here's a key insight: while JavaScript's native throw statement works great inside operators like map or tap, throwError() shines when you're in operators that expect you to return an Observable - like switchMap, mergeMap, or inside a custom creation function. In those cases, just throwing would break the chain; you need to return an error Observable instead. It's the difference between throwing an error in your code versus constructing an Observable that represents an error state.
Examples
Example 1: Basic error emission
Example 2: Conditional error in switchMap
Example 3: Using throwError with retry strategy
Related Recipes
Additional Resources
throwError 📰 - Official docs
Error Handling in RxJS 📰 - Comprehensive guide
📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/throwError.ts
Last updated