> ## Content Index
> Fetch the complete content index at: https://www.karls.io/llms.txt
> Use this file to discover other available public pages before exploring further.

# NestJs Microservice RpcException Error Handling
- URL: https://www.karls.io/nestjs-microservice-exception-handling/
- Published: 2019-11-10T16:05:45.000Z
- Updated: 2025-12-16T13:29:41.000Z
- Description: But todays tip is about how to handle errors in NestJs microservice architecture. For an example lets use Redis as a proxy between other microservices instead of letting each microservice talk to each other.
- Author: Karl Scerbiakas
- Tags: NestJS

Microservices is this concept of breaking up a monolyth into a smaller pieces to make project more manageable. You can learn more [here](https://www.karls.io/microservices-nestjs/).

But todays tip is about how to handle errors in NestJs microservice architecture. For an example lets use Redis as a proxy between other microservices instead of letting each microservice talk to each other. We are using Redis of instead of direct TCP connection for several reasons: it can serve as a cache and we can utilize [Pub/Sub](https://redis.io/topics/pubsub?ref=karls.io) feature. The main benefit is that microservices are decoupled, meaning it doesn't need to know about each other, don't care how to connect to each other etc. Anyway, who don't use Redis these days?

So lets say you have a math service in your microservice architecture which accumulates numbers. Errors happen. Exceptions should be thrown. Instead of throwing standart `HttpException` you have to throw `RpcException`

Heres an example:

```typescript
@Injectable()
export class MathService {
  public accumulate(data: number[]): number {
    throw new RpcException(‘An error was thrown’);
  }
}

```

Math microservice is throwing RpcException. So in your client microservice ( where this math microservice will be consumed ) you can handle it like this:

```typescript
@Injectable()
export class MathService {
  private client: ClientProxy;

  constructor() {
    this.client = ClientProxyFactory.create({
      transport: Transport.REDIS,
      options: {
        url: ‘redis://localhost:6379’,
      },
    });
  }

  public accumulate(data: number[]) {
    return (
      this.client
        // From here we connect to the math microservice.
        // if there is no error it will return a response directly
        // to the browser or other api consumer
        .send<number, number[]>(‘add’, data)
        // if there is an error `catchError` operator will catch it
        // and `of` observable will return observable with your error object
        .pipe(catchError(val => of({ error: val.message })))
    );
  }
}

```

So if you’re familiar with RxJS you probably already saw that the client ( what consumes our math microservice ) returns an observable, what it essentialy means you can apply other operators to your observable stream and modify response to your needs.

## Join Devs Who Are Building Better Backend Systems

Get production-tested NestJS patterns, microservices insights,   
and GCP Cost hacks. 

Subscribe 

Email sent! Check your inbox to complete your signup. 

No spam. Unsubscribe anytime.