Skip to content

Instantly share code, notes, and snippets.

@adon-at-work
Last active August 29, 2015 14:17
Show Gist options
  • Save adon-at-work/81842b80c9612feeb835 to your computer and use it in GitHub Desktop.
Save adon-at-work/81842b80c9612feeb835 to your computer and use it in GitHub Desktop.
express-error-handling-examples.js
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('hello world! '
+ '<br/><a href="error">An example throwing error</a>'
+ '<br/><a href="error-handled">An example throwing error, but handled').end();
});
app.get('/error', function (req, res) {
JSON.parse('invalidJSON');
});
app.get('/error-handled', function (req, res) {
JSON.parse('invalidJSON');
});
// Error handler registered at last to catch all errors thrown
// Reference: http://expressjs.com/guide/error-handling.html
app.use('/error-handled', function (err, req, res, next) {
console.error(err);
res.status(500).send('Error occurred. Contact admin!');
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment