Meteor: send an email if 500 errors occur

I am running some Meteor applications on ec2 and want to receive an email notification if a user has a 500 error. Is there a way to catch all 500 errors and process them in one place?

+4
source share
1 answer

All server-side errors should be logged in your server. I run the Meteor application on an EC2 instance that I installed with a script at https://github.com/storyandstructure/meteorite-ec2-install , and it configures things such that my application logs are saved under /var/log/myapp.log ; you probably have something like that.

As for receiving email whenever an error occurs, there are two options. For all errors that result from your code (i.e., not from the package), simply find where you placed the throw new Meteor.Error and add the Email.send(...) immediately after it. (Of course, make sure you add the email package and configure a valid MAIL_URL.)

You can even catch some errors caused by packages this way if they return anything back to the calling function; for example, an email package throws an error message if send () does not work, and you can catch in the try-catch in the code where you call Email.send() (and then, um, you could call Email.send again, which I hope will work a second time, although if the failure the first time was due to the recipient, perhaps the email would do the trick instead).

For errors that result from packages that do not return any feedback to the calling function, well, this is more complicated. You will need to create a separate application, also running on the server, that will read the server log file so often and look for Exception or Error: at the beginning of the line. Whenever this text is found, your monitoring application sends you an email (presumably also by copying some of the last lines from the journal into the body of the message). This other application does not have to be a Meteor application, in fact it would be better if it were a simple node.js application, rather than listening to any port. We hope that the only packages whose errors you are worried about, such as email, all throw their errors in a chain so that your application logic can catch them and do something with them.

0
source

Source: https://habr.com/ru/post/1496593/


All Articles