<commit_before>const path = require('path');
const errors = require('./index');

const defaults = {
  public: path.resolve(__dirname, 'public'),
  logger: console
};
const defaultHtmlError = path.resolve(defaults.public, 'default.html');

module.exports = function (options = {}) {
  options = Object.assign({}, defaults, options);

  if (typeof options.html === 'undefined') {
    options.html = {
      401: path.resolve(options.public, '401.html'),
      404: path.resolve(options.public, '404.html'),
      default: defaultHtmlError
    };
  }

  if (typeof options.json === 'undefined') {
    options.json = {};
  }

  return function (error, req, res, next) {
    // Log the error if it didn't come from a service method call
    if (options.logger && typeof options.logger.error === 'function' && !res.hook) {
      options.logger.error(error);
    }

    if (error.type !== 'FeathersError') {
      let oldError = error;
      error = new errors.GeneralError(oldError.message, {
        errors: oldError.errors
      });

      if (oldError.stack) {
        error.stack = oldError.stack;
      }
    }

    error.code = !isNaN(parseInt(error.code, 10)) ? parseInt(error.code, 10) : 500;
    const formatter = {};

    // If the developer passed a custom function for ALL html errors<commit_msg>fix: Do not log as errors below a 500 response (#1256)<commit_after>const path = require('path');
const errors = require('./index');

const defaults = {
  public: path.resolve(__dirname, 'public'),
  logger: console
};
const defaultHtmlError = path.resolve(defaults.public, 'default.html');

module.exports = function (options = {}) {
  options = Object.assign({}, defaults, options);

  if (typeof options.html === 'undefined') {
    options.html = {
      401: path.resolve(options.public, '401.html'),
      404: path.resolve(options.public, '404.html'),
      default: defaultHtmlError
    };
  }

  if (typeof options.json === 'undefined') {
    options.json = {};
  }

  return function (error, req, res, next) {
    // Set the error code for HTTP processing semantics
    error.code = !isNaN(parseInt(error.code, 10)) ? parseInt(error.code, 10) : 500;

    // Log the error if it didn't come from a service method call
    if (options.logger && typeof options.logger.error === 'function' && !res.hook) {
      if (error.code >= 500) {
        options.logger.error(error);
      } else {
        options.logger.info(error);
      }
    }

    if (error.type !== 'FeathersError') {
      let oldError = error;
      error = new errors.GeneralError(oldError.message, {
        errors: oldError.errors
      });

      if (oldError.stack) {
        error.stack = oldError.stack;
      }
    }

    const formatter = {};

    // If the developer passed a custom function for ALL html errors
{'fraction_non_alphanumeric': 0.14602720114531137, 'fraction_numerical': 0.01717967072297781, 'mean_word_length': 2.925561797752809, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '507575', 'n_tokens_mistral': 869, 'n_tokens_neox': 817, 'n_words': 224}