Silently reporting errors to Papertrail - node.js

I'm working with a large Node.js application on Heroku, with logging maintained by Papertrail. I have a central error handling and logging function which, at the moment, just logs an error to the console, displays a generic "An error occurred!" dialog, and redirects a client to a specific page (depending on where the error occurred). But neither Papertrail nor Heroku detect this as a real error, and I don't get any sort of notifications or alerts if and when they occur.
At the moment, this is my function:
utilities.errorLogger = (err) => {
console.error(err);
};
I've tried to throw the error, which works like below:
utilities.errorLogger = (err) => {
throw new Error(err);
};
But then the error is displayed to the client, rather than being redirected away, leaving the end user confused. Similarly, putting the error in a try-catch block does not change anything from what I currently have (the error is logged but not picked up on by Papertrail or Heroku).
utilities.errorLogger = (err) => {
try {
throw new Error(err);
}
catch (err) {
console.error(err);
}
};
How can I silently throw an error for Papertrail and Heroku to pick up on and treat as an error, without needing to send the error to the client? I'd like to be able to silently handle the error and have the reporting go on in the background, rather than sending any of the error details to the client.

Ended up finding out the answer. I'm using KeystoneJS which comes with a default error handler that I hadn't seen before; I've modified it now to just redirect people while still being able to log the error.

Related

How to show the error alerts on UI whenever the catch error or console error comes and the server stops in localhost or in production side (heroku)?

I want a solution for how I can write the code in nodejs so that whenever console error occurs I want to send the alert erros on UI sid to the user so that he can go back or refresh or login again instead of blocking the page/ website in between whenever the application error logs comes in heroku.
Please provide me a solution for this!!!
Thanks!
Edit:
I am asking for general only. Means just like I was testing my web app on heroku after making live and in between while testing an error occured and I got redirected to the heroku application error log page like this below. So I just want to ignore this and instead of this an alert should appear telling the user to go back or login again as some error occured. but not to break the page in between like this.
:(
The server can't send the response to the client if it crashes completely. When a server crashes it means that your code is not handling the error properly.
As you didn't specify which programming language or framework you are using. I assume that it is Node.js as you mentioned .catch()
In this case, you should have a try/catch block in your code or a .catch if you are using promises. The error occurred but the server won't just crash completly.
You will need to have something similar as below in your route handlers:
Async/Await:
try{
// Do stuff
}catch(err){ // Bad things happen
// Log the error so you know what went wrong
console.log(err)
// Send the error response to the frontend
res.status(500).json({msg:'Server Error'})
}
Promises:
something
.then(//Do stuff)
.catch(err => { // Bad things happened
console.log(err)
// Send the error response to the frontend
res.status(500).json({msg:'Server Error'})
})

catch never outputs the console even though I know it's failing

I have my mongodb service stopped, so I know that my front end is not connected to my DB. I am using react and express.
Upon my app starting, I want to indicate that to the user somehow the server is offline so I figured if my original get call for users fails, then the server is offline.
I'm doing a simple call:
componentDidMount () {
axios.get ('/api/users')
.then ((res) => this.setState(
{ users : res.data }
))
.catch ((error) => {
//console.error(error);
console.log('error found : offline');
});
}
But nothing happens in situation. I never get the catch call for the console. Am I going about this wrong? I'm new to backend so this is all a learning experience for me.
I was going to set a failed flag and then render a display error for the user and then retry the connection every 1500ms or something (is that bad programming?).

Meteor - How to handle external API connection errors?

I'm using a few external API's (some in timers, every minute or so) and sometimes I get some connection errors because of network problems or because the external systems are down for some reason. When I get this errors, the app restarts, and if the error persists the app continues restarting.
Any ideas on how can I ignore the connection error and keep the app running?
Code Example:
try {
var req = https.request(options, callback);
req.write(JSON.stringify(params));
req.end();
} catch (e) {
throw e;
}
Based on your code example. You're doing throw e inside your try catch. Essentially, you're catching an error and then throwing the error. Just do console.error(err) or however you want to handle that error, without throwing. This is what will cause your instance to stop.

Unknown error during authentication

I'm using PouchDB 4.0. To test the credentials to a remote CouchDB server, I use the following code:
testCredentials: function(credentials, callback){
var remoteDb = new PouchDB('http://X.X.X.X/dbName', {
auth: {
username: credentials.username,
password: credentials.password
}
});
remoteDb.info(function(err, info) {
if (err) {
if(err.status == 401)
callback("unauthorized");
else {
console.log(err);
callback("error");
}
} else {
callback("success");
}
});
}
When I pass invalid credentials (e.g. existing user but invalid password), I see a 401 error returned inside the CouchDB log file. However, PouchDb always returns the following 500 error:
{"status":500,
"name":"unknown_error",
"message":"Database encountered an unknown error",
"error":true}
The same problem occurs when using the pouchdb-authentication plugin.
This code worked perfectly in previous versions of PouchDB.
Can you file a bug with steps to reproduce at the Github issues page? Seems to be a new bug in PouchDB. Also if you could print out the logs from the error (might be an HTTP error, check the "Network" tab), then that would be helpful too.
"Database encountered an unknown error" is a generic error message that PouchDB throws up when it hits an error it doesn't know how to handle, so posting or screenshotting that other error would be very helpful. :)
Also, if you are using pouchdb-authentication, then you typically don't need to add the auth option when creating a new PouchDB; that option is for plain http-based authentication, whereas pouchdb-authentication uses cookie authentication.

Debugging in node.js

I build a server which get many requests and response to them.
In some cases, there is an error which cause the server to crush:
events.js:72
throw er; // Unhandled 'error' event
^
Error: ENOENT, open '/mnt/ace/0/file'
I have two problems:
the stack trace doesn't give me any information about the line in my application that cause this exception (I can't do manually debugging because it happens just when I get 1000 request or more).
I don't want that my server ould crush. I prefer that it will raise an exception, but will continue to work.
What the best implementation for this?
You can listen for that kind of stuff and not have it crash the app, but that's not always a great idea.
process.on('uncaughtException', function(err) {
console.log('Something bad happened');
console.log(err.stack);
});
In your case, have you tried checking ulimit settings? You may be having problems opening file handles under loads of 1000+.
Another way of thinking about this is to use domains (if you're using >= 0.8). Domains give you a finer grain of control over how you handle errors based on what contexts cause them.
var domain = require('domain').create();
domain.on('error', function(err) {
console.log(err);
});
domain.run(function() {
// Your code that might throw
});

Resources