Get more info about uncaughtException in NodeJS - javascript

How do i get more info on events like unhandledError or unhandledRejection in nodeJs?
For example, in browser, i can simply add this listener:
window.onerror = function(message, page, lineno, colno, error) {
};
Which gives me error name, stack, type and even location.
But in nodeJs, i see only the message, and nothing else:
process.on('uncaughtException', (err) => {
console.log('catched!', err);
});
How do i get more info about it? full stack trace, type of error separately from it's message etc? Like i did in browser.

Related

Sending error in a specific channel Discord.js

I have a try...catch system to send an error to a channel when there is an error.
Here is the code:
try {
cmd.execute(client, message, args, prefix, result);
} catch (err) {
echannel.send(err);
}
echannel is the channel that I got with client.channels.cache.get(). The channel is the correct one.
I made a command to throw the error with:
message.channel.send('Throwing error...');
throw new Error('Testing');
I can see the error in the console though. Any help?
Try to simply send the error to the channel:
message.channel.send(err);
I've figured it out. I need to add process.on('uncaughtException, err => {})

Why does `websocket.close()` before connection establishes triggers `onerror`?

Calling websocket.close() before connection is established triggers onerror. I wasn't able to figure out what the error is, nor where it came from.
const connection = new WebSocket("wss://echo.websocket.org");
connection.onopen = () => {
console.log('open');
}
connection.onerror = (error) => {
throw error; // this is thrown
}
connection.close();
Tested in chrome dev console. onerror is being triggered when close is called.
If I wait until the connection is established before calling close, no error is thrown. I wonder what the error is
Edit:
included the error output:
I took my own advice and checked it out - and it gave me this:
Not sure if that answers your question if I say that it's more strange not to expect an error when you're not waiting for a connection before closing a socket.
Just handle it with a try-catch or put your connection.close() inside your onopen handler?

How do I send a custom error message from Node, and display it with catch?

I'm trying to send a custom error message from node, receive it on the frontend, and display it.
On server I'm trying to do:
res.status(500).send('Something broke!')
And on client I'm doing:
try {
const res = await axios.post(`${API_URL}/profiles/signup`, credentials)
} catch(err) {
console.log("Sign up error:", err)
}
What I want is to get something like this in my console:
Sign up error: Something broke!
but I'm getting:
Sign up error: Error: Request failed with status code 500
at createError (createError.js:16)
at settle (settle.js:18)
at XMLHttpRequest.handleLoad (xhr.js:77)
How do I access the custom message I'm sending?
You can create an error with the help of Error()
throw new Error({code: 500, message: 'Sign up error: Something broke!'});
throw send your error to the catch block automatically.
Figured it out:
console.log("Sign up error:", err.response.data)

Failed to load resource: net::ERR_CONNECTION_REFUSED on NodeJs ,AngularJs Web Application

when i run my web application i get this error.this was happened suddenly.my application is MEAN stack appllication whicj has a end to end connection through APIs.
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
at ServerResponse.res.set.res.header (D:
this is an API that is being exposed.
app.post('/api/filesReadUpload', function(req, res) {
var eventReadUpload=new filesReadUpload({
//id:req.body.id,
dat:req.body.dat,
details:req.body.details,
tim:req.body.tim,
// space:req.body.space
});
eventReadUpload.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Read file!' });
});
});
can anyone support with this.if this information is not enough please inform me.
Thanks
The ERR_CONNECTION_REFUSED appears because the server is down. The reason why the server is down is because of the error you pointed:
Error: Can't set headers after they are sent.
That means you tried to send the response twice. In case of error you have to run return res.send(err) because you want to not call the res.json in case of error. If you do (what currently is happening), you will get that error.
eventReadUpload.save(function(err) {
if (err)
return res.send(err);
res.json({
message: 'Read file!'
});
});
You will have to figure out why the saving fails, but this will fix the initial problem.

Socketio: Error: Lone surrogate U+D83D is not a scalar value

recently my node.js server has been crashing a lot due to this socket.io error. My suspicion is that client is sending invalid utf string, which will throw an error in the utf8.js file. Is there a way to avoid server crash? It's very frustrating. I'm down to monkey patch this file if needed.
Error: Lone surrogate U+D83D is not a scalar value
at Error (native)
at checkScalarValue (/root/node_modules/socket.io/node_modules/engine.io/node_modules/engine.io-parser/node_modules/utf8/utf8.js:69:10)
at encodeCodePoint (/root/node_modules/socket.io/node_modules/engine.io/node_modules/engine.io-parser/node_modules/utf8/utf8.js:90:4)
at Object.utf8encode [as encode] (/root/node_modules/socket.io/node_modules/engine.io/node_modules/engine.io-parser/node_modules/utf8/utf8.js:111:18)
at Object.exports.encodePacket (/root/node_modules/socket.io/node_modules/engine.io/node_modules/engine.io-parser/lib/index.js:74:34)
at encodeOne (/root/node_modules/socket.io/node_modules/engine.io/node_modules/engine.io-parser/lib/index.js:387:13)
at eachWithIndex (/root/node_modules/socket.io/node_modules/engine.io/node_modules/engine.io-parser/lib/index.js:256:5)
at map (/root/node_modules/socket.io/node_modules/engine.io/node_modules/engine.io-parser/lib/index.js:263:5)
at Object.exports.encodePayloadAsBinary (/root/node_modules/socket.io/node_modules/engine.io/node_modules/engine.io-parser/lib/index.js:411:3)
at Object.exports.encodePayload (/root/node_modules/socket.io/node_modules/engine.io/node_modules/engine.io-parser/lib/index.js:225:20)
at XHR.Polling.send (/root/node_modules/socket.io/node_modules/engine.io/lib/transports/polling.js:238:10)
at Socket.flush (/root/node_modules/socket.io/node_modules/engine.io/lib/socket.js:341:20)
at Socket.sendPacket (/root/node_modules/socket.io/node_modules/engine.io/lib/socket.js:317:10)
at Socket.send.Socket.write (/root/node_modules/socket.io/node_modules/engine.io/lib/socket.js:290:8)
at writeToEngine (/root/node_modules/socket.io/lib/client.js:148:17)
at Client.packet (/root/node_modules/socket.io/lib/client.js:159:7)
Probably you are trying to send your data in some wrong way.
You could try to have a try {} catch (e) {} around your Client/socket.packet, or do something like this:
process.on('uncaughtException', (err) => {
console.log(`Caught exception: ${err}`);
});
To prevent the server from dieing.

Categories

Resources