Error handling on request piping - javascript

I wrote simple proxy on nodejs and it looks like
var request = require( 'request' );
app.all( '/proxy/*', function( req, res ){
req.pipe( request({
url: config.backendUrl + req.params[0],
qs: req.query,
method: req.method
})).pipe( res );
});
It works fine if remote host is available, but if remote host is unavailable the whole node server crashes with unhandled exception
stream.js:94
throw er; // Unhandled stream error in pipe.
^
Error: connect ECONNREFUSED
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
How can I handle such errors?

Looking at the docs (https://github.com/mikeal/request) you should be able to do something along the following lines:
You can use the optional callback argument on request, for example:
app.all( '/proxy/*', function( req, res ){
req.pipe( request({
url: config.backendUrl + req.params[0],
qs: req.query,
method: req.method
}, function(error, response, body){
if (error.code === 'ECONNREFUSED'){
console.error('Refused connection');
} else {
throw error;
}
})).pipe( res );
});
Alternatively, you can catch an uncaught exception, with something like the following:
process.on('uncaughtException', function(err){
console.error('uncaughtException: ' + err.message);
console.error(err.stack);
process.exit(1); // exit with error
});

If you catch the uncaught exception for ECONNREFUSED make sure to restart your process. I saw in testing that the socket becomes unstable if you ignore the exception and simply try to re-connect.
Here's a great overview: http://shapeshed.com/uncaught-exceptions-in-node/
I ended up using the "forever" tool to restart my node process, with the following code:
process.on('uncaughtException', function(err){
//Is this our connection refused exception?
if( err.message.indexOf("ECONNREFUSED") > -1 )
{
//Safer to shut down instead of ignoring
//See: http://shapeshed.com/uncaught-exceptions-in-node/
console.error("Waiting for CLI connection to come up. Restarting in 2 second...");
setTimeout(shutdownProcess, 2000);
}
else
{
//This is some other exception..
console.error('uncaughtException: ' + err.message);
console.error(err.stack);
shutdownProcess();
}
});
//Desc: Restarts the process. Since forever is managing this process it's safe to shut down
// it will be restarted. If we ignore exceptions it could lead to unstable behavior.
// Exit and let the forever utility restart everything
function shutdownProcess()
{
process.exit(1); //exit with error
}

You should actually try to prevent the ECONNREFUSED exception from becoming uncaught:
var request = require( 'request' );
app.all( '/proxy/*', function( req, res ){
req.pipe( request({
url: config.backendUrl + req.params[0],
qs: req.query,
method: req.method
}))
.on('error', err => {
const msg = 'Error on connecting to the webservice.';
console.error(msg, err);
res.status(500).send(msg);
})
.pipe( res );
});
If you get an actual uncaught exception, then you should just let the application die.

Related

Nextjs API request only works locally

I am trying write to google sheet using 'google-spreadsheet' via Next.js API route. It works perfectly fine when I am testing locally. I can see the data being updated in the google sheet. However, when I deploy it to Vercel, it doesn't work. The 'Functions' log from Vercel shows the following error message.
Error authentication FetchError: request to https://www.googleapis.com/oauth2/v4/token failed, reason: Client network socket disconnected before secure TLS connection was established
at ClientRequest. (/var/task/node_modules/node-fetch/lib/index.js:1461:11)
at ClientRequest.emit (events.js:315:20)
at TLSSocket.socketErrorListener (_http_client.js:469:9)
at TLSSocket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
type: 'system',
errno: 'ECONNRESET',
code: 'ECONNRESET',
config: {
method: 'POST',
url: 'https://www.googleapis.com/oauth2/v4/token',
data: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: ....
Below is my code if that's any help.
export default async function addRowAPI(req, res) {
if (req.method === 'POST') {
try {
let doc;
try {
doc = new GoogleSpreadsheet(process.env.SPREADSHEET_ID);
} catch (error) {
console.log('error at line 15:', error);
}
try {
await doc.useServiceAccountAuth({
client_email: process.env.GOOGLE_SHEETS_CLIENT_EMAIL,
private_key: (process.env.GOOGLE_SHEETS_PRIVATE_KEY || '').replace(
/\\n/g,
'\n'
),
});
} catch (error) {
console.log('error authentication', error);
}
await doc.loadInfo();
console.log(doc.title);
const sheet = doc.sheetsByTitle['Test_Sheet'];
console.log(sheet.title);
console.log('addRow Doc:', doc);
const newRow = await sheet.addRow(req.body);
res.status(201).send();
} catch (error) {
res.status(500).json(error);
}
} else if (req.method === 'GET') {
res.status(200).json({ ping: 'pong' });
}
}
As mentioned in the comments, the error complains about an authentication issue which indicates wrong/non-existing credentials. Double-check you have all the environment variables properly set in Vercel.

AssertionError [ERR_ASSERTION]: handler (func) is required in mongodb

I am using mongooose to connect mongodb but i am getting following error
/Users/uchitkumar/api/node_modules/mongodb/lib/mongo_client.js:804
throw err;
^
AssertionError [ERR_ASSERTION]: handler (func) is required
at new AssertionError (internal/errors.js:315:11)
at _toss (/Users/uchitkumar/api/node_modules/assert-plus/assert.js:22:11)
at Function.out.(anonymous function) [as func] (/Users/uchitkumar/api/node_modules/assert-plus/assert.js:122:17)
at process (/Users/uchitkumar/api/node_modules/restify/lib/server.js:1352:20)
at argumentsToChain (/Users/uchitkumar/api/node_modules/restify/lib/server.js:1361:12)
at Server.serverMethod [as put] (/Users/uchitkumar/api/node_modules/restify/lib/server.js:1475:21)
my code for connection is as follow
server.listen(config.port, function() {
mongoose.connection.on('error', function(err) {
console.log('Mongoose default connection error: ' + err)
process.exit(1)
})
mongoose.connection.on('open', function(err) {
if (err) {
console.log('Mongoose default connection error: ' + err)
process.exit(1)
}
console.log(
'%s v%s ready to accept connections on port %s in %s environment.',
server.name,
config.version,
config.port,
config.env
)
require('./routes')
})
global.db = mongoose.connect(config.db.uri)
})
routes code
server.get('/', function indexHTML(req, res, next) {
fs.readFile(__dirname + '/../index.html', function (err, data) {
if (err) {
next(err);
return;
}
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.end(data);
next();
});
});
This was fine ... I changed something and now it stopped working with this error. The error is that it is not able to assert some function... in mongodb client. it needed a function. Is it asking to add some handler function? where to add that
Thank in advance
handler (func) is required is an error that is thrown by restify if one of your routes or middlewares is undefined.
For example:
server.put('/foo/');
This would also trigger it:
var myMidelware = undefined; // todo: define this
app.put('/route', myMiddleware, (req, res) => { /* todo: handle req */ })
That will throw the error handler (func) is required when it tries to validate that myMidelware is a function.
I don't see that in your posted routes code, but I think it's happening somehow. Do you have a PUT method defined somewhere?
(The same error would also happen with server.get(), server.post(), etc, but the [as put] in the stack trace indicates that it's choking on a server.put() call.)
See https://github.com/restify/node-restify/blob/v7.2.1/lib/server.js#L1386
Also, I don't believe the error has anything to do with mongodb; mongo is just in the stack because you run require('./routes') in the mongo connection open handler. The error is coming from your routes file. Annoyingly, mongo's error handling is loosing part of the stack trace. If you moved require('./routes') to outside of the mongo stuff, it would give you the proper stack trace.

Server shutting down itself using twitter api with nodejs and expressjs

I have an web app with using Twitter API. Main focus is to get the users that are not following back based on given twitter user name. It works fine until this point but when I get an error because of the fact that user name does not exist on Twitter, Server shuts down itself. Using Nodejs, expressjs.
The Error Message :
App is running on port : 3000
[ { code: 34, message: 'Sorry, that page does not exist.' } ]
_http_outgoing.js:494
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:494:11)
at ServerResponse.setHeader (_http_outgoing.js:501:3)
at ServerResponse.header (/home/ugurcan/dev/Projects/Twitter-App/twitter-api-app/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/home/ugurcan/dev/Projects/Twitter-App/twitter-api-app/node_modules/express/lib/response.js:170:12)
at /home/ugurcan/dev/Projects/Twitter-App/twitter-api-app/twitter-api.js:30:21
at Request._callback (/home/ugurcan/dev/Projects/Twitter-App/twitter-api-app/node_modules/twitter/lib/twitter.js:215:14)
at Request.self.callback (/home/ugurcan/dev/Projects/Twitter-App/twitter-api-app/node_modules/request/request.js:186:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (/home/ugurcan/dev/Projects/Twitter-App/twitter-api-app/node_modules/request/request.js:1163:10)
Problematic part of the code is below. The question is : How can I avoid this situation ? Or is it even possible ?
client.get('followers/ids', params, function(error, followers_results, response) {
if (error) {
res.send("error");
console.log(error);
}
let followers = followers_results.ids;
client.get('friends/ids', params, function(error, following_results, response ) {
if (error) {
res.send("error");
console.log(error);
}
let following = following_results.ids;
following.forEach(function(person){
if(followers.indexOf(person) === -1){
one_way_following.push(person);
}
});
// console.log(one_way_following);
one_way_following = one_way_following.slice(0, 100);
one_way_following_string = one_way_following.join();
// console.log("----------------------------------------------------");
// console.log(one_way_following_string);
// console.log("----------------------------------------------------");
client.get('users/lookup', {user_id: one_way_following_string}, function(error, user_results, response, next) {
if (error) {
res.send("error");
console.log(error);
}
user_results.forEach(function(user){
let userObject = {
name : user.name,
screen_name : user.screen_name,
avatar: user.profile_image_url
}
// console.log(user.name);
users.push(userObject);
})
res.render("results.ejs",{users:users});
// console.log(users);
});
});
});
This error is actually happening because, you can't call res.render("results.ejs",{users:users}); or res.send("error"); multiple times, because headers can't be changed once set.
Easiest solution would be to break out of the function once you've caught an error (if (error) { on line 2), or handle it appropriately (render something blank, or render your friendly error view).
For more information, about this, check out the answer to this question, Error: Can't set headers after they are sent to the client
Hope that helps :)

node.js Error: read ECONNRESET

I m running an Express 4 application, and I added some logic to router:
router.get('/pars', function(req, res, next) {
fetcher.parseXml(function(err, result){ //download files from ftp server, it can takes from 10 sec to 1 minute
if(err) {
console.log("router " + err);
res.render('index', { title: err });
}else {
console.log(result);
res.render('index', { title: 'Download finish' });
}
});
});
And added coressponding button to start index page, that send ajax to that '/pars' endpoint:
...
<button id="btn">Parse Data</button>
<script>
$( document ).ready(function() {
$('#btn').click(function () {
$.get(
"/pars",
onAjaxSuccess
);
});
function onAjaxSuccess(data) {
alert(data);
};
});
</script>
So all works fine and I sucesfully reloading page and downloading files from ftp using 'jsftp' module, but after some time (it may be 30 sec or 2 minutes ) I got error which crash all my app:
events.js:85
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at exports._errnoException (util.js:746:11)
at TCP.onread (net.js:559:26)
I found similar problem at Node js ECONNRESET
And added this 'catching' code to my app.js:
process.on('uncaughtException', function (err) {
console.error(err.stack);
console.log("Node NOT Exiting...");
});
Now app doesnt crashes, but spams that error from time to timeto my log, and all logic works fine.
I think issue can be in ftp.get:
Ftp.get(config.get("ftpDownloader:dir") + '/'+ fileName, __dirname + '/xml/' + fileName, function(hadErr) {
if (hadErr){
log.error('There was an error retrieving the file.' + hadErr);
ftpDonwloadCallback(hadErr);
}else{
log.info("XML WAS DOWNLOADED: " + fileName);
readFile(fileName, ftpDonwloadCallback);
}
});
Maybe somebody can help me how I can fix that problem?
depends on the error info:
events.js:85
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at exports._errnoException (util.js:746:11)
at TCP.onread (net.js:559:26)
it's caused by TCP connection. if the underlying socket receive a 'error' event, but no 'error' event listener, it will propagate and crash you process.
check your http server, add error event listener to it.
for example:
var server = http.createServer(function(request, response){ ... ... });
server.on('error', function(err) { ... ... });
if you want to catch the error from client, you can listener 'clientError' event.

Node.js POST causes [Error: socket hang up] code: 'ECONNRESET'

I created a sample to post data to a rest services and I found out that when I have non-ascii or non-latin character (please see data.firstName), my post request using TEST-REST.js will throw
error: { [Error: socket hang up] code: 'ECONNRESET' }.
// TEST-REST.js
var http = require('http');
var data = JSON.stringify({
firstName: 'JoaquÌn',
});
var options = {
host: '127.0.0.1',
port: 3000,
path: '/users',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
var req = http.request(options, function(res) {
var result = '';
res.on('data', function(chunk) {
result += chunk;
});
res.on('end', function() {
console.log(result);
});
});
req.on('error', function(err) {
console.log(err);
});
req.write(data);
req.end();
and on my rest services, it throw me error like this:
SyntaxError: Unexpected end of input Sun Sep 08 2013 23:25:02 GMT-0700 (PDT) - at Object.parse (native)
at IncomingMessage.<anonymous> (/Volumes/Data/Program_Data/GitHub/app/node_modules/express/node_modules/connect/lib/middleware/json.js:66:27) info at IncomingMessage.EventEmitter.emit (events.js:92:17)
at _stream_readable.js:920:16 : - - - [Mon, 09 Sep 2013 06:25:02 GMT] "POST /users HTTP/1.1" 400 - "-" "-"
at process._tickDomainCallback (node.js:459:13)
if I replace firstName value from 'JoaquÌn' to 'abc', everything works just fine. I think I'm missing something to support or escape to make it work.
does anyone have any idea how I solve this problem? I also tried following: require('querystring').escape(model.givenName), and it works but I'm not happy with it.
UPDATED
I found out that if I comment out: app.use(express.bodyParser());, the error disappears.
This is node's issue, not express's issue. https://github.com/visionmedia/express/issues/1749
to resolve, change from
'Content-Length': data.length
to
'Content-Length': Buffer.byteLength(data)
RULE OF THUMB
Always use Buffer.byteLength() when you want to find the content length of strings
UPDATED
We also should handle error gracefully on server side to prevent crashing by adding middleware to handle it.
app.use(function (error, req, res, next) {
if (!error) {
next();
} else {
console.error(error.stack);
res.send(500);
}
});
The problem is that if you don't handle this error and keep the server alive, this remote crash exploit could be used for a DOS attack. However, you can handle it and continue on, and still shut down the process when unhandled exceptions occur (which prevents you from running in undefined state -- a very bad thing).
The connect module handles the error and calls next(), sending back an object with the message body and status = 400. In your server code, you can add this after express.bodyParser():
var exit = function exit() {
setTimeout(function () {
process.exit(1);
}, 0);
};
app.use(function (error, req, res, next) {
if (error.status === 400) {
log.info(error.body);
return res.send(400);
}
log.error(error);
exit();
});

Categories