Unhandled Error in Node JS Application - javascript

I have unhandled piece of code in my NodeJS apps data layer that connects to DB. I am explicitly generating error in my code, at the same time not catching it. This is:
AdminRoleData.prototype.getRoleByRoleId = function (params) {
var connection = new xtrDatabaseConnection();
var query = "CALL role_getRoleByRoleId(:p_RoleId)";
var replacements = { p_RoleId: params.roleId };
replacements = null;
return connection.executeQuery(query, Sequelize.QueryTypes.RAW, replacements);
}
replacements = null; this is where i am generating error. No error handling at the moment here. I want to capture these sort of unhandled errors in my application. And want to log them on to file as unhandled exceptions or errors.
process.on('uncaughtException', (err) => {
logger.log('whoops! There was an uncaught error', err);
// do a graceful shutdown,
// close the database connection etc.
process.exit(1);
});
My question is that my "uncaughtException" is not getting called. Any help ? Or best practices in such scenarios. Or to catch them globally at some centralized place.

If you are using express, you would first define all your routes as:
app.use('/api/users', users);
app.use('/api/accounts', accounts);
...
Then you could catch the 404 errors like so:
app.use(function(req, res, next) {
console.log("A request triggered 404");
next(err);
});
Finally the block, to catch all errors:
// error handler
app.use(function(err, req, res, next) {
console.log(err.message);
res.status(err.status || 500);
res.render('error');
});
Note: The order of the functions is important. For example if you put the 404 handler before the other routes then all you responses will be 404.
Node will traverse the routes in the order in which you declare them until it finds a match.

simply put the code inside a try-catch block and log them ....
try {
//your code that might cause an error to occur
}catch (e){
console.log("ERROR:" + e.toString());
}

Related

node express try catch not working as expected

I'm a beginner in Node/Express.js and I'm trying out some try catch logic but doesn't seem to work as expected.
app.get("/tasks/:id", async (req, res) => {
try {
const _id = req.params.id;
const task = await Task.findById(_id);
if (!task) {
return res.status(404).send("task not found");
}
res.send(task);
} catch (error) {
res.status(500).send("Internal server error");
}
});
from this code sample, I'm making a query to Mongo DB to fetch some task but the problem is that if the task is not found, instead of running through the if statement, the program jumps directly to the catch block hence the if condition is not checked thus resulting to a different error. How can I fix this issue?
This is simply how MongooseJS works - check their Promises documentation and you will see that if a document is not found, an error will be thrown. If you were not using await, the findById() documentation shows how an error is returned if it cannot be found:
// find adventure by id and execute
Adventure.findById(id, function (err, adventure) {});
You can simply modify your route to look like the following:
app.get("/tasks/:id", async (req, res) => {
let task;
try {
task = await Task.findById(req.params.id);
} catch (error) {
return res.status(404).send("task not found");
}
/* perform other checks or actions here if desired */
res.send(task);
});
This is a little more flexible if you want to perform other error-checking; task is declared first so it can be accessible outside the try/catch block, and a 404 error is thrown if the task does not exist.
You can also look at the exists method which will return true or false based on if the document exists, but since you need the actual result that does not make as much sense.
You don't indicate what Task is, but it appears that it is rejecting when it doesn't find anything, rather than returning a false value (which is what you seem to be expecting).
Given that, you should probably just handle the error that it is throwing with something like
} catch ( error ) {
// Will need to adapt this if to something that identifies the error thrown by `getById`
if ( error.message.includes( 'not found' ) ) {
res.status( 404 ).send( 'task not found' );
} else {
res.status( 500 ).send( 'Internal server error' );
}
}

Proper error handling in node using try catch

Im new to javascript programming and i am required to make a web app. Node.js will be used as the js runtime environment. In order to minimize the amount of time needed for debugging as the app develops I would like to implement a robust error handling scheme. However, due to my limited background I am not sure if what I am implementing is best practise or if it is even adequate. So any feedback will be accepted.
The function is asynchronous and will use catch to detect if any errors occurred while operating. A try-catch statement will be used to catch any errors. This was done in order to allow for individual error identification from the functions. My aim is to propagate the errors up to the calling function that will handle it in the highest level catch statement (in my case where it is logged *this will change eventually). Any feedback?
create: async function(req, res) {
let data = JSON.parse(req.body.data);
let result;
let request_body;
let sub_networks_lora;
try {
request_body = sub_network_api_request_data(data, 1);
result = await lora_app_server.create_applications(request_body)
.catch(err => {
//Error updating application on lora app server
throw err;
});
await db_sub_networks.create_sub_network(result.data.id, data.sub_network_name, data.network_id)
.catch(err => {
throw err;
//Error creating sub network in database
});
sub_networks_lora = await get_sub_networks()
.catch(err => {
throw err;
//Error getting networks from lora app server
})
sub_networks_lora = JSON.stringify(sub_networks_lora);
res.status(200).send({
sub_networks_lora
});
} catch (err) {
console.log(err)
} finally {}
}

throw error versus normal return in express

