I am currently working on a web application as a learning project, which has a NodeJS backend and uses a MongoDB database; however, I believe my question is not specific to the technologies I am using.

When creating a RESTful API, I can appreciate that you should always respond to the client with the appropriate HTTP error code whenever your program encounters an error during execution (i.e. respond with some HTTP error code when MongoDB encounters an issue trying to fetch some data and provide some front end feedback about error). For example, you would typically do the following when creating an entry in a MongoDB database

            // create some data in my db
        }, (err, data) => {
            if (err) {
                // respond with 404
            } else {
                // respond with 200 and serve data to client

However, I'm unsure as to how I should handle errors in instances outside of the above. For example, for code that executes on a regular interval that is not requested by a client, what should I do with the error given that I would expect this routine code to run smoothly all the time?

Is the standard engineering practice to have an alert sent to me when an error is thrown? My current solution is to simply console.log the error which seems completely pointless since I wouldn't be watching my server 24/7 so any logged errors would go unnoticed.

            // create some data in my db
        }, (err, data) => {
            if (err) {
                // what should I do here??
                // currently pointlessly console.logging here...
            } else {
                // do something on success

Below is another instance where I'm unsure of how I should handle error. Here, I'm sending a slack notification every time something happens to monitor my application and I would expect this to work every time. If a message fails to send, it's no big deal but what do I do with the error?

slack.webhook({ // posts message to slack
        channel: "#general",
        username: "bot",
        text: `Yay success`
    }, function(err, response) {
        // what should I do if the slack message doesn't send??
        // Send an alert to tell me that there is an issue with sending messages to slack? 
        // Force the app to keep trying to resend a message until it's successful?

1 Answer 1


So, there is not a standard way that I'm aware of, but I'll tell you whats common in production in my experience.

Broadly speaking, you have two kinds of errors: things that admins actually need to take action on,and those that users need to take action on.

In HTTP land, the former are 500 series errors and the latter are 400 series (broadly, there are exceptions, if you'll pardon the pun)

First, any HTTP errors can and should be handled by middleware. So each request handler can just call next(err) and then you can use a middleware like praeter (full disclosure: I wrote that) to figure out what HTTP response code to use, etc. Any admin-action errors can also be logged to stderr. (console.err() unless you're using Winston or the like)

Second, yes, the best way (imo) to handle other errors and warnings is through logging. So use a logging framework or console commands, but I believe it should all go to stdout and stderr. Why? Well, that's the most portable, maintainable, simple, and standards-compliant way.

What about admins having to check logs, you ask? Well, I believe in each piece of software doing one set of things well. Your web app is not primarily an admin tool, so don't try and make it do things specifically to support it's own maintenance. Instead, configure a log aggregator like ElasticStack or use a service like Loggly. Those can both parse your logs and send notification to you if something is wrong. New Relic is another good service for watching your app health and there are others. Pick the one that fits your price point (many have free tiers) if you don't want to host your own.

  • 1
    thanks so much for this detailed answer. Will look into all your suggestions :)
    – ptk
    Mar 29, 2018 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.