docs: function initErrorHandler (config) {
      // add error handler
      config.express.use(function(error, req, res, next) {
          // error must be object with stack
          if (typeof error !== 'object' || typeof error.stack !== 'string') {
              error = new Error(error)
          }
          // whether or not response will be JSON
          var json = req.query.json === '1' || req.query.json === 'true' || (req.xhr && req.query.json !== '0' && req.query.json !== 'false') ? true : false
          // set clean stack
          if (defined(stackUtils)) {
              error.stack = stackUtils.clean(error.stack)
          }
          // if error contains cookies then set cookies with response
          if (error.cookies) {
              // set cookies
              _.each(error.cookies, (value, name) => {
                  setCookie(name, res, value)
              })
          }
          // get http code from error code
          var code = error.code
          // if code is not between 300 and 500 then set to 500
          if (!(code >= 300 && code <= 500)) {
              code = error.code = 500
          }
          // json response is expected
          if (json) {
              // code is for redirect
              if (code === 301 || code === 302) {
                  res.json({
                      code: code,
                      redirect: true,
                      url: error.url,
                  })
              }
              // if error is 409 conflict data should be current instance
              else if (code === 409) {
                  // set http status code
                  res.status(code)
                  // send current instance data
                  res.json(error.data)
              }
              // code is other error
              else {
                  // get message
                  var message = error.message || 'Unknown error'
                  // build response
                  var resData = {
                      error: message,
                      code: code,
                      data: error.data,
                  }
                  // add stack if in dev
                  if (!isProd) {
                      resData.stack = error.stack.split('\n')
                      // remove last element from stack which is empty
                      resData.stack.pop()
                  }
                  // set http status code
                  res.status(code)
                  // send current instance data
                  res.json(resData)
              }
          }
          // html response is expected
          else {
              // code is for redirect
              if (code === 301 || code === 302) {
                  res.redirect(code, error.url)
              }
              // code is error
              else {
                  // set http status code
                  res.status(code)
                  // create pretty printed json data
                  if (error.data) {
                      error.prettyData = JSON.stringify(error.data, null, 4)
                  }
                  // build data for error template
                  var data = {
                      error: error,
                      production: isProd,
                      session: req.session,
                  }
                  // add assets to template data
                  _.merge(data, config.assets)
                  // render error page
                  res.render('error', data)
              }
          }
      })
  }