Node.js: Any error crashes Heroku server - javascript

I have a Node.js app to do some simple processing, however, every time there's an error, the server crashes.
2011-12-19T19:27:49+00:00 app[web.1]: node.js:134
2011-12-19T19:27:49+00:00 app[web.1]: throw e; // process.nextTick error, or 'error' event on first tick
2011-12-19T19:27:49+00:00 app[web.1]: ^
2011-12-19T19:27:49+00:00 app[web.1]: TypeError: Object { customer_id: 4eef8ddb74019b9d4f00001d,
2011-12-19T19:27:49+00:00 app[web.1]: date: Mon, 19 Dec 2011 19:27:36 GMT,
2011-12-19T19:27:49+00:00 app[web.1]: callsid: '123',
2011-12-19T19:27:49+00:00 app[web.1]: _id: 4eef902887e9770100000008 } has no method 'save'
2011-12-19T19:27:49+00:00 app[web.1]: at Promise.<anonymous> (/app/src/call.js:137:24)
2011-12-19T19:27:49+00:00 app[web.1]: at Promise.<anonymous> (/app/node_modules/mongoose/lib/promise.js:120:8)
2011-12-19T19:27:49+00:00 app[web.1]: at Promise.<anonymous> (events.js:64:17)
2011-12-19T19:27:49+00:00 app[web.1]: at Promise.emit (/app/node_modules/mongoose/lib/promise.js:59:38)
2011-12-19T19:27:49+00:00 app[web.1]: at Promise.complete (/app/node_modules/mongoose/lib/promise.js:70:20)
2011-12-19T19:27:49+00:00 app[web.1]: at /app/node_modules/mongoose/lib/query.js:738:28
2011-12-19T19:27:49+00:00 app[web.1]: at model.init (/app/node_modules/mongoose/lib/model.js:181:36)
2011-12-19T19:27:49+00:00 app[web.1]: at cb (/app/node_modules/mongoose/lib/query.js:736:14)
2011-12-19T19:27:49+00:00 app[web.1]: at Array.<anonymous> (/app/node_modules/mongoose/lib/utils.js:394:16)
2011-12-19T19:27:49+00:00 app[web.1]: at model.<anonymous> (/app/node_modules/mongoose/lib/document.js:181:5)
How do I protect from such errors? For example, when I call a variable that doesn't exist, the server crashes. I understand the error but sometimes I only notice the error when deployed and the server crashes.

You can make sure your script doesn't stop by catching the 'uncaughtException' with process.on('uncaughtException', callback), like so:
process.on('uncaughtException', function (exception) {
// handle or ignore error
console.log(exception);
});
setInterval(function() {
console.log('tick, even after error');
}, 1000);
a += 1;

Related

How do I catch an ECONNRESET error using the basic FTP-Library?

I want my program to continue running after catching ECONNRESET. How do I do that? I'm using basic-ftp for my ftp library. Here's the error below:
Node.js v17.7.2
node:events:505
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:217:20)
Emitted 'error' event on Socket instance at:
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read'
}
How do I catch it and have the program continue running? I've already tried try/catch blocks shown below:
async connect(port,ip_address){
try{
await this.client.access({
port: port,
host: ip_address,
secure: false
})
}
catch{
this.num_of_files_read=0;
this.files_completed=0;
return 1
}
return 0
}

Heroku: Javascript/MySQL project crashing on request

