123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- const generateErrors = (errors, f) => {
- // Exact/regex match to create {CODE: ErrorClassName}
- const exactMatch = [];
- const regexMatch = [];
- // Find out what subclasses to import and which to create
- const importBase = new Set();
- const createBase = {};
- for (const error of errors) {
- if (error.subclassExists) {
- importBase.add(error.subclass);
- } else {
- createBase[error.subclass] = error.intCode;
- }
- if (error.hasCaptures) {
- regexMatch.push(error);
- } else {
- exactMatch.push(error);
- }
- }
- // Imports and new subclass creation
- f.write(
- `const { RPCError, ${[...importBase.values()].join(
- ', '
- )} } = require('./rpcbaseerrors');`
- );
- f.write("\nconst format = require('string-format');");
- for (const [cls, intCode] of Object.entries(createBase)) {
- f.write(
- `\n\nclass ${cls} extends RPCError {\n constructor() {\n this.code = ${intCode};\n }\n}`
- );
- }
- // Error classes generation
- for (const error of errors) {
- f.write(
- `\n\nclass ${error.name} extends ${error.subclass} {\n constructor(args) {\n `
- );
- if (error.hasCaptures) {
- f.write(
- `const ${error.captureName} = Number(args.capture || 0);\n `
- );
- }
- const capture = error.description.replace(/'/g, "\\'");
- if (error.hasCaptures) {
- f.write(`super(format('${capture}', {${error.captureName}})`);
- } else {
- f.write(`super('${capture}'`);
- }
- f.write(' + RPCError._fmtRequest(args.request));\n');
- if (error.hasCaptures) {
- f.write(
- ` this.${error.captureName} = ${error.captureName};\n`
- );
- }
- f.write(' }\n}\n');
- }
- f.write('\n\nconst rpcErrorsObject = {\n');
- for (const error of exactMatch) {
- f.write(` ${error.pattern}: ${error.name},\n`);
- }
- f.write('};\n\nconst rpcErrorRe = [\n');
- for (const error of regexMatch) {
- f.write(` [/${error.pattern}/, ${error.name}],\n`);
- }
- f.write('];');
- f.write("module.exports = {");
- for (const error of regexMatch) {
- f.write(` ${error.name},\n`);
- }
- for (const error of exactMatch) {
- f.write(` ${error.name},\n`);
- }
- f.write(" rpcErrorsObject,\n");
- f.write(" rpcErrorRe,\n");
- f.write("}");
- };
- module.exports = {
- generateErrors,
- };
|