@codama/errors
Version:
Error management for Codama
164 lines (157 loc) • 10.2 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/cli/index.ts
var cli_exports = {};
__export(cli_exports, {
run: () => run
});
module.exports = __toCommonJS(cli_exports);
var import_chalk = __toESM(require("chalk"));
var import_commander = require("commander");
// package.json
var version = "1.3.1";
// src/context.ts
function decodeEncodedContext(encodedContext) {
const decodedUrlString = true ? Buffer.from(encodedContext, "base64").toString("utf8") : atob(encodedContext);
return Object.fromEntries(new URLSearchParams(decodedUrlString).entries());
}
// src/codes.ts
var CODAMA_ERROR__UNRECOGNIZED_NODE_KIND = 1;
var CODAMA_ERROR__UNEXPECTED_NODE_KIND = 2;
var CODAMA_ERROR__UNEXPECTED_NESTED_NODE_KIND = 3;
var CODAMA_ERROR__LINKED_NODE_NOT_FOUND = 4;
var CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE = 5;
var CODAMA_ERROR__VERSION_MISMATCH = 6;
var CODAMA_ERROR__UNRECOGNIZED_NUMBER_FORMAT = 7;
var CODAMA_ERROR__UNRECOGNIZED_BYTES_ENCODING = 8;
var CODAMA_ERROR__ENUM_VARIANT_NOT_FOUND = 9;
var CODAMA_ERROR__DISCRIMINATOR_FIELD_NOT_FOUND = 10;
var CODAMA_ERROR__DISCRIMINATOR_FIELD_HAS_NO_DEFAULT_VALUE = 11;
var CODAMA_ERROR__VISITORS__CANNOT_ADD_DUPLICATED_PDA_NAMES = 12e5;
var CODAMA_ERROR__VISITORS__INVALID_PDA_SEED_VALUES = 1200001;
var CODAMA_ERROR__VISITORS__CYCLIC_DEPENDENCY_DETECTED_WHEN_RESOLVING_INSTRUCTION_DEFAULT_VALUES = 1200002;
var CODAMA_ERROR__VISITORS__CANNOT_USE_OPTIONAL_ACCOUNT_AS_PDA_SEED_VALUE = 1200003;
var CODAMA_ERROR__VISITORS__INVALID_INSTRUCTION_DEFAULT_VALUE_DEPENDENCY = 1200004;
var CODAMA_ERROR__VISITORS__ACCOUNT_FIELD_NOT_FOUND = 1200005;
var CODAMA_ERROR__VISITORS__INVALID_NUMBER_WRAPPER = 1200006;
var CODAMA_ERROR__VISITORS__CANNOT_EXTEND_MISSING_VISIT_FUNCTION = 1200007;
var CODAMA_ERROR__VISITORS__FAILED_TO_VALIDATE_NODE = 1200008;
var CODAMA_ERROR__VISITORS__INSTRUCTION_ENUM_ARGUMENT_NOT_FOUND = 1200009;
var CODAMA_ERROR__VISITORS__CANNOT_FLATTEN_STRUCT_WITH_CONFLICTING_ATTRIBUTES = 1200010;
var CODAMA_ERROR__VISITORS__RENDER_MAP_KEY_NOT_FOUND = 1200011;
var CODAMA_ERROR__VISITORS__CANNOT_REMOVE_LAST_PATH_IN_NODE_STACK = 1200012;
var CODAMA_ERROR__ANCHOR__UNRECOGNIZED_IDL_TYPE = 21e5;
var CODAMA_ERROR__ANCHOR__ACCOUNT_TYPE_MISSING = 2100001;
var CODAMA_ERROR__ANCHOR__ARGUMENT_TYPE_MISSING = 2100002;
var CODAMA_ERROR__ANCHOR__TYPE_PATH_MISSING = 2100003;
var CODAMA_ERROR__ANCHOR__SEED_KIND_UNIMPLEMENTED = 2100004;
var CODAMA_ERROR__ANCHOR__PROGRAM_ID_KIND_UNIMPLEMENTED = 2100005;
var CODAMA_ERROR__RENDERERS__UNSUPPORTED_NODE = 28e5;
// src/messages.ts
var CodamaErrorMessages = {
[CODAMA_ERROR__ANCHOR__ACCOUNT_TYPE_MISSING]: "Account type [$name] is missing from the IDL types.",
[CODAMA_ERROR__ANCHOR__ARGUMENT_TYPE_MISSING]: "Argument name [$name] is missing from the instruction definition.",
[CODAMA_ERROR__ANCHOR__PROGRAM_ID_KIND_UNIMPLEMENTED]: "Program ID kind [$kind] is not implemented.",
[CODAMA_ERROR__ANCHOR__SEED_KIND_UNIMPLEMENTED]: "Seed kind [$kind] is not implemented.",
[CODAMA_ERROR__ANCHOR__TYPE_PATH_MISSING]: "Field type is missing for path [$path] in [$idlType].",
[CODAMA_ERROR__ANCHOR__UNRECOGNIZED_IDL_TYPE]: "Unrecognized Anchor IDL type [$idlType].",
[CODAMA_ERROR__DISCRIMINATOR_FIELD_HAS_NO_DEFAULT_VALUE]: "Discriminator field [$field] has no default value.",
[CODAMA_ERROR__DISCRIMINATOR_FIELD_NOT_FOUND]: "Could not find discriminator field [$field]",
[CODAMA_ERROR__ENUM_VARIANT_NOT_FOUND]: "Enum variant [$variant] not found in enum type [$enumName].",
[CODAMA_ERROR__LINKED_NODE_NOT_FOUND]: "Could not find linked node [$name] from [$kind].",
[CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE]: "Node.js filesystem function [$fsFunction] is not available in your environment.",
[CODAMA_ERROR__RENDERERS__UNSUPPORTED_NODE]: "Cannot render the encountered node of kind [$kind].",
[CODAMA_ERROR__UNEXPECTED_NESTED_NODE_KIND]: "Expected nested node of kind [$expectedKinds], got [$kind]",
[CODAMA_ERROR__UNEXPECTED_NODE_KIND]: "Expected node of kind [$expectedKinds], got [$kind].",
[CODAMA_ERROR__UNRECOGNIZED_BYTES_ENCODING]: "Unrecognized bytes encoding [$encoding].",
[CODAMA_ERROR__UNRECOGNIZED_NODE_KIND]: "Unrecognized node kind [$kind].",
[CODAMA_ERROR__UNRECOGNIZED_NUMBER_FORMAT]: "Unrecognized number format [$format].",
[CODAMA_ERROR__VERSION_MISMATCH]: "The provided RootNode version [$rootVersion] is not compatible with the installed Codama version [$codamaVersion].",
[CODAMA_ERROR__VISITORS__ACCOUNT_FIELD_NOT_FOUND]: "Account [$name] does not have a field named [$missingField].",
[CODAMA_ERROR__VISITORS__CANNOT_ADD_DUPLICATED_PDA_NAMES]: "Cannot add PDAs to program [$programName] because the following PDA names already exist [$duplicatedPdaNames].",
[CODAMA_ERROR__VISITORS__CANNOT_EXTEND_MISSING_VISIT_FUNCTION]: "Cannot extend visitor with function [$visitFunction] as the base visitor does not support it.",
[CODAMA_ERROR__VISITORS__CANNOT_FLATTEN_STRUCT_WITH_CONFLICTING_ATTRIBUTES]: "Cannot flatten struct since this would cause the following attributes to conflict [$conflictingAttributes].",
[CODAMA_ERROR__VISITORS__CANNOT_REMOVE_LAST_PATH_IN_NODE_STACK]: "Cannot remove the last path in the node stack.",
[CODAMA_ERROR__VISITORS__CANNOT_USE_OPTIONAL_ACCOUNT_AS_PDA_SEED_VALUE]: "Cannot use optional account [$seedValueName] as the [$seedName] PDA seed for the [$instructionAccountName] account of the [$instructionName] instruction.",
[CODAMA_ERROR__VISITORS__CYCLIC_DEPENDENCY_DETECTED_WHEN_RESOLVING_INSTRUCTION_DEFAULT_VALUES]: "Circular dependency detected when resolving the accounts and arguments' default values of the [$instructionName] instruction. Got the following dependency cycle [$formattedCycle].",
[CODAMA_ERROR__VISITORS__FAILED_TO_VALIDATE_NODE]: "Failed to validate the given node [$formattedHistogram].",
[CODAMA_ERROR__VISITORS__INSTRUCTION_ENUM_ARGUMENT_NOT_FOUND]: "Could not find an enum argument named [$argumentName] for instruction [$instructionName].",
[CODAMA_ERROR__VISITORS__INVALID_INSTRUCTION_DEFAULT_VALUE_DEPENDENCY]: "Dependency [$dependencyName] of kind [$dependencyKind] is not a valid dependency of [$parentName] of kind [$parentKind] in the [$instructionName] instruction.",
[CODAMA_ERROR__VISITORS__INVALID_NUMBER_WRAPPER]: "Invalid number wrapper kind [$wrapper].",
[CODAMA_ERROR__VISITORS__INVALID_PDA_SEED_VALUES]: "Invalid seed values for PDA [$pdaName] in instruction [$instructionName].",
[CODAMA_ERROR__VISITORS__RENDER_MAP_KEY_NOT_FOUND]: "Cannot find key [$key] in RenderMap."
};
// src/message-formatter.ts
function getHumanReadableErrorMessage(code, context = {}) {
const messageFormatString = CodamaErrorMessages[code];
const message = messageFormatString.replace(
/(?<!\\)\$(\w+)/g,
(substring, variableName) => variableName in context ? `${context[variableName]}` : substring
);
return message;
}
// src/cli/index.ts
var program = new import_commander.Command();
program.name("@codama/errors").description("Decode Codama JavaScript errors thrown in production").version(version);
program.command("decode").description("Decode a `CodamaErrorCode` to a human-readable message").argument("<code>", "numeric error code to decode", (rawCode) => {
const code = parseInt(rawCode, 10);
if (isNaN(code) || `${code}` !== rawCode) {
throw new import_commander.InvalidArgumentError("It must be an integer");
}
if (!(code in CodamaErrorMessages)) {
throw new import_commander.InvalidArgumentError("There exists no error with that code");
}
return code;
}).argument("[encodedContext]", "encoded context to interpolate into the error message", (encodedContext) => {
try {
return decodeEncodedContext(encodedContext);
} catch {
throw new import_commander.InvalidArgumentError("Encoded context malformed");
}
}).action((code, context) => {
const message = getHumanReadableErrorMessage(code, context);
console.log(`
${import_chalk.default.bold(
import_chalk.default.rgb(154, 71, 255)("[") + import_chalk.default.rgb(144, 108, 244)("D") + import_chalk.default.rgb(134, 135, 233)("e") + import_chalk.default.rgb(122, 158, 221)("c") + import_chalk.default.rgb(110, 178, 209)("o") + import_chalk.default.rgb(95, 195, 196)("d") + import_chalk.default.rgb(79, 212, 181)("e") + import_chalk.default.rgb(57, 227, 166)("d") + import_chalk.default.rgb(19, 241, 149)("]")
) + import_chalk.default.rgb(19, 241, 149)(" Codama error code #" + code)}
- ${message}`);
if (context) {
console.log(`
${import_chalk.default.yellowBright(import_chalk.default.bold("[Context]"))}
${JSON.stringify(context, null, 4).split("\n").join("\n ")}`);
}
});
function run(argv) {
program.parse(argv);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
run
});
//# sourceMappingURL=cli.cjs.map