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

After upgrading to v9.x.x, throwing error from middleware (fastify) cause application to crash #10781

Closed
5 of 15 tasks
srkimir opened this issue Dec 31, 2022 · 7 comments
Closed
5 of 15 tasks
Labels
needs triage This issue has not been looked into

Comments

@srkimir
Copy link

srkimir commented Dec 31, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

After upgrading nestjs to version 9 (exact one 9.2.1) with using @nestjs/platform-fastify, throwing an error from middleware is causing application to crash.

Using following code in middleware will cause application to crash with below error:
Code:

use(req: Request, res: Response, next: NextFunction): void {
    throw new BadRequestException('Something is not good here...');
  }

Error:

/Users/srkimir/Projects/nest/test/node_modules/fastify/lib/reply.js:479
  if (reply[kRouteContext].preSerialization !== null) {
                           ^
TypeError: Cannot read properties of undefined (reading 'preSerialization')
    at preserializeHook (/Users/srkimir/Projects/nest/test/node_modules/fastify/lib/reply.js:479:28)
    at Reply.send (/Users/srkimir/Projects/nest/test/node_modules/fastify/lib/reply.js:191:7)
    at FastifyAdapter.reply (/Users/srkimir/Projects/nest/test/node_modules/@nestjs/platform-fastify/adapters/fastify-adapter.js:167:29)
    at ExceptionsHandler.catch (/Users/srkimir/Projects/nest/test/node_modules/@nestjs/core/exceptions/base-exception-filter.js:28:28)
    at ExceptionsHandler.next (/Users/srkimir/Projects/nest/test/node_modules/@nestjs/core/exceptions/exceptions-handler.js:16:20)
    at /Users/srkimir/Projects/nest/test/node_modules/@nestjs/core/router/router-proxy.js:13:35
    at Holder.done (/Users/srkimir/Projects/nest/test/node_modules/@fastify/middie/engine.js:107:13)
    at Object.run (/Users/srkimir/Projects/nest/test/node_modules/@fastify/middie/engine.js:59:12)
    at Object.runMiddie (/Users/srkimir/Projects/nest/test/node_modules/@fastify/middie/index.js:42:21)
    at hookIterator (/Users/srkimir/Projects/nest/test/node_modules/fastify/lib/hooks.js:246:10)

For some reason if i do not throw error from middleware, but rather using next callback with passing error, everything seems to be fine, application will not crash and no preSerialization error will occur.

Code:

use(req: Request, res: Response, next: NextFunction): void {
    next(new BadRequestException('Something is not good here...'));
}

Minimum reproduction code

https://github.com/srkimir/nestjs-fastify-preSerialization-error

Steps to reproduce

  1. npm install
  2. npm start
  3. GET localhost:3000/

Expected behavior

Application to handle error, and to respond with BadRequestException which was thrown from middleware.

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

9.2.1

Packages versions

{
  "name": "test",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "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": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/platform-fastify": "^9.2.1",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "29.2.4",
    "@types/node": "18.11.18",
    "@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": "29.3.1",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "29.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.1",
    "typescript": "^4.7.4"
  },
  "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

18.12.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@srkimir srkimir added the needs triage This issue has not been looked into label Dec 31, 2022
@micalevisk
Copy link
Member

micalevisk commented Dec 31, 2022

from which version of @nestjs/platform-fastify did you upgraded?

@srkimir
Copy link
Author

srkimir commented Dec 31, 2022

from which version of @nestjs/platform-fastify did you upgraded?

Thank you @micalevisk for answer
From: "@nestjs/platform-fastify": "8.4.7"
To: "@nestjs/platform-fastify": "9.2.1"

But the github repo link i have posted where issue is reproducible, is brand new small project just showing this behaviour.

@micalevisk
Copy link
Member

That bug was introduced in 9.1.3, 9.1.2 went fine

@micalevisk
Copy link
Member

I found that the if we use fastify@4.6.x it works.

image

@adworacz
Copy link
Contributor

adworacz commented Jan 4, 2023

Just ran into this issue myself. I can confirm that pinning our version to 9.1.2 of @nestjs/platform-fastify works around the issue for now.

We'll upgrade once the internal upgrade to fastify 4.6.x has been confirmed.

@micalevisk
Copy link
Member

micalevisk commented Jan 4, 2023

$ npm view @nestjs/platform-fastify dependencies
{
  '@fastify/cors': '8.2.0',
  '@fastify/formbody': '7.3.0',
  '@fastify/middie': '8.0.0',
  fastify: '4.10.2',
  'light-my-request': '5.6.1',
  'path-to-regexp': '3.2.0',
  tslib: '2.4.1'
}

the issue is that @nestjs/platform-fastify isn't compatible with fastify@^4.7. Then there's nothing to upgrade but downgrade.

@adworacz
Copy link
Contributor

adworacz commented Jan 4, 2023

Oh sorry I had that backwards. I thought it was a backwards compatibility issue, not a forwards compatibility one.

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

4 participants