@tgsnake/core
Version:
Pure Telegram MTProto library for nodejs
121 lines (120 loc) • 4.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnknownError = exports.RPCError = void 0;
const Logger_js_1 = require("../Logger.js");
const All_js_1 = require("./exceptions/All.js");
const platform_node_js_1 = require("../platform.node.js");
const index_js_1 = require("./index.js");
function getModule(name) {
const [namespace, mod] = name.split('.');
if (index_js_1.Exceptions[namespace] &&
index_js_1.Exceptions[namespace][mod]) {
return index_js_1.Exceptions[namespace][mod];
}
return UnknownError;
}
class RPCError extends Error {
id;
code;
message;
name;
value;
_isSigned;
_isUnknown;
_rpcName;
constructor(value, rpcName, isUnknown, isSigned) {
super();
Logger_js_1.Logger.debug(`[8] Creating new instance RPCError(${rpcName ?? this.name})`);
this._isSigned = isSigned;
this._isUnknown = isUnknown;
this._rpcName = rpcName;
if (!Number.isNaN(value)) {
this.value = Number(value);
}
else {
this.value = value;
}
if (isUnknown) {
Logger_js_1.Logger.debug(`[9] UnknownError : ${this.name}`);
}
}
_format() {
this.message = `Telegram Says: [${this._isSigned ? '-' : ''}${this.code} ${this.id || this.name}] - ${(this.message || '').replace(/\{value\}/g, String(this.value))} ${this._rpcName ? `(caused by ${this._rpcName})` : ''}`;
}
static async raise(rpcError, rpcType) {
let code = rpcError.errorCode;
const message = rpcError.errorMessage;
const isSigned = code < 0;
const name = rpcType.className;
if (isSigned)
code = -code;
if (!(code in All_js_1.Exceptions)) {
throw new UnknownError(`[${code} ${message}]`, name, true, isSigned);
}
let id = message.replace(/\_\d+/gm, '_X');
const regexMatch = message.match(/\_(\d+)/gm);
let value = '';
if (regexMatch) {
value = regexMatch[0].replace(/\_/g, '');
}
if (!(id in All_js_1.Exceptions[code])) {
id = id.split('_').splice(-1, 1, '*').join('_');
if (!(id in All_js_1.Exceptions[code])) {
const modules = getModule(All_js_1.Exceptions[code]['_']);
const _module = new modules(value, name, true, isSigned);
_module.message = `[${code} ${message}]`;
_module.id = message.replace(/\_\d+/gm, '_X');
_module._format();
throw _module;
}
}
const modules = await getModule(All_js_1.Exceptions[code][id]);
const _module = new modules(value, name, false, isSigned);
_module._format();
throw _module;
}
[Symbol.for('nodejs.util.inspect.custom')]() {
const toPrint = {
_: this.constructor.name,
};
for (const key in this) {
if (Object.prototype.hasOwnProperty.call(this, key)) {
const value = this[key];
if (!key.startsWith('_') && value !== undefined && value !== null) {
toPrint[key] = value;
}
}
}
Object.setPrototypeOf(toPrint, {
stack: this.stack,
});
return toPrint;
}
[Symbol.for('Deno.customInspect')]() {
return String((0, platform_node_js_1.inspect)(this[Symbol.for('nodejs.util.inspect.custom')](), { colors: true }));
}
toJSON() {
const toPrint = {
_: this.constructor.name,
stack: this.stack,
};
for (const key in this) {
if (Object.prototype.hasOwnProperty.call(this, key)) {
const value = this[key];
if (!key.startsWith('_')) {
toPrint[key] = typeof value === 'bigint' ? String(value) : value;
}
}
}
return toPrint;
}
toString() {
return `[constructor of ${this.constructor.name}] ${JSON.stringify(this, null, 2)}`;
}
}
exports.RPCError = RPCError;
class UnknownError extends RPCError {
code = 520;
name = 'Unknown Error';
}
exports.UnknownError = UnknownError;