S
S
Stanislav2018-12-09 02:16:07
Node.js
Stanislav, 2018-12-09 02:16:07

How to catch a URL with an error in a template engine?

I ran into a problem, a big project, decent traffic, and from time to time errors fly out in the pm2 logs

TypeError: Cannot read property 'href' of undefined
    at eval (eval at wrap (/var/www/site.ru/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:19:718)
    at template (eval at wrap (/var/www/site.ru/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:41:78)
    at Object.exports.renderFile (/var/www/site.ru/node_modules/pug/lib/index.js:427:38)
    at Object.exports.renderFile (/var/www/site.ru/node_modules/pug/lib/index.js:417:21)
    at View.exports.__express [as engine] (/var/www/site.ru/node_modules/pug/lib/index.js:464:11)
    at View.render (/var/www/site.ru/node_modules/express/lib/view.js:135:8)
    at tryRender (/var/www/site.ru/node_modules/express/lib/application.js:640:10)
    at Function.render (/var/www/site.ru/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/var/www/site.ru/node_modules/express/lib/response.js:1008:7)
    at /var/www/site.ru/index.js:109:14
    at Layer.handle_error (/var/www/site.ru/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/var/www/site.ru/node_modules/express/lib/router/index.js:315:13)
    at /var/www/site.ru/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/var/www/site.ru/node_modules/express/lib/router/index.js:335:12)
    at next (/var/www/site.ru/node_modules/express/lib/router/index.js:275:10)
    at Layer.handle_error (/var/www/site.ru/node_modules/express/lib/router/layer.js:67:12)
    at trim_prefix (/var/www/site.ru/node_modules/express/lib/router/index.js:315:13)
    at /var/www/site.ru/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/var/www/site.ru/node_modules/express/lib/router/index.js:335:12)
    at next (/var/www/site.ru/node_modules/express/lib/router/index.js:275:10)
    at Layer.handle_error (/var/www/site.ru/node_modules/express/lib/router/layer.js:67:12)
    at trim_prefix (/var/www/site.ru/node_modules/express/lib/router/index.js:315:13)

I revised all the URLs, and could not reproduce the error, I know the area where the error crashes, I know what should be there. For some reason, the server does not pick up the object on some page, 3-4 errors per day.
In index.js
// error handler
app.use(function(e, request, response, next) {
    if (request.headers['x-requested-with'] == 'XMLHttpRequest') return response.status(e.status || 403), response.send(e.message)

    if(e.code == 'EBADCSRFTOKEN') e.message = 'Переданный вами токен недействителен ;('
    response.locals.message = e.message
    response.locals.error = request.app.get('env') === 'development' ? e : {}
  
    response.status(e.status || 500)
    if(e.status > 404) console.log(e)
    response.render('error', { page: {
        name: `${e.status} ${e.message}`
    } })
})

Line 104 is Before the error catching code, a middleware is connected with Which creates a container for all pages with a data object, and for some reason this object is not transferred to layout.pug on some page, although I reproduced all the error pages and everything seems to be buzzing. Is it possible to identify on which page the error has taken off? I already had an idea to check the frontend for 500 with js and write it to some file, but it’s even )) response.render('error', { page: {
app.use(require('./libs/language'))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question