{"id":"abea1f76d14379f1","repo":"nestjs/nest","slug":"3","errorCode":"3","errorMessage":"dividing by 0 is not possible","messagePattern":"dividing by 0 is not possible","errorType":"exception","errorClass":"RpcException","httpStatus":null,"severity":"error","filePath":"integration/microservices/src/grpc/grpc.controller.ts","lineNumber":100,"sourceCode":"        },\n        error: err => {\n          reject(err as Error);\n        },\n      });\n    });\n  }\n\n  @GrpcStreamCall('Math')\n  async sumStreamPass(stream: any) {\n    stream.on('data', (msg: any) => {\n      stream.write({ result: msg.data.reduce((a, b) => a + b) });\n    });\n  }\n\n  @GrpcMethod('Math')\n  async divide(request: { dividend: number; divisor: number }): Promise<any> {\n    if (request.divisor === 0) {\n      throw new RpcException({\n        code: 3,\n        message: 'dividing by 0 is not possible',\n      });\n    }\n    return {\n      result: request.dividend / request.divisor,\n    };\n  }\n\n  // contrived example meant to show when an error is encountered, like dividing by zero, the\n  // application does not crash and the error is returned appropriately to the client\n  @GrpcMethod('Math', 'StreamDivide')\n  streamDivide({\n    data,\n  }: {\n    data: { dividend: number; divisor: number }[];\n  }): Observable<any> {\n    return from(data).pipe(mergeMap(request => this.divide(request)));","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/nestjs/nest/blob/6ec0e2783d15290732447f304d8549b591b9749e/integration/microservices/src/grpc/grpc.controller.ts#L82-L118","documentation":"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.","triggerScenarios":"Calling the `Math/divide` gRPC method with `{ dividend: <number>, divisor: 0 }`.","commonSituations":"Client omits validation before calling divide; UI permits a zero divisor; division-by-zero stress test.","solutions":["Validate `divisor !== 0` on the client before calling divide.","Return an explicit error field in the proto response instead of throwing for expected business failures.","Document the gRPC code mapping (3 = INVALID_ARGUMENT) for client error handling."],"exampleFix":"// before\nif (request.divisor === 0) {\n  throw new RpcException({ code: 3, message: 'dividing by 0 is not possible' });\n}\n// after (client-side guard)\nif (divisor === 0) {\n  showError('Cannot divide by zero');\n  return;\n}\nawait svc.divide({ dividend, divisor });","handlingStrategy":"validation","validationCode":"function canDivide(dividend: number, divisor: number): boolean {\n  return typeof divisor === 'number' && divisor !== 0;\n}\nif (!canDivide(dividend, divisor)) { throw new Error('divisor must be non-zero'); }","typeGuard":"function isDivideRequest(v: unknown): v is { dividend: number; divisor: number } {\n  return typeof v === 'object' && v !== null\n    && typeof (v as any).dividend === 'number'\n    && typeof (v as any).divisor === 'number'\n    && (v as any).divisor !== 0;\n}","tryCatchPattern":"try {\n  return await svc.divide({ dividend, divisor }).toPromise();\n} catch (e) {\n  if (e?.code === 3) { /* INVALID_ARGUMENT: show 'cannot divide by zero' */ }\n  throw e;\n}","preventionTips":["Validate `divisor !== 0` on the client before the RPC.","Map gRPC status codes (3 = INVALID_ARGUMENT) to user-facing messages.","Prefer returning an error field in the proto response for expected business failures."],"tags":["grpc","rpc-exception","validation","microservices","nestjs","invalid-argument"],"analyzedSha":"6ec0e2783d15290732447f304d8549b591b9749e","analyzedAt":"2026-08-03T17:42:23.673Z","schemaVersion":2}