Views & Templating Engines
Last updated: 4 - 5 - 2019

ZinkyJS offers a way to handle all errors in one place instead of trying to catch them in every method and function.

Catcher

Define a catcher function in Zinky instance (app) that takes req and res as parameters. The thrown error is found in req.error.

Eg:

 const Zinky = require('zinky');

var app = new Zinky({
  aliases: {
    '': 'home'
    'customers': 'users'
  },
  catcher: function(req, res) {
    console.log(req.error);
    res.deliver(500, 'Oooops something went wrong!');
  }
});

app.listen();

Now we'll simulate an error to test the catcher. Add this operation to 'app_modules/home/index.js':

 GET_simulate(req, res) {
  a = b;
}

Visit localhost:3000/simulate

Even if we don't define a catcher, ZinkyJS has built-in function that logs the error then responds 500 as status code and 'Internal Server Error' as message. Here is its code:

 catcher(req, res) {
  console.log(req.error);
  res.deliver(500, 'Internal Server Error');
}
Next Course: Serving Files