# RpcExceptionFilter

**Kind:** Interface

**Source:** [`packages/common/interfaces/exceptions/rpc-exception-filter.interface.ts`](https://github.com/nestjs/nest/blob/master/packages/common/interfaces/exceptions/rpc-exception-filter.interface.ts#L11)

**Part of:** [Common](subsystem-packages-common)

Interface describing implementation of an RPC exception filter.

`RpcExceptionFilter` defines the contract for handling exceptions thrown during RPC and microservice message processing. Implementations receive the thrown exception and execution context, then return an RxJS `Observable` representing the error response sent through the configured transport.

## Diagram

```mermaid
graph LR
  A[RPC Request] --> B[Message Handler]
  B -->|Throws exception| C[RpcExceptionFilter]
  C --> D[catch exception, host]
  D --> E[Observable Error Response]
  E --> F[RPC Client / Transport]
```

## Usage

```ts
import { ArgumentsHost, Catch, RpcExceptionFilter } from '@nestjs/common';
import { RpcException } from '@nestjs/microservices';
import { Observable, throwError } from 'rxjs';

@Catch(RpcException)
export class RpcErrorFilter
  implements RpcExceptionFilter<RpcException>
{
  catch(exception: RpcException, host: ArgumentsHost): Observable<never> {
    const rpcContext = host.switchToRpc();
    const request = rpcContext.getData();

    console.error('RPC request failed:', request, exception.getError());

    return throwError(() => exception.getError());
  }
}
```

## AI Coding Instructions

- Implement `catch(exception, host)` and always return an RxJS `Observable`; use `throwError` when propagating an RPC error.
- Use `host.switchToRpc()` to access RPC-specific data, context, or transport arguments instead of HTTP request/response APIs.
- Prefer throwing or handling `RpcException` instances in microservice handlers so transport-compatible error payloads are preserved.
- Register custom filters through `@UseFilters()` or as global microservice exception filters, depending on the required scope.
- Avoid returning plain objects or promises directly from `catch`; the interface expects an observable response.

## Used by

2 references from 2 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

### Imported by (2)

- `BaseRpcExceptionFilter` — `packages/microservices/exceptions/base-rpc-exception-filter.ts`:15
- `ExceptionFilter` — `sample/03-microservices/src/common/filters/rpc-exception.filter.ts`:5
