nestjs/nest · error · RpcException

3

3

Error message

dividing by 0 is not possible

What it means

The gRPC `divide` handler throws an `RpcException` carrying gRPC status code `3` (`INVALID_ARGUMENT`) and message 'dividing by 0 is not possible' when `request.divisor === 0`. NestJS serialises this into a proper gRPC error on the wire, so the server keeps running and the client receives a structured error rather than a crash.

Source

Thrown at integration/microservices/src/grpc/grpc.controller.ts:100

        },
        error: err => {
          reject(err as Error);
        },
      });
    });
  }

  @GrpcStreamCall('Math')
  async sumStreamPass(stream: any) {
    stream.on('data', (msg: any) => {
      stream.write({ result: msg.data.reduce((a, b) => a + b) });
    });
  }

  @GrpcMethod('Math')
  async divide(request: { dividend: number; divisor: number }): Promise<any> {
    if (request.divisor === 0) {
      throw new RpcException({
        code: 3,
        message: 'dividing by 0 is not possible',
      });
    }
    return {
      result: request.dividend / request.divisor,
    };
  }

  // contrived example meant to show when an error is encountered, like dividing by zero, the
  // application does not crash and the error is returned appropriately to the client
  @GrpcMethod('Math', 'StreamDivide')
  streamDivide({
    data,
  }: {
    data: { dividend: number; divisor: number }[];
  }): Observable<any> {
    return from(data).pipe(mergeMap(request => this.divide(request)));

View on GitHub (pinned to 6ec0e2783d)

Solutions

  1. Validate `divisor !== 0` on the client before calling divide.
  2. Return an explicit error field in the proto response instead of throwing for expected business failures.
  3. Document the gRPC code mapping (3 = INVALID_ARGUMENT) for client error handling.

Example fix

// before
if (request.divisor === 0) {
  throw new RpcException({ code: 3, message: 'dividing by 0 is not possible' });
}
// after (client-side guard)
if (divisor === 0) {
  showError('Cannot divide by zero');
  return;
}
await svc.divide({ dividend, divisor });
Defensive patterns

Strategy: validation

Validate before calling

function canDivide(dividend: number, divisor: number): boolean {
  return typeof divisor === 'number' && divisor !== 0;
}
if (!canDivide(dividend, divisor)) { throw new Error('divisor must be non-zero'); }

Type guard

function isDivideRequest(v: unknown): v is { dividend: number; divisor: number } {
  return typeof v === 'object' && v !== null
    && typeof (v as any).dividend === 'number'
    && typeof (v as any).divisor === 'number'
    && (v as any).divisor !== 0;
}

Try / catch

try {
  return await svc.divide({ dividend, divisor }).toPromise();
} catch (e) {
  if (e?.code === 3) { /* INVALID_ARGUMENT: show 'cannot divide by zero' */ }
  throw e;
}

Prevention

When it happens

Trigger: Calling the `Math/divide` gRPC method with `{ dividend: <number>, divisor: 0 }`.

Common situations: Client omits validation before calling divide; UI permits a zero divisor; division-by-zero stress test.

Related errors


AI-assisted analysis of nestjs/nest@6ec0e2783d (2026-08-03). Data as JSON: /data/errors/abea1f76d14379f1.json. Report an issue: GitHub.