<commit_before>'use strict';

if (!Error.captureStackTrace) throw new Error('Error.captureStackTrace does not exist!');

var util = require('./util');
var BE = require('./base_error');
var v = require('./validator').v;
var check = require('./validator').check;


var BaseError = BE.BaseError;

var Errors = {
    template: null,
};
exports = module.exports = Errors;

exports.setErrorStackSeparator = function(separator) {
    BE.ERROR_STACK_SEPARATOR = separator;
};
exports.setErrorStackSeparator('\n==== Pre-Error-Stack ====\n');

exports.setMessageConnector = function(connector) {
    BE.MESSAGE_CONNECTOR = connector;
};
exports.setMessageConnector(' && ');

/**
 * define a subclass of BaseError
 */
exports.defineError = function(definition, name) {
    if (!Errors.template) throw new Error('The error template should be defined firstly!');

    var E = function() {
        BaseError.apply(this, arguments);
    };
    util.inherits(E, BaseError);

    util.each(Errors.template, function(opts, key) {
        var value = definition[key];
        if (value !== undefined) {
            E.prototype[key] = value;
        } else {
            if (opts.required === true) {
                throw new Error('The key "' + key + '" is required, while definition = ' + JSON.stringify(definition));
            } else {
                value = opts.default;
            }
        }
    });

    E.prototype.name = name;

    Errors[name] = E;

    return E;
};

/**
 * To determine whether it is your custom error.
 *
 * if the error is an instance of the native Error, return `false`.
 *
 * @param  {*}  error
 * @return {Boolean}
 * @method isCustomError(err)
 */
exports.isCustomError = function(err) {
    return err instanceof BaseError;
};
// alias for isCustomError
exports.isError = Errors.isCustomError;

exports.setTemplate = function(template) {
    if (util.isObject(template) === false) {
        throw new Error('template should be an Object!');
    }

    var t = {};
    util.each(template, function(params, key) {
        if (util.isString(params)) {
            t[key] = {
                message: params,
                required: true,
            };
        } else if (util.isObject(params)) {
            t[key] = check(params, v.definition());
        } else {
            throw new Error('The value of template item should be an object or a string. Actual key=' + key + ' value=' + JSON.stringify(params));
        }
    });
    Errors.template = t;
};

/**
 * Assigns own enumerable properties of meta to the err.meta.
 *
 * If the err.meta is undefined before assigning, it will be assigned a new object,
 * and then the own enumerable properties of second parameter will be assigned to err.meta.
 * If the err.meta is not undefined, it should be a plain object.
 *
 * the properties of meta will overwrite the properties of err.meta, if they have same name.
 *
 * @side_effect  err, err.meta
 * @param  {Error}  err  the instance of Error class or Error subclass
 * @param  {Object} meta
 * @method addMeta
 */
exports.addMeta = function(err, meta) {
    if (!err.meta) err.meta = {};
    util.extend(err.meta, meta);
};

/**
 * initialize module
 *
 * @param  {Object} params
 * @param  {Object} params.template  a template for all error sub-classes
 * @param  {Array<Object>} params.definitions the definitions of error sub-classes
 * @param  {Boolean} [params.lazy=false]  if true, params.template and params.definitions could be optional.
 *                                        Otherwise, template and definitions are required.
 * @return {Null}
 * @method init(params)
 */
exports.init = function(params) {<commit_msg>update more specific code comments<commit_after>'use strict';

if (!Error.captureStackTrace) throw new Error('Error.captureStackTrace does not exist!');

var util = require('./util');
var BE = require('./base_error');
var v = require('./validator').v;
var check = require('./validator').check;


var BaseError = BE.BaseError;

var Errors = {
    template: null,
};
exports = module.exports = Errors;

/**
 * set the separator for multi error stacks.
 *
 * Default to "\n==== Pre-Error-Stack ====\n"
 *
 * @param  {String} separator
 * @method setErrorStackSeparator(separator)
 */
exports.setErrorStackSeparator = function(separator) {
    BE.ERROR_STACK_SEPARATOR = separator;
};
exports.setErrorStackSeparator('\n==== Pre-Error-Stack ====\n');

/**
 * set the connector for multi error messages. Default to " && "
 *
 * @param  {String} connector
 * @return {Undefined}
 * @method setMessageConnector(connector)
 */
exports.setMessageConnector = function(connector) {
    BE.MESSAGE_CONNECTOR = connector;
};
exports.setMessageConnector(' && ');

/**
 * define a subclass of BaseError
 *
 * @param  {Object} definition
 * @param  {String} name  the name of Error Class
 * @return {Function}  Error Class
 * @method defineError(definition, name)
 */
exports.defineError = function(definition, name) {
    if (!Errors.template) throw new Error('The error template should be defined firstly!');

    var E = function() {
        BaseError.apply(this, arguments);
    };
    util.inherits(E, BaseError);

    util.each(Errors.template, function(opts, key) {
        var value = definition[key];
        if (value !== undefined) {
            E.prototype[key] = value;
        } else {
            if (opts.required === true) {
                throw new Error('The key "' + key + '" is required, while definition = ' + JSON.stringify(definition));
            } else {
                value = opts.default;
            }
        }
    });

    E.prototype.name = name;

    Errors[name] = E;

    return E;
};

/**
 * To determine whether it is your custom error.
 *
 * if the error is an instance of the native Error, return `false`.
 *
 * @param  {*}  error
 * @return {Boolean}
 * @method isCustomError(err)
 */
exports.isCustomError = function(err) {
    return err instanceof BaseError;
};
/**
 * alias for isCustomError
 */
exports.isError = Errors.isCustomError;

/**
 * @param  {Object} template
 * @return {Undefined}
 * @method setTemplate(template)
 */
exports.setTemplate = function(template) {
    if (util.isObject(template) === false) {
        throw new Error('template should be an Object!');
    }

    var t = {};
    util.each(template, function(params, key) {
        if (util.isString(params)) {
            t[key] = {
                message: params,
                required: true,
            };
        } else if (util.isObject(params)) {
            t[key] = check(params, v.definition());
        } else {
            throw new Error('The value of template item should be an object or a string. Actual key=' + key + ' value=' + JSON.stringify(params));
        }
    });
    Errors.template = t;
};

/**
 * Assigns own enumerable properties of meta to the err.meta.
 *
 * If the err.meta is undefined before assigning, it will be assigned a new object,
 * and then the own enumerable properties of second parameter will be assigned to err.meta.
 * If the err.meta is not undefined, it should be a plain object.
 *
 * the properties of meta will overwrite the properties of err.meta, if they have same name.
 *
 * @side_effect  err, err.meta
 * @param  {Error}  err  the instance of Error class or Error subclass
 * @param  {Object} meta
 * @return {Undefined}
 * @method addMeta
 */
exports.addMeta = function(err, meta) {
    if (!err.meta) err.meta = {};
    util.extend(err.meta, meta);
};

/**
 * initialize module
 *
 * @param  {Object} params
 * @param  {Object} params.template  a template for all error sub-classes
 * @param  {Array<Object>} params.definitions the definitions of error sub-classes
 * @param  {Boolean} [params.lazy=false]  if true, params.template and params.definitions could be optional.
 *                                        Otherwise, template and definitions are required.
 * @return {Undefined}
 * @method init(params)
 */
exports.init = function(params) {
{'fraction_non_alphanumeric': 0.12959478108142014, 'fraction_numerical': 0.0, 'mean_word_length': 2.851207729468599, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1008717', 'n_tokens_mistral': 2293, 'n_tokens_neox': 2263, 'n_words': 722}