fix: fixed new error codegen

This commit is contained in:
alina 🌸 2023-09-07 22:26:52 +03:00
parent aa863c6a2f
commit 55edbde3e7
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI

View file

@ -2,18 +2,16 @@ import { TlErrors } from '../types'
import { snakeToCamel } from './utils' import { snakeToCamel } from './utils'
const TEMPLATE_JS = ` const TEMPLATE_JS = `
const _descriptionsMap = { const _descriptionsMap = JSON.parse('{descriptionsMap}')
{descriptionsMap}
}
class RpcError extends Error { class RpcError extends Error {
constructor(code, name) { constructor(code, text) {
super(_descriptionsMap[name] || 'Unknown RPC error: [' + code + ':' + name + ']'); super(_descriptionsMap[text] || 'Unknown RPC error: [' + code + ':' + text + ']');
this.code = code; this.code = code;
this.name = name; this.text = text;
} }
static is(err, name) { return err.constructor === RpcError && (!name || err.name === name); } static is(err, text) { return err.constructor === RpcError && (!text || err.text === text); }
is(name) { return this.name === name; } is(text) { return this.text === text; }
} }
RpcError.fromTl = function (obj) { RpcError.fromTl = function (obj) {
const err = new RpcError(obj.errorCode, obj.errorMessage); const err = new RpcError(obj.errorCode, obj.errorMessage);
@ -109,7 +107,7 @@ export function generateCodeForErrors(
errors: TlErrors, errors: TlErrors,
exports = 'exports.', exports = 'exports.',
): [string, string] { ): [string, string] {
let descriptionsMap = '' const descriptionsMap: Record<string, string> = {}
let texts = '' let texts = ''
let argMap = '' let argMap = ''
let matchers = '' let matchers = ''
@ -125,9 +123,7 @@ export function generateCodeForErrors(
const [name, placeholders] = parseCode(error.name, error._paramNames) const [name, placeholders] = parseCode(error.name, error._paramNames)
if (error.description) { if (error.description) {
descriptionsMap += ` '${name}': ${JSON.stringify( descriptionsMap[name] = error.description
error.description,
)},\n`
} }
texts += ` | '${name}'\n` texts += ` | '${name}'\n`
@ -142,12 +138,10 @@ export function generateCodeForErrors(
' },\n' ' },\n'
const regex = name.replace('%d', '(\\d+)') const regex = name.replace('%d', '(\\d+)')
const setters = placeholders.map( const setters = placeholders
(it, i) => `err.${it} = parseInt(match[${i + 1}])`, .map((it, i) => `err.${it} = parseInt(match[${i + 1}])`)
) .join('; ')
const settersStr = matchers += ` if ((match=obj.errorMessage.match(/^${regex}$/))!=null){ err.text = '${name}'; ${setters} }\n`
setters.length > 1 ? `{ ${setters.join('; ')} }` : setters[0]
matchers += ` if ((match=obj.errorMessage.match(/^${regex}$/))!=null)${settersStr}\n`
} }
} }
@ -156,7 +150,10 @@ export function generateCodeForErrors(
template(TEMPLATE_JS, { template(TEMPLATE_JS, {
exports, exports,
statics: staticsJs, statics: staticsJs,
descriptionsMap, descriptionsMap: JSON.stringify(descriptionsMap).replace(
/'/g,
"\\'",
),
matchers, matchers,
}), }),
] ]