<commit_before>
const domain  = require('domain');

const config  = require('../config');
const logger  = require('../logger');
const mailer  = require('../mailer');

//
const getURL = (req) => {
  return [
    req.protocol,
    '://',
    req.headers['x-forwarded-host'] || req.get('host'),
    req.originalUrl
  ].join('');
};

//
const formatMessage = (req, err) => {
  const url = getURL(req);
  const message = `
    <h1>${url}</h1>
    <pre>${err.stack}</pre>
    <table cellspacing="10">
      <tr><td>URL:</td><td>${req.method} ${url}</td></tr>
      <tr><td>req.body:</td><td>${JSON.stringify(req.body)}</td></tr>
      <tr><td>req.session:</td><td>${JSON.stringify(req.session)}</td></tr>
      <tr><td>Referer:</td><td>${req.get('Referer')}</td></tr>
      <tr><td>User-Agent:</td><td>${req.headers['user-agent']}</td></tr>
    </table>
  `;
  return message;
};

// log and show error
const handle = (err, req, res) => {

  // uri error
  if (err instanceof URIError) {
    return res.status(404).render('errors/404');
  }

  logger.error(`${req.method} ${getURL(req)} : ${err}`);
  logger.error(err.stack);

  if (config.mailcrashto) {
    mailer.send('crash', {
      to:       config.mailcrashto,
      subject:  `[${config.appname}] Crash: ${err}`,
      body:     formatMessage(req, err)
    });
  }
  
  if (!res._headerSent) {
    // show error
    if (config.env === 'production') {
      return res.status(500).render('errors/500');
    }

    //
    const stacktrace = `
      <h1>${req.method}: ${req.originalUrl}</h1>
      <pre>${err.stack}</pre>
    `;
    res.status(500).send(stacktrace);
  }
<commit_msg>Handle json syntax errors<commit_after>
const domain  = require('domain');

const config  = require('../config');
const logger  = require('../logger');
const mailer  = require('../mailer');

//
const getURL = (req) => {
  return [
    req.protocol,
    '://',
    req.headers['x-forwarded-host'] || req.get('host'),
    req.originalUrl
  ].join('');
};

//
const formatMessage = (req, err) => {
  const url = getURL(req);
  const message = `
    <h1>${url}</h1>
    <pre>${err.stack}</pre>
    <table cellspacing="10">
      <tr><td>URL:</td><td>${req.method} ${url}</td></tr>
      <tr><td>req.body:</td><td>${JSON.stringify(req.body)}</td></tr>
      <tr><td>req.session:</td><td>${JSON.stringify(req.session)}</td></tr>
      <tr><td>Referer:</td><td>${req.get('Referer')}</td></tr>
      <tr><td>User-Agent:</td><td>${req.headers['user-agent']}</td></tr>
    </table>
  `;
  return message;
};

// log and show error
const handle = (err, req, res) => {

  // uri error
  if (err instanceof URIError) {
    return res.status(404).render('errors/404');
  }

  // Syntax error (json format)
  if (err instanceof SyntaxError) {
    return res.status(500).render('errors/500');
  }

  logger.error(`${req.method} ${getURL(req)} : ${err}`);
  logger.error(err.stack);

  if (config.mailcrashto) {
    mailer.send('crash', {
      to:       config.mailcrashto,
      subject:  `[${config.appname}] Crash: ${err}`,
      body:     formatMessage(req, err)
    });
  }
  
  if (!res._headerSent) {
    // show error
    if (config.env === 'production') {
      return res.status(500).render('errors/500');
    }

    //
    const stacktrace = `
      <h1>${req.method}: ${req.originalUrl}</h1>
      <pre>${err.stack}</pre>
    `;
    res.status(500).send(stacktrace);
  }

{'fraction_non_alphanumeric': 0.23647058823529413, 'fraction_numerical': 0.01411764705882353, 'mean_word_length': 2.9684947491248543, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2238843', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1252, 'n_words': 197}