<commit_before>var util = require('util');

function _has(obj, propName) {
    return Object.prototype.hasOwnProperty.call(obj, propName);
}

function copyPlainObject(src) {
    var o = {};
    Object.getOwnPropertyNames(src).forEach(function(key) {
        o[key] = src[key];
    });
    return o;
}

/**
 * @constructor
 * @param {Number} [code=Terror.CODES.UNKNOWN_ERROR]
 * @param {String} [message]
 * @returns {Terror}
 * @example
 *      ```
 *      var MyError = Terror.create('MyError', {
 *          MY_FAULT: "It\'s my own fault, because %reason%"
 *      });
 *
 *      try {
 *          // dangerous code here
 *      } catch(err) {
 *          MyError.createError(MyError.CODES.MY_FAULT, err)
 *              .bind({ reason : 'something strange happens!' })
 *              .log();
 *      }
 *
 *      // try to use Terror#ensureError if catched error can be Terror successor or not
 *
 *      try {
 *          // dangerous code here
 *      } catch(err) {
 *          MyError.ensureError(err, MyError.CODES.MY_FAULT)
 *              .bind({ reason : 'i was sad when write code above' })
 *              .log();
 *      }
 *
 *      // or simply log and throw Terror
 *
 *      throw MyError.createError(MyError.CODES.MY_FAULT, { reason : 'i was too stupid!' })
 *          .log('warn');
 *      ```
 */
function Terror(code, message) {
    Error.apply(this);

    this.message = '$$MESSAGE$$';
    Error.captureStackTrace(this, this.constructor);
    this._stackTemplate = this.stack;

    this.code = typeof code === 'undefined' || code === null ? Terror.CODES.UNKNOWN_ERROR : code;
    this.setMessage(message || this.constructor.MESSAGES[this.code]);
    this.data = {};
}
util.inherits(Terror, Error);

Terror.prototype.name = 'Terror';

/**
 * @param {String} name Error class name
 * @param {Object} [codes] hash { ERROR_CODE: 'error message', … }
 */
Terror.create = function(name, codes) {
    if (typeof name !== 'string') {
        codes = name;
        name = null;
    }

    var Inheritor = function (code, message) {
        Terror.call(this, code, message);
    };
    util.inherits(Inheritor, Terror);

    if (name) {
        Inheritor.prototype.name = name;
    }

    // link contructors methods
    Inheritor.create = Terror.create;
    Inheritor.extendCodes = Terror.extendCodes;
    Inheritor.setLogger = Terror.setLogger;
    Inheritor.createError = Terror.createError;
    Inheritor.ensureError = Terror.ensureError;
    // copy static fields
    Inheritor.CODES = copyPlainObject(this.CODES);
    Inheritor.MESSAGES = copyPlainObject(this.MESSAGES);
    Inheritor.DEFAULT_ERROR_LEVEL = this.DEFAULT_ERROR_LEVEL;

    if (typeof codes === 'object' && codes !== null) {
        Inheritor.extendCodes(codes);
    }

    return Inheritor;
};

/**
 * Extends existing CODE and MESSAGE hashes.
 * @param {Object} codes { CODE_NAME: 'error message', … }
 * @returns {Function} constructor
 * @throws TerrorError if collisions detected.
 */
Terror.extendCodes = function(codes) {
    var ctor = this;

    Object.keys(codes).forEach(function(code) {
        if (_has(ctor.CODES, code)) {
            throw new Error([
                'Terror codes collision detected in the ',
                ctor.prototype.name,
                '.extendCodes call for code "',
                code,
                '"'
            ].join(''));
        }

        ctor.CODES[code] = code;
        ctor.MESSAGES[code] = codes[code];<commit_msg>replace strings array join with direct strings concat<commit_after>var util = require('util');

function _has(obj, propName) {
    return Object.prototype.hasOwnProperty.call(obj, propName);
}

function copyPlainObject(src) {
    var o = {};
    Object.getOwnPropertyNames(src).forEach(function(key) {
        o[key] = src[key];
    });
    return o;
}

/**
 * @constructor
 * @param {Number} [code=Terror.CODES.UNKNOWN_ERROR]
 * @param {String} [message]
 * @returns {Terror}
 * @example
 *      ```
 *      var MyError = Terror.create('MyError', {
 *          MY_FAULT: "It\'s my own fault, because %reason%"
 *      });
 *
 *      try {
 *          // dangerous code here
 *      } catch(err) {
 *          MyError.createError(MyError.CODES.MY_FAULT, err)
 *              .bind({ reason : 'something strange happens!' })
 *              .log();
 *      }
 *
 *      // try to use Terror#ensureError if catched error can be Terror successor or not
 *
 *      try {
 *          // dangerous code here
 *      } catch(err) {
 *          MyError.ensureError(err, MyError.CODES.MY_FAULT)
 *              .bind({ reason : 'i was sad when write code above' })
 *              .log();
 *      }
 *
 *      // or simply log and throw Terror
 *
 *      throw MyError.createError(MyError.CODES.MY_FAULT, { reason : 'i was too stupid!' })
 *          .log('warn');
 *      ```
 */
function Terror(code, message) {
    Error.apply(this);

    this.message = '$$MESSAGE$$';
    Error.captureStackTrace(this, this.constructor);
    this._stackTemplate = this.stack;

    this.code = typeof code === 'undefined' || code === null ? Terror.CODES.UNKNOWN_ERROR : code;
    this.setMessage(message || this.constructor.MESSAGES[this.code]);
    this.data = {};
}
util.inherits(Terror, Error);

Terror.prototype.name = 'Terror';

/**
 * @param {String} name Error class name
 * @param {Object} [codes] hash { ERROR_CODE: 'error message', … }
 */
Terror.create = function(name, codes) {
    if (typeof name !== 'string') {
        codes = name;
        name = null;
    }

    var Inheritor = function (code, message) {
        Terror.call(this, code, message);
    };
    util.inherits(Inheritor, Terror);

    if (name) {
        Inheritor.prototype.name = name;
    }

    // link contructors methods
    Inheritor.create = Terror.create;
    Inheritor.extendCodes = Terror.extendCodes;
    Inheritor.setLogger = Terror.setLogger;
    Inheritor.createError = Terror.createError;
    Inheritor.ensureError = Terror.ensureError;
    // copy static fields
    Inheritor.CODES = copyPlainObject(this.CODES);
    Inheritor.MESSAGES = copyPlainObject(this.MESSAGES);
    Inheritor.DEFAULT_ERROR_LEVEL = this.DEFAULT_ERROR_LEVEL;

    if (typeof codes === 'object' && codes !== null) {
        Inheritor.extendCodes(codes);
    }

    return Inheritor;
};

/**
 * Extends existing CODE and MESSAGE hashes.
 * @param {Object} codes { CODE_NAME: 'error message', … }
 * @returns {Function} constructor
 * @throws TerrorError if collisions detected.
 */
Terror.extendCodes = function(codes) {
    var ctor = this;

    Object.keys(codes).forEach(function(code) {
        if (_has(ctor.CODES, code)) {
            throw new Error('Terror codes collision detected in the ' +
                ctor.prototype.name + '.extendCodes call for code "' + code + '"');
        }

        ctor.CODES[code] = code;
        ctor.MESSAGES[code] = codes[code];
{'fraction_non_alphanumeric': 0.14090446415588193, 'fraction_numerical': 0.0, 'mean_word_length': 2.5002544529262085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '292322', 'n_tokens_mistral': 2167, 'n_tokens_neox': 2128, 'n_words': 484}