UNPKG

@kubiklabs/wasmkit

Version:

Wasmkit is a development environment to compile, deploy, test, run cosmwasm contracts on different networks.

114 lines (113 loc) 4.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.assertWasmKitInvariant = exports.applyErrorMessageTemplate = exports.WasmKitPluginError = exports.WasmkitError = void 0; const caller_package_1 = require("../util/caller-package"); const strings_1 = require("../util/strings"); const errors_list_1 = require("./errors-list"); class WasmkitError extends Error { static isWasmkitError(other) { return (other !== undefined && other !== null && other._isWasmkitError === true); } static isWasmkitErrorType(other, // eslint-disable-line descriptor) { return (WasmkitError.isWasmkitError(other) && other.errorDescriptor.number === descriptor.number); } constructor(errorDescriptor, messageArguments = {}, // eslint-disable-line @typescript-eslint/no-explicit-any parentError) { const prefix = `${(0, errors_list_1.getErrorCode)(errorDescriptor)}: `; const formattedMessage = applyErrorMessageTemplate(errorDescriptor.message, messageArguments); super(prefix + formattedMessage); this.errorDescriptor = errorDescriptor; this.number = errorDescriptor.number; this.messageArguments = messageArguments; if (parentError instanceof Error) { this.parent = parentError; } this._isWasmkitError = true; Object.setPrototypeOf(this, WasmkitError.prototype); } } exports.WasmkitError = WasmkitError; /** * This class is used to throw errors from WasmKit plugins made by third parties. */ class WasmKitPluginError extends Error { static isWasmKitPluginError(other) { return (other !== undefined && other !== null && other._isWasmKitPluginError === true); } constructor(pluginNameOrMessage, messageOrParent, parent) { if (typeof messageOrParent === 'string') { super(messageOrParent); this.pluginName = pluginNameOrMessage; this.parent = parent; } else { super(pluginNameOrMessage); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.pluginName = (0, caller_package_1.getClosestCallerPackage)(); this.parent = messageOrParent; } this._isWasmKitPluginError = true; Object.setPrototypeOf(this, WasmKitPluginError.prototype); } } exports.WasmKitPluginError = WasmKitPluginError; /** * This function applies error messages templates like this: * * - Template is a string which contains a variable tags. A variable tag is a * a variable name surrounded by %. Eg: %plugin1% * - A variable name is a string of alphanumeric ascii characters. * - Every variable tag is replaced by its value. * - %% is replaced by %. * - Values can't contain variable tags. * - If a variable is not present in the template, but present in the values * object, an error is thrown. * * @param template The template string. * @param values A map of variable names to their values. */ function applyErrorMessageTemplate(template, // eslint-disable-next-line values) { return _applyErrorMessageTemplate(template, values); } exports.applyErrorMessageTemplate = applyErrorMessageTemplate; /* eslint-disable sonarjs/cognitive-complexity */ function _applyErrorMessageTemplate(template, // eslint-disable-next-line values) { if (template.includes('%%')) { return template .split('%%') .map((part) => _applyErrorMessageTemplate(part, values)) .join('%'); } for (const variableName of Object.keys(values)) { let value; if (values[variableName] === undefined) { value = 'undefined'; } else if (values[variableName] === null) { value = 'null'; } else { value = values[variableName].toString(); } if (value === undefined) { value = 'undefined'; } const variableTag = `%${variableName}%`; template = (0, strings_1.replaceAll)(template, variableTag, value); } return template; } function assertWasmKitInvariant(invariant, message) { if (!invariant) { throw new WasmkitError(errors_list_1.ERRORS.GENERAL.ASSERTION_ERROR, { message }); } } exports.assertWasmKitInvariant = assertWasmKitInvariant;