-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy patherrors.js
94 lines (82 loc) · 3.22 KB
/
errors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const fs = require('fs');
const VoiceErrors = require('@twilio/voice-errors');
const { USED_ERRORS } = require('../lib/twilio/constants');
let output = `/* tslint:disable max-classes-per-file max-line-length */
/**
* @packageDocumentation
* @module Voice
* @publicapi
* @internal
*/
/**
* This is a generated file. Any modifications here will be overwritten. See scripts/errors.js.
*/
import TwilioError from './twilioError';
export { TwilioError };
// TypeScript doesn't allow extending Error so we need to run constructor logic on every one of these
// individually. Ideally this logic would be run in a constructor on a TwilioError class but
// due to this limitation TwilioError is an interface.
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes
function construct(context: TwilioError, messageOrError?: string | Error, originalError?: Error) {
if (typeof messageOrError === 'string') {
context.message = messageOrError;
if (originalError instanceof Error) {
context.originalError = originalError;
}
} else if (messageOrError instanceof Error) {
context.originalError = messageOrError;
}
}
\n`;
const escapeQuotes = str => str.replace("'", "\\'");
const generateStringArray = arr => arr ? `[
${arr.map(value => `'${escapeQuotes(value)}'`).join(',\n ')},
]` : '[]';
const generateDefinition = (code, subclassName, errorName, error) => `\
export class ${errorName} extends Error implements TwilioError {
causes: string[] = ${generateStringArray(error.causes)};
code: number = ${code};
description: string = '${escapeQuotes(error.description)}';
explanation: string = '${escapeQuotes(error.explanation)}';
solutions: string[] = ${generateStringArray(error.solutions)};
constructor();
constructor(message: string);
constructor(originalError: Error);
constructor(message: string, originalError?: Error);
constructor(messageOrError?: string | Error, originalError?: Error) {
super('');
Object.setPrototypeOf(this, ${subclassName}Errors.${errorName}.prototype);
construct(this, messageOrError, originalError);
}
}`;
const generateNamespace = (name, contents) => `export namespace ${name}Errors {
${contents}
}\n\n`;
let mapEntries = [];
for (const topClass of VoiceErrors) {
for (const subclass of topClass.subclasses) {
const subclassName = subclass.class.replace(' ', '');
const definitions = [];
for (const error of subclass.errors) {
const code = (topClass.code * 1000) + ((subclass.code || 0) * 100) + error.code;
const errorName = error.name.replace(' ', '');
const fullName = `${subclassName}Errors.${errorName}`;
if (USED_ERRORS.includes(fullName)) {
const definition = generateDefinition(code, subclassName, errorName, error);
definitions.push(definition);
mapEntries.push(`[ ${code}, ${fullName} ]`);
}
}
if (mapEntries.length && definitions.length) {
output += generateNamespace(subclassName, definitions.join('\n\n'));
}
}
}
output += `/**
* @private
*/
export const errorsByCode: ReadonlyMap<number, any> = new Map([
${mapEntries.join(',\n ')},
]);
Object.freeze(errorsByCode);\n`;
fs.writeFileSync('./lib/twilio/errors/generated.ts', output, 'utf8');