Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microservices - Exception Filtering - RPCException not working #9024

Closed
4 of 15 tasks
mustafa-barakat opened this issue Jan 25, 2022 · 2 comments
Closed
4 of 15 tasks
Labels
needs triage This issue has not been looked into

Comments

@mustafa-barakat
Copy link

mustafa-barakat commented Jan 25, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When using Microservices with Kafka transport and try to throw a new error using
throw new RpcException('Invalid credentials.');

I get

(node:27352) 
UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object
    at Function.from (buffer.js:330:9)
    at ServerKafka.assignErrorHeader (C:\Users\Barakat\Desktop\Projects\nestjs-micro-poc\node_modules\@nestjs\microservices\server\server-kafka.js:137:73)
    at ServerKafka.sendMessage (C:\Users\Barakat\Desktop\Projects\nestjs-micro-poc\node_modules\@nestjs\microservices\server\server-kafka.js:119:14)
    at C:\Users\Barakat\Desktop\Projects\nestjs-micro-poc\node_modules\@nestjs\microservices\server\server-kafka.js:81:31
    at C:\Users\Barakat\Desktop\Projects\nestjs-micro-poc\node_modules\@nestjs\microservices\server\server.js:46:31
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
(node:27352) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:27352) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Minimum reproduction code

https://github.com/mustafa-barakat/nestjs-micro-rpc-exception

Steps to reproduce

docker-compose up
wait for the second 42, an client module is sending the message with schedule

Expected behavior

The client app should recieve an exception from Kafka as a response.
Or the observable be rejected but no observable is processed

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

8.0.0

Packages versions

{
  "name": "nestjs-micro-poc",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^8.0.0",
    "@nestjs/core": "^8.0.0",
    "@nestjs/mapped-types": "*",
    "@nestjs/microservices": "^8.2.6",
    "@nestjs/platform-express": "^8.0.0",
    "@nestjs/schedule": "^1.0.2",
    "kafkajs": "^1.15.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^8.0.0",
    "@nestjs/schematics": "^8.0.0",
    "@nestjs/testing": "^8.0.0",
    "@types/cron": "^1.7.3",
    "@types/express": "^4.17.13",
    "@types/jest": "27.0.2",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "^27.2.5",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "^27.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "^3.10.1",
    "typescript": "^4.3.5"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

14.17.5

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

If we add return instead of throw, it works as expected but the client observable isn't rejected
If we use throwError from rxjs instead of RPC exception it works fine but not as the documentation states

@mustafa-barakat mustafa-barakat added the needs triage This issue has not been looked into label Jan 25, 2022
@mustafa-barakat
Copy link
Author

This is all using KAFKA, i tested it with Nats it's working fine

@kamilmysliwiec
Copy link
Member

Let's track this here #9293

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

2 participants