I know how to write simple API using node.js (express). But now I'm confused and couldn't differentiate this two block of code
if(err){ return res.status(500).json(err) }
return res.json(result)
versus
if(err) { throw new Error(err) }
return res.json(result)
What is the standard for a API response? I simply return 2 property, like
if(err){ return res.json({ status: false, msg: 'user password is incorrect }) }
return ({ status: true, msg: result.token })
What's wrong with my approach and why we should use throw?
You don't generally want to throw an error in Express at all, because unless it's caught it will crash the process without giving your user warning, and it's not easy to catch the error and maintain the request context to do so otherwise.
Instead the choice in an Express handler should be between directly returning an error response (as in your example) and calling next(err). In my apps I always do the latter, because it lets me set up error handling middlware to always and consistently handle the various problem cases.
Example below:
app.get('/something', (req, res, next) => {
// whatever database call or the like
Something.find({ name: 'something'}, (err, thing) => {
// some DB error, we don't know what.
if (err) return next(err);
// No error, but thing wasn't found
// In this case, I've defined a NotFoundError that extends Error and has a property called statusCode set to 404.
if (!thing) return next(new NotFoundError('Thing was not found'));
return res.json(thing);
});
});
Then some middleware for handling errors like so:
app.use((err, req, res, next) => {
// log the error; normally I'd use debug.js or similar, but for simplicity just console in this example
console.error(err);
// Check to see if we have already defined the status code
if (err.statusCode){
// In production, you'd want to make sure that err.message is 'safe' for users to see, or else use a different value there
return res.status(err.statusCode).json({ message: err.message });
}
return res.status(500).json({ message: 'An error has occurred, please contact the system administrator if it continues.' });
});
Note that nearly everything in Express is done through middleware.

How to construct a non-custom error by Hand in Sails.js

I am writing a Service to be used by my Controllers in my Sails.js application.
If an error is thrown in the Service, I would like to return a error. I am used to the syntax of function callback(err, bar){ if (err) return error}. I am in a slightly different case, though. In the following code
function callBack(err, uploadedFiles){
if (err) return err;
if (uploadedFiles.length == {
return foo; //This is what needs to be of type "err"
}
});
}
}
So do I create a JSON object like
return { error: "error", message: 404}
I am not sure how to do this.
Why are you not using sails functions for responses? I am also new so excuse me if said something silly.
If you want to send a 404 status code which is for Not Found errors the you can use sails provided response res.notFound() like this:
return res.notFound({
message: "Some Message Here"
});
If you want to show completely custom message without any status code like 404 then use send() instead like this:
return res.send({
status: "404 or anything you like",
message: "Some Message Here"
});
Sorry, res.send() will also generate a status code but it will be 200 which means OK, so its a success code.
I believe it is:
{success:false, error: err}
You can raise your 404 error in your Services like this (assuming your service needs to work asynchronously):
var err = new Error('your message');
err.status = 404; // could be other HTTP status
return cb(err);
And in your calling function, you can handle the error like this:
SomeServices.somefunction(options, function (err, data) {
// res.negotiate(err) will automatically trigger res.notFound if err.status is 404
if (err) return res.negotiate(err);
/* your logic if not 404 */
}
The codes are quite simple here. Is that what you want?

Node.js/Express - Render error when page not found

I have the following controller/route definition in Node.js (using Express and Mongoose). What would be the leanest most appropriate way to handle Error when the user requests a page that does not exist?
app.get('/page/:pagetitle', function(req, res) {
Page.findOne({ title: req.params.pagetitle}, function(error, page) {
res.render('pages/page_show.ejs',
{ locals: {
title: 'ClrTouch | ' + page.title,
page:page
}
});
});
});
It currently breaks my app. I believe because I'm not doing anything with the error i'm just passing it to the view like a success?
TypeError: Cannot read property 'title' of null
Thanks much.
Check out the express error-pages example. The principle is to register your app routes first, then you register a catch all 404 handler for all other requests that do not map to a route. Finally, a 500 handler is registered, as follows:
// "app.router" positions our routes
// specifically above the middleware
// assigned below
app.use(app.router);
// Since this is the last non-error-handling
// middleware use()d, we assume 404, as nothing else
// responded.
app.use(function(req, res, next){
// the status option, or res.statusCode = 404
// are equivalent, however with the option we
// get the "status" local available as well
res.render('404', { status: 404, url: req.url });
});
// error-handling middleware, take the same form
// as regular middleware, however they require an
// arity of 4, aka the signature (err, req, res, next).
// when connect has an error, it will invoke ONLY error-handling
// middleware.
// If we were to next() here any remaining non-error-handling
// middleware would then be executed, or if we next(err) to
// continue passing the error, only error-handling middleware
// would remain being executed, however here
// we simply respond with an error page.
app.use(function(err, req, res, next){
// we may use properties of the error object
// here and next(err) appropriately, or if
// we possibly recovered from the error, simply next().
res.render('500', {
status: err.status || 500
, error: err
});
});
One of the major problems with Node.JS is that there is no clean error catching. The conventional way is usually for every callback function, the first argument is the not null if there is an error, so for example:
function( error, page ){
if( error != null ){
showErrorPage( error, req, res );
return;
}
...Page exists...
}
Things can get ugly after a while with too many callbacks, and I recommend using something like async, so that if there is one error, it goes directly to an error callback.
EDIT: You can also use express error handling.

Categories

Resources