I have a Heroku API written in JavaScript that reads data from a MySQL database. It previously worked, but now every time I send a request to the API, I get this error:
2020-06-17T18:37:13.493711+00:00 app[web.1]: > my_api#0.0.0 start /app
2020-06-17T18:37:13.493712+00:00 app[web.1]: > node ./bin/www
2020-06-17T18:37:13.493712+00:00 app[web.1]:
2020-06-17T18:37:14.977800+00:00 heroku[web.1]: State changed from starting to up
2020-06-17T18:38:40.255982+00:00 app[web.1]: /app/node_modules/mysql2/lib/connection.js:149
2020-06-17T18:38:40.256022+00:00 app[web.1]: err.fatal = true;
2020-06-17T18:38:40.256023+00:00 app[web.1]: ^
2020-06-17T18:38:40.256023+00:00 app[web.1]:
2020-06-17T18:38:40.256026+00:00 app[web.1]: TypeError: Cannot create property 'fatal' on string 'SOCKS: Authentication failed'
2020-06-17T18:38:40.256027+00:00 app[web.1]: at Connection._handleFatalError (/app/node_modules/mysql2/lib/connection.js:149:15)
2020-06-17T18:38:40.256027+00:00 app[web.1]: at Connection._handleNetworkError (/app/node_modules/mysql2/lib/connection.js:169:10)
2020-06-17T18:38:40.256027+00:00 app[web.1]: at SocksConnection.emit (events.js:315:20)
2020-06-17T18:38:40.256029+00:00 app[web.1]: at /app/node_modules/socksjs/socks.js:146:18
2020-06-17T18:38:40.256029+00:00 app[web.1]: at Socket.dataReady (/app/node_modules/socksjs/socks.js:93:13)
2020-06-17T18:38:40.256029+00:00 app[web.1]: at Socket.emit (events.js:315:20)
2020-06-17T18:38:40.256030+00:00 app[web.1]: at emitReadable_ (_stream_readable.js:556:12)
2020-06-17T18:38:40.256030+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:83:21)
2020-06-17T18:38:40.263200+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-06-17T18:38:40.263391+00:00 app[web.1]: npm ERR! errno 1
2020-06-17T18:38:40.264251+00:00 app[web.1]: npm ERR! my_api#0.0.0 start: `node ./bin/www`
2020-06-17T18:38:40.264354+00:00 app[web.1]: npm ERR! Exit status 1
2020-06-17T18:38:40.264471+00:00 app[web.1]: npm ERR!
The code in question looks like this:
function new_db_connection() {
var sockConn = new SocksConnection(remote_options, sock_options);
var dbConnection = mysql.createConnection({
user: 'myusername',
database: 'mydatabase',
password: 'mypassword',
stream: sockConn
});
return [sockConn, dbConnection];
};
router.get('/myrequest', function(req, res, next) {
new_conn = new_db_connection();
dbConnection = new_conn[1];
sockConn = new_conn[0];
dbConnection.query('SELECT * from mytable', function(error, results, fields) {
if (error) {
res.send(JSON.stringify({
"status": 500,
"error": error,
"response": null
}));
//If there is error, we send the error in the error section with 500 status
} else {
res.send(JSON.stringify({
"status": 200,
"error": null,
"response": results
}));
//If there is no error, all is good and response is 200OK.
}
sockConn.dispose();
});
dbConnection.end()
});
I saw a similar question asked on here and the solution was to close the connection, but I do close the connection after each request, so I don't know where the issue is.
All help appreciated.
Try to change this
sockConn.dispose();
});
dbConnection.end()
});
in this
sockConn.dispose();
dbConnection.end()
});
});
Edit: reading better the error, it could be the problem is some where else.
Looking at the string 'SOCKS: Authentication failed', could it be that you or the SocksConnection remote endpoint changed something in the authentication?
Hope this helps.
You should take a look at the answer provided by #daniele Rici you need to end your connection properly; however, fixing this will not solve your initial question.
The issue is not with your code. Your issue is with connection configuration.
'fatal' on string 'SOCKS: Authentication failed'
Means there was an error with authentication.
var sockConn = new SocksConnection(remote_options, sock_options);
under the assumention you are using ther npm package socksjs from https://openbase.io/js/socksjs
It is documented that the correct way to initialize a sockjs connection is by doing the following:
var sock = new SocksConnection(remote_options, sock_options);
var sock = SocksConnection.connect(remote_options, sock_options, connect_handler);

MongoDb Connection Error when using Mongoose

