basant307/AI_Governance_Project
045
1'use strict';2 3const codes = {};4 5function createErrorType(code, message, Base) {6 if (!Base) {7 Base = Error8 }9 10 function getMessage (arg1, arg2, arg3) {11 if (typeof message === 'string') {12 return message13 } else {14 return message(arg1, arg2, arg3)15 }16 }17 18 class NodeError extends Base {19 constructor (arg1, arg2, arg3) {20 super(getMessage(arg1, arg2, arg3));21 }22 }23 24 NodeError.prototype.name = Base.name;25 NodeError.prototype.code = code;26 27 codes[code] = NodeError;28}29 30// https://github.com/nodejs/node/blob/v10.8.0/lib/internal/errors.js31function oneOf(expected, thing) {32 if (Array.isArray(expected)) {33 const len = expected.length;34 expected = expected.map((i) => String(i));35 if (len > 2) {36 return `one of ${thing} ${expected.slice(0, len - 1).join(', ')}, or ` +37 expected[len - 1];38 } else if (len === 2) {39 return `one of ${thing} ${expected[0]} or ${expected[1]}`;40 } else {41 return `of ${thing} ${expected[0]}`;42 }43 } else {44 return `of ${thing} ${String(expected)}`;45 }46}47 48// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith49function startsWith(str, search, pos) {50 return str.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;51}52 53// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith54function endsWith(str, search, this_len) {55 if (this_len === undefined || this_len > str.length) {56 this_len = str.length;57 }58 return str.substring(this_len - search.length, this_len) === search;59}60 61// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes62function includes(str, search, start) {63 if (typeof start !== 'number') {64 start = 0;65 }66 67 if (start + search.length > str.length) {68 return false;69 } else {70 return str.indexOf(search, start) !== -1;71 }72}73 74createErrorType('ERR_INVALID_OPT_VALUE', function (name, value) {75 return 'The value "' + value + '" is invalid for option "' + name + '"'76}, TypeError);77createErrorType('ERR_INVALID_ARG_TYPE', function (name, expected, actual) {78 // determiner: 'must be' or 'must not be'79 let determiner;80 if (typeof expected === 'string' && startsWith(expected, 'not ')) {81 determiner = 'must not be';82 expected = expected.replace(/^not /, '');83 } else {84 determiner = 'must be';85 }86 87 let msg;88 if (endsWith(name, ' argument')) {89 // For cases like 'first argument'90 msg = `The ${name} ${determiner} ${oneOf(expected, 'type')}`;91 } else {92 const type = includes(name, '.') ? 'property' : 'argument';93 msg = `The "${name}" ${type} ${determiner} ${oneOf(expected, 'type')}`;94 }95 96 msg += `. Received type ${typeof actual}`;97 return msg;98}, TypeError);99createErrorType('ERR_STREAM_PUSH_AFTER_EOF', 'stream.push() after EOF');100createErrorType('ERR_METHOD_NOT_IMPLEMENTED', function (name) {101 return 'The ' + name + ' method is not implemented'102});103createErrorType('ERR_STREAM_PREMATURE_CLOSE', 'Premature close');104createErrorType('ERR_STREAM_DESTROYED', function (name) {105 return 'Cannot call ' + name + ' after a stream was destroyed';106});107createErrorType('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times');108createErrorType('ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable');109createErrorType('ERR_STREAM_WRITE_AFTER_END', 'write after end');110createErrorType('ERR_STREAM_NULL_VALUES', 'May not write null values to stream', TypeError);111createErrorType('ERR_UNKNOWN_ENCODING', function (arg) {112 return 'Unknown encoding: ' + arg113}, TypeError);114createErrorType('ERR_STREAM_UNSHIFT_AFTER_END_EVENT', 'stream.unshift() after end event');115 116module.exports.codes = codes;117 