process killed when exception happen in async function in nest 9.x.x
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Current behavior
nestjs process exit when an error happern in an async function (without await) like this
i create a new project and use fasrify
and also try to use errors.interceptor.ts
https://docs.nestjs.com/interceptors
but notworks
controller
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
async getHello() {
this.appService.getHello();
return 'ok';
}
}
service
import {
BadGatewayException,
BadRequestException,
Injectable,
} from '@nestjs/common';
@Injectable()
export class AppService {
async getHello() {
throw new BadGatewayException();
return 'Hello World!';
}
}
ouput
/home/doctop/t/project-name/src/app.service.ts:10
throw new BadGatewayException();
^
BadGatewayException: Bad Gateway
at AppService.getHello (/home/doctop/t/project-name/src/app.service.ts:10:11)
at AppController.getHello (/home/doctop/t/project-name/src/app.controller.ts:18:21)
at /home/doctop/t/project-name/node_modules/@nestjs/core/router/router-execution-context.js:38:29
at InterceptorsConsumer.transformDeferred (/home/doctop/t/project-name/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:31:33)
at /home/doctop/t/project-name/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:15:53
at Observable._subscribe (/home/doctop/t/project-name/node_modules/rxjs/src/internal/observable/defer.ts:55:15)
at Observable._trySubscribe (/home/doctop/t/project-name/node_modules/rxjs/src/internal/Observable.ts:245:19)
at /home/doctop/t/project-name/node_modules/rxjs/src/internal/Observable.ts:235:18
at Object.errorContext (/home/doctop/t/project-name/node_modules/rxjs/src/internal/util/errorContext.ts:29:5)
at Observable.subscribe (/home/doctop/t/project-name/node_modules/rxjs/src/internal/Observable.ts:221:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Minimum reproduction code
https://stackblitz.com/edit/nestjs-typescript-starter-3fwqre?file=src/app.controller.ts
Steps to reproduce
1 - oppen link https://stackblitz.com/edit/nestjs-typescript-starter-3fwqre?file=src/app.controller.ts 2 - yarn install 3 - yarn start 4 - refresh page
Expected behavior
Expected internal server error
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.0.3
Packages versions
{
"name": "nest-typescript-starter",
"private": true,
"version": "1.0.0",
"description": "Nest TypeScript starter repository",
"license": "MIT",
"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": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/platform-fastify": "^9.0.3",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.5.5"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.13",
"@types/jest": "^28.1.4",
"@types/node": "^18.0.3",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"eslint": "^8.19.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^28.1.2",
"prettier": "^2.7.1",
"source-map-support": "^0.5.21",
"supertest": "^6.2.4",
"ts-jest": "^28.0.5",
"ts-loader": "^9.3.1",
"ts-node": "^10.8.2",
"tsconfig-paths": "^4.0.0",
"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
16
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
No response
Issue Analytics
- State:
- Created a year ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Common Mistakes in JavaScript Async Function Error ...
Error thrown from async function is a rejected promise For a rejected promise wrapping an uncaught error, we have two options to handle...
Read more >How do I prevent node.js from crashing? try-catch doesn't ...
With your suggestion, I can kill your entire cluster repeatedly. The answer is to make the application fail gracefully - ie handle the...
Read more >Async Await Error Handling in JavaScript
Error handling in async/await causes a lot of confusion. ... If the error handler // throws an error, kill the process. catch(err ...
Read more >Node.js v19.3.0 Documentation
Creates a new CallTracker object which can be used to track if functions were called a specific number of times. The tracker.verify() must...
Read more >GitHub - learning-zone/nodejs-basics: Node.js Basics ( v16 ...
When we go for the asynchronous method, an exception thrown during function execution cannot be detected in a try/catch statement. The event happens...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
another resource on this subject: Let It Crash: Best Practices for Handling Node.js Errors on Shutdown
thanks