I did not have a problem connecting to Atlas previously from my wifi at home. But I have issues at Starbucks. I have now switched to google fiber and I am getting this error.
at Pool.<anonymous> (/Users/j/Desktop/projects/templateApp/node_modules/mongodb-core/lib/topologies/server.js:431:11)
at Pool.emit (events.js:182:13)
at connect (/Users/j/Desktop/projects/templateApp/node_modules/mongodb-core/lib/connection/pool.js:557:14)
at callback (/Users/j/Desktop/projects/templateApp/node_modules/mongodb-core/lib/connection/connect.js:109:5)
at runCommand (/Users/j/Desktop/projects/templateApp/node_modules/mongodb-core/lib/connection/connect.js:129:7)
at Connection.errorHandler (/Users/j/Desktop/projects/templateApp/node_modules/mongodb-core/lib/connection/connect.js:321:5)
at Object.onceWrapper (events.js:273:13)
at Connection.emit (events.js:182:13)
at TLSSocket.<anonymous> (/Users/j/Desktop/projects/templateApp/node_modules/mongodb-core/lib/connection/connection.js:350:12)
at Object.onceWrapper (events.js:273:13)
at TLSSocket.emit (events.js:182:13)
at _handle.close (net.js:596:12)
at TCP.done [as _onclose] (_tls_wrap.js:383:7)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],```
I have checked my strings and they are correct.
My connection code is:
```mongoose.connect(process.env.dataBaseConnection, {
dbName: process.env.dataBaseName,
useFindAndModify: false,
useNewUrlParser: true
});```
The issue was that I needed to white list my new ip address.

authorization failed emailjs heroku

Using emailjs to send emails on my nodejs application.
However, it shows the error below when deployed on heroku, it's strange because application works locally
2018-01-28T22:28:24.689274+00:00 app[web.1]: { Error:
authorization.failed (timedout while connecting to smtp server)
2018-01-28T22:28:24.689286+00:00 app[web.1]: at module.exports
(/app/node_modules/emailjs/smtp/error.js:2:13)
2018-01-28T22:28:24.689290+00:00 app[web.1]: at failed
(/app/node_modules/emailjs/smtp/smtp.js:501:28)
2018-01-28T22:28:24.689292+00:00 app[web.1]: at response
(/app/node_modules/emailjs/smtp/smtp.js:506:13)
2018-01-28T22:28:24.689293+00:00 app[web.1]: at caller
(/app/node_modules/emailjs/smtp/smtp.js:48:14)
2018-01-28T22:28:24.689295+00:00 app[web.1]: at response
(/app/node_modules/emailjs/smtp/smtp.js:198:9)
2018-01-28T22:28:24.689296+00:00 app[web.1]: at caller
(/app/node_modules/emailjs/smtp/smtp.js:48:14)
2018-01-28T22:28:24.689298+00:00 app[web.1]: at TLSSocket.response
(/app/node_modules/emailjs/smtp/smtp.js:178:11)
2018-01-28T22:28:24.689300+00:00 app[web.1]: at Object.onceWrapper
(events.js:315:30)
2018-01-28T22:28:24.689302+00:00 app[web.1]: at emitOne
(events.js:116:13)
2018-01-28T22:28:24.689304+00:00 app[web.1]: at TLSSocket.emit
(events.js:211:7)
2018-01-28T22:28:24.689307+00:00 app[web.1]: code: 3,
2018-01-28T22:28:24.689308+00:00 app[web.1]: previous: { Error:
timedout while connecting to smtp server
2018-01-28T22:28:24.689310+00:00 app[web.1]: at module.exports
(/app/node_modules/emailjs/smtp/error.js:2:13)
2018-01-28T22:28:24.689312+00:00 app[web.1]: at TLSSocket.timedout
(/app/node_modules/emailjs/smtp/response.js:32:29)
2018-01-28T22:28:24.689313+00:00 app[web.1]: at Object.onceWrapper
(events.js:313:30)
2018-01-28T22:28:24.689314+00:00 app[web.1]: at emitNone
(events.js:106:13)
2018-01-28T22:28:24.689315+00:00 app[web.1]: at TLSSocket.emit
(events.js:208:7)
2018-01-28T22:28:24.689316+00:00 app[web.1]: at
TLSSocket.Socket._onTimeout (net.js:407:8)
2018-01-28T22:28:24.689318+00:00 app[web.1]: at ontimeout
(timers.js:475:11)
2018-01-28T22:28:24.689319+00:00 app[web.1]: at tryOnTimeout
(timers.js:310:5)
2018-01-28T22:28:24.689320+00:00 app[web.1]: at Timer.listOnTimeout
(timers.js:270:5) code: 4, smtp: undefined },
2018-01-28T22:28:24.689322+00:00 app[web.1]: smtp: undefined }
My code for stmp authentication
var server = email.server.connect({
user: email,
password: pass,
host: "smtp-mail.outlook.com",
tls: { ciphers: "SSLv3" }
});
I already tried
var https = require('https');
https.globalAgent.options.secureProtocol = 'SSLv3_method';
There's existing post for the similar problem, however, in this scenario i'm using outlook as opposed to other servcies

Node.js - events.js throw er

Before posting this question I went through a couple of SO posts with the same heading. All of them suggested me to restart my app..I tried that but I am still getting the error.
The code that is throwing the error is
app.post('/insert', function(request, response) {
var connection = mySequel.createConnection(ConnectionParams);
connection.connect();
var UserName = request.body.UserName;
// insert into userrecords values ("123456",DEFAULT,DEFAULT,DEFAULT,10);
var InsertQuery = "insert into userrecords values (" + UserName + ",DEFAULT,DEFAULT,DEFAULT,-10);";
connection.query(InsertQuery, function(error, result, fields) {
if (error) {
response.send("error");
console.log(error);
throw error;
} else {
// response.send("success");
console.log(result.insertId);
response.send(result.insertId);
}
});
connection.end();
});
The error caused by the code is this
events.js:160
2017-04-26T11:42:52.410094+00:00 app[web.1]:
2017-04-26T11:42:52.410092+00:00 app[web.1]: throw er; // Unhandled 'error' event
2017-04-26T11:42:52.410096+00:00 app[web.1]: Error: Quit inactivity timeout
2017-04-26T11:42:52.410097+00:00 app[web.1]: at Quit.<anonymous> (/app/node_modules/mysql/lib/protocol/Protocol.js:160:17)
2017-04-26T11:42:52.410093+00:00 app[web.1]: ^
2017-04-26T11:42:52.410098+00:00 app[web.1]: at emitNone (events.js:86:13)
2017-04-26T11:42:52.410099+00:00 app[web.1]: at Quit._onTimeout (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:127:8)
2017-04-26T11:42:52.410099+00:00 app[web.1]: at Quit.emit (events.js:185:7)
2017-04-26T11:42:52.410100+00:00 app[web.1]: at ontimeout (timers.js:380:14)
2017-04-26T11:42:52.410101+00:00 app[web.1]: at tryOnTimeout (timers.js:244:5)
2017-04-26T11:42:52.410102+00:00 app[web.1]: at Timer.listOnTimeout (timers.js:214:5)
I am hosting my app on Heroku and whenever I try to access this URL the app is crashing forcing me to restart the dyno. The data I am sending is being successfully inserted into the DB but the auto incremented primary key is not being returned to my app. Instead I get a server error.
JSON.stringify() returns the correct value once and after that the app crashed which maked the subsequent requests to fail.
How can I solve this problem?
Try converting your response into JSON before sending it.
Like
response.send(JSON.stringify(result.insertId));
I too once had a similar problem and I think that is how I solved it.
EDIT:
I went through the myql npm library and found something called Pool. Pool is used when you app needs to handle more than one request at a time. I suggest that you try that. There is a good example availble here.
I have same problem, i just downgrade to react-scripts#2.1.8
this is the steps:
cd my-app
npm install react-scripts#2.1.8
npm start

Categories

Resources