5. You want to create an ExceptionMappingInterceptor that converts all QueryFailedError exceptions into BadRequestException with a custom message, but lets other exceptions pass unchanged. Which code snippet correctly implements this behavior?
hard
A. return next.handle().pipe(catchError(err => {
if (err instanceof QueryFailedError) {
return throwError(() => new BadRequestException('Invalid query'));
}
return throwError(() => err);
}));
B. return next.handle().pipe(catchError(err => {
if (err instanceof QueryFailedError) {
throw new BadRequestException('Invalid query');
}
throw err;
}));
C. return next.handle().pipe(catchError(err => {
if (err.name === 'QueryFailedError') {
return new BadRequestException('Invalid query');
}
return err;
}));
D. return next.handle().pipe(catchError(err => {
if (err instanceof BadRequestException) {
return throwError(() => new QueryFailedError());
}
return throwError(() => err);
}));
Solution
Step 1: Check error type and return observable error
The interceptor must check if the error is a QueryFailedError instance and return a new BadRequestException wrapped in throwError.
Step 2: Pass other errors unchanged as observable errors
Other errors must be re-thrown as observable errors using throwError to keep the stream consistent.
Final Answer:
Option A correctly checks error type and returns observable errors properly -> Option A
Quick Check:
Use instanceof + throwError for error mapping [OK]
Hint: Use instanceof and return throwError for mapped and original errors [OK]
Common Mistakes:
Throwing errors directly inside catchError
Returning error objects instead of observable errors
Checking error name string instead of instanceof
Swapping error types incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.