module.exports = function(app) {
// --------------------------------------------------------------------------------------
const mail = require( './mail' );
// --------------------------------------------------------------------------------------
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  });
// --------------------------------------------------------------------------------------
// error handlers
// --------------------------------------------------------------------------------------
  // development error handler
  // will print stacktrace
  if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
      if(err.status != 404)         // do not log 404
        console.error(err);         // log error to console

      res.status(err.status || 500);
      res.render('error', {
        message: err.message,
        error: err
      });
    });
  }
// --------------------------------------------------------------------------------------
  // production error handler
  // no stacktraces leaked to user
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
     
    var error =  { status : err.status };
     
    if(err.status == undefined) {       // server failure
      error.stack = "V aplikaci došlo k chybě. Hlášení o chybě bylo odesláno vývojářům.";
      mail.send_error_report(err);    // send error report to developers
    }
    else {
      if(err.status == 404)
        error.stack = "Stránka nenalezena.";
      else if(err.status == 401)
        error.stack = "Uživatel nepřihlášen.";
      else if(err.status == 400)
        error.stack = "";   // nothing more to do here
      else
        error.stack = "Neznámá chyba.";
    }

    res.render('error', {
      message: err.message,
      error: error
    });
  });
// --------------------------------------------------------------------------------------
}
// --------------------------------------------------------------------------------------

@@ -350,17 +350,26 @@
 or('
-Not Found
+Str%C3%A1nka nenalezena
 ');%0A
@@ -1254,47 +1254,27 @@
 var 
-error =  %7B status : err.status %7D;%0A     
+msg = err.message;%0A
 %0A   
@@ -1330,35 +1330,27 @@
 ilure%0A      
-error.stack
+msg
  = %22V aplika
@@ -1498,24 +1498,16 @@
    else 
-%7B%0A      
 if(err.s
@@ -1523,360 +1523,139 @@
 404)
+ %7B
 %0A      
-  error.stack = %22Str%C3%A1nka nenalezena.%22;%0A      else if(err.status == 401)%0A        error.stack = %22U%C5%BEivatel nep%C5%99ihl%C3%A1%C5%A1en.%22;%0A      else if(err.status == 400)%0A        error.stack = %22%22;   // nothing more to do here%0A      else%0A        error.stack = %22Nezn%C3%A1m%C3%A1 chyba.%22;%0A    %7D%0A%0A
+res.render('error', %7B%0A        message: err.message,%0A        error: %7B%7D%0A      %7D);%0A    %7D%0A    else %7B%0A   
    
- 
 res.
-r
 end
-er('error', %7B%0A      message: err.message,%0A      error: error
+(msg);
 %0A    %7D
-);
 %0A  %7D

rewrite error handling