NestJS - UnhandledPromiseRejectionWarning - nestjs

Short version:
I am looking for a way to way to run my NestJS application with the '--trace-warnings' flags that nodeJS offers. Is there some way to do this or does NestJS offer something similar?
Long version:
Hi! NestJS noob here. I am trying to run a dev version of the NestJS application I am working on. However, on starting the application I get the error below.
Clearly, it is missing a catcherror somewhere! However, the dev version has a LOT of updates and this error can be anywhere so I am hoping for a more efficient way of finding this bug than just checking every single new function! In the errormessage there are a few tips on flags to run while starting the application (node --trace-warnings ...). However, these are for node and not NestJS.
So therefore my question; is there some way to run NestJS with the --trace-warnings flag or some other efficient way to find where I am missing the catcherror?
Thanks in advance!
Error:
(node:72899) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:72899) 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: 1)
(node:72899) [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.
(node:72899) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
(node:72899) 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)

Well, 6379 is the Redis default port, it seems like Nest cannot connect to it :)
By the way, give a look at this paragraph:
https://docs.nestjs.com/exception-filters#catch-everything
In order to catch every unhandled exception (regardless of the
exception type), leave the #Catch() decorator's parameter list empty,
e.g., #Catch().
import {
ExceptionFilter,
Catch,
ArgumentsHost,
HttpException,
HttpStatus,
} from '#nestjs/common';
#Catch()
export class AllExceptionsFilter implements ExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const request = ctx.getRequest();
const status =
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
response.status(status).json({
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
});
}
}

To answer your specific question:
I am looking for a way to way to run my NestJS application with the '--trace-warnings' flags that nodeJS offers. Is there some way to do this or does NestJS offer something similar?
I've been trying to do the same thing, and there seems to be next-to-no documentation online on how to do this. Through some trial and error, I've found that this seems to work:
node --trace-warnings -r source-map-support/register --inspect dist/main
Note that this won't rebuild your application (neither when you first start it up, or by watching for changes), so you'll need to do that manually. The debugger is enabled via --inspect switch, remove that if you don't need to debug.

try to use a npm response / request logger like winston or moesif you will get a lot of help trust me.
here are the links for these library for nestjs
https://github.com/scalio/nest-couchdb

Related

(node:1928) UnhandledPromiseRejectionWarning: MongooseError: Operation `users.createIndex()` buffering timed out

Getting the following error when I tried to estalish connection to my MongoDB Atlas.
[nodemon] starting `node index.js`
Note-It backend listening at http://localhost:5000
(node:1928) UnhandledPromiseRejectionWarning: MongooseError: Operation `users.createIndex()` buffering timed out after 10000ms
at Timeout.<anonymous> (C:\Users\Meet\OneDrive\Desktop\note-it-backend-api\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:149:23)
at listOnTimeout (internal/timers.js:557:17)
at processTimers (internal/timers.js:500:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:1928) 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: 1)
(node:1928) [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.
***********************Connected to MongoDB successfully***********************
I cant make any HTTP requests because of this
The db.js contents are :
const mongoose = require('mongoose');
// Connection string for MongoDB ATLAS
const mongoURL = "mongodb+srv://new-meeti:<password>#meet-dev.cc0qw.mongodb.net/?retryWrites=true&w=majority"
const connectToMongo = () => {
// .connect method taks 2 args, 2nd one is a callback function
mongoose.connect(mongoURL, () => {
console.log("***********************Connected to MongoDB successfully***********************");
})
}
module.exports = connectToMongo;
This was more than a year old project hosted on heroku but then I had to shift to to some other hosting service so again deploying is getting problematic due to various deprecation issues I think.
A help would be really appreciated. Thanks :)
PS : I checked my Network access on the MongoDB Atlas and 0.0.0.0/0 (includes your current IP address) is active.

Get error when try to post to another domain

I'm trying to post form-urlencoded in Axios
This is my code
const qs = require("qs");
const axios = require("axios");
const tmp = { id: "96e8ef9f-7f87-4fb5-a1ab-fcc247647cce", filter_type: "2" };
axios
.post("https://www.lalal.ai/api/preview/", qs.stringify(tmp))
.then((result) => {
console.log(result);
});
This what I got
(node:2440) UnhandledPromiseRejectionWarning: Error: Request failed with status code 403
at createError (D:\reactjs\crud-mern\server\node_modules\axios\lib\core\createError.js:16:15)
at settle (D:\reactjs\crud-mern\server\node_modules\axios\lib\core\settle.js:17:12)
at IncomingMessage.handleStreamEnd (D:\reactjs\crud-mern\server\node_modules\axios\lib\adapters\http.js:244:11)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2440) 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:2440) [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.
Can anyone explain to me what is the error and how to fix it? thanks :D
In the error response it states Request failed with status code 403. 403 usually means "Forbidden" and that you do not have access to perform that request. I would check to make sure you have any access keys, tokens, or authorization required to make that request.
403 Forbidden: The server understood the request, but is refusing to fulfill it. (Source)

How to see stacktrace / cause of an error in Jest?

I'm trying to debug a nodeJS app.
I have some code which causes an error, a variable is undefined.
When I run the code normally, the error is very clear and easy to find:
without jest:
➜ server git:(dc/build) ✗ node test/runner.js
/Users/dc/dev/exiteer/xbot/server/src/mup/Story.js:24
Logger.logObj('loaded story', {name: doc.name})
^
ReferenceError: doc is not defined
at Story.reload (/Users/dc/dev/exiteer/xbot/server/src/mup/Story.js:24:42)
at Game.reload (/Users/dc/dev/exiteer/xbot/server/src/mup/Game.js:48:16)
at Object.<anonymous> (/Users/dc/dev/exiteer/xbot/server/test/runner.js:4:10)
Sweet, I can fix it.
Now, Jest has some nice tooling for writing tests so I thought I'd try that.
But the errors are seemingly impossible to track down:
➜ server git:(dc/build) ✗ npm run jest
> cbg#0.1.0 jest /Users/dc/dev/exiteer/xbot/server
> jest
PASS src/index.test.js
(node:23114) UnhandledPromiseRejectionWarning: ReferenceError: doc is not defined
(Use `node --trace-warnings ...` to show where the warning was created)
(node:23114) 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: 1)
(node:23114) [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.
FAIL src/mup/Actor.test.js
● Console
console.log
actors undefined
at Object.<anonymous> (src/mup/Actor.test.js:9:13)
● Game.js › can load a story
expect(received).toHaveLength(expected)
Matcher error: received value must have a length property whose value must be a number
Received has value: undefined
8 |
9 | console.log('actors', game.story.room.name.actors)
> 10 | expect(game.story.room.actors).toHaveLength(1);
| ^
11 | const actor = game.story.room.actors[0]
12 | const reply = actor.sayTo('hi')
13 | expect(reply).toBe('hi back from Sid')
at Object.<anonymous> (src/mup/Actor.test.js:10:36)
This tells me where my tests failed, but I'd prefer to know where the actual error is. Tests aren't the end goal here, a working app is.
Googling around I found and tried this but it gives the same error message.
node --trace-warnings node_modules/.bin/jest --no-cache
➜ server git:(dc/build) ✗ npm run test
> cbg#0.1.0 test /Users/dc/dev/exiteer/xbot/server
> node --trace-warnings node_modules/.bin/jest --no-cache
(node:23263) UnhandledPromiseRejectionWarning: ReferenceError: doc is not defined
at emitUnhandledRejectionWarning (internal/process/promises.js:151:15)
at processPromiseRejections (internal/process/promises.js:211:11)
at processTicksAndRejections (internal/process/task_queues.js:98:32)
(node:23263) 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: 1)
which gives a tiny bit more info but an
at emitUnhandledRejectionWarning (internal/process/promises.js:151:15)
at processPromiseRejections (internal/process/promises.js:211:11)
at processTicksAndRejections (internal/process/task_queues.js:98:32)
This is not very helpful for debugging my code.
Also Jest seems to swallow all console.log as if it's doing the best to make debugging as painful as possible. When your environment doesn't even log, its a real WTF moment.
This was a bug in Node.js or Jest depending on your perspective - I fixed this a few months ago.
It happens because Jest throws errors in JavaScript coming from a different realm - so instanceof Error fails. You can see my PR here.
There is a workaround if you are forced to use old versions of Node or Jest here:
process.on('unhandledRejection', (reason) => {
console.log(reason); // log the reason including the stack trace
throw e;
});
We added this hook in Node 1.3 so it should be safe to use in virtually all Node code.

Can't create bot

Im having a really odd error here...
Today I wanted to setup a new bot project but im having so much trouble!
Running under Ubuntu 18.04 if that makes any difference. Here's exactly what I did:
npm init #I follow the steps and all
npm install discord.js
npm install nodemon
touch index.js
#I start the bot
nodemon index.js
From there, i'll get an error basically telling me that index.js is empty. So, I add in the following code in it:
const discord = require("discord.js"); //import discord
var client = new discord.Client();
//console.log(client.emojis.find(emoji => emoji.name === "pkachu").id);
client.on('ready', () => {
console.log("ready to kick ass");
});
client.login('VALID discord token thing');
Sadly, i'll get the following error:
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
(node:9771) UnhandledPromiseRejectionWarning: Error [TOKEN_INVALID]: An invalid token was provided.
at WebSocketManager.connect (/home/lolcat/Documents/goodbot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:135:26)
at Client.login (/home/lolcat/Documents/goodbot/node_modules/discord.js/src/client/Client.js:220:21)
(node:9771) 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: 1)
(node:9771) [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.
[nodemon] clean exit - waiting for changes before restart
Okay, so this thing is telling me that it has an invalid discord.js token. Alright, maybe it changed or something. Thing is, it didn't change. In fact, that token appears to still be valid and make one of my older bots work!
I've tried everything at this point. I also tried following the link it gave me but to no avail, it still "crashes".
Any tips?
If you guys think you can get away with selfbots, no you can't. :kek:

npm start giving me a "UnhandledPromiseRejectionWarning: Error" and not sure how to fix it

I have been following along to Acadamind's NodeJS / Express / MongoDb-build a shopping cart tutorial on youtube and everything was going fine until I restarted the mongodb server when I wanted to update a product item.
Before this change, I would write npm start and the code would run smoothly, but now I am given a response of:
(node:49784) UnhandledPromiseRejectionWarning: Error: connect
ECONNREFUSED 127.0.0.1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
(node:49784) 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(). (rejection id: 1)
(node:49784) [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.
I am new to node.js and am not sure why this is occuring when I believe all i did was change a product item in my seed folder.
Here is the link to my github repository:clone my git hub "shopping-cart" repository here
Note: since this is my first node.js project that I am having problems with, is this the correct format to ask for help due to the larger nature or the project.
Thanks for any feedback!!!
Mongod needs to be running before starting the app:
sudo service mongod start
Status check:
sudo service mongod status
(should show Active (running))

Resources