UNPKG

@hi18n/core

Version:

Message internationalization meets immutability and type-safety - core runtime

1 lines 6.76 kB
{"version":3,"file":"errors.mjs","names":["MessageError","Error","constructor","options","locale","id","restOptions","inLocale","cause","message","prototype","name","MissingTranslationError","NoLocaleError","MissingLocaleError","availableLocales","ParseError","MessageEvaluationError","MissingArgumentError","argName","ArgumentTypeError","expectedType","got"],"sources":["../src/errors.ts"],"sourcesContent":["/**\n * An error for a specific message. It is thrown when:\n *\n * - the message is missing,\n * - the message contains a syntax error, or\n * - the message cannot be evaluated with the supplied parameters.\n *\n * @since 0.1.7 (`@hi18n/core`)\n */\nexport class MessageError extends Error {\n public override readonly cause: Error;\n public readonly locale?: string | undefined;\n public readonly id: string;\n static {\n this.prototype.name = \"MessageError\";\n }\n constructor(\n options: ErrorOptions & {\n cause: Error;\n locale?: string | undefined;\n id: string;\n }\n ) {\n const { locale, id, ...restOptions } = options;\n\n const inLocale = locale != null ? ` in ${locale}` : \"\";\n\n super(\n `Error translating ${id}${inLocale}: ${options.cause.message}`,\n restOptions\n );\n this.locale = locale;\n this.id = id;\n this.cause ??= options.cause;\n }\n}\n\n/**\n * Missing translation. Usually wrapped in {@link MessageError}.\n *\n * @since 0.1.7 (`@hi18n/core`)\n */\nexport class MissingTranslationError extends Error {\n static {\n this.prototype.name = \"MissingTranslationError\";\n }\n constructor(message = \"Missing translation\", options?: ErrorOptions) {\n super(message, options);\n }\n}\n\n/**\n * No locale specified.\n *\n * @since 0.1.7 (`@hi18n/core`)\n */\nexport class NoLocaleError extends Error {\n static {\n this.prototype.name = \"NoLocaleError\";\n }\n constructor(message = \"No locale specified\", options?: ErrorOptions) {\n super(message, options);\n }\n}\n\n/**\n * Locale is specified, but no such locale exists in the book.\n *\n * @since 0.1.7 (`@hi18n/core`)\n */\nexport class MissingLocaleError extends Error {\n public readonly locale: string;\n public readonly availableLocales: readonly string[];\n static {\n this.prototype.name = \"MissingLocaleError\";\n }\n constructor(\n options: ErrorOptions & {\n locale: string;\n availableLocales: readonly string[];\n }\n ) {\n const { locale, availableLocales, ...restOptions } = options;\n\n super(`Missing locale: ${locale}`, restOptions);\n this.locale = locale;\n this.availableLocales = availableLocales;\n }\n}\n\n/**\n * Parse error. Usually wrapped in {@link MessageError}.\n *\n * @since 0.1.7 (`@hi18n/core`)\n */\nexport class ParseError extends Error {\n static {\n this.prototype.name = \"ParseError\";\n }\n}\n\n/**\n * An error during evaluating messages. Usually wrapped in {@link MessageError}.\n *\n * @since 0.1.7 (`@hi18n/core`)\n */\nexport class MessageEvaluationError extends Error {\n static {\n this.prototype.name = \"MessageEvaluationError\";\n }\n}\n\n/**\n * Missing translation argument. Usually wrapped in {@link MessageError}.\n *\n * @since 0.1.7 (`@hi18n/core`)\n */\nexport class MissingArgumentError extends MessageEvaluationError {\n public readonly argName: string | number;\n static {\n this.prototype.name = \"MissingArgumentError\";\n }\n constructor(\n options: ErrorOptions & {\n argName: string | number;\n }\n ) {\n const { argName, ...restOptions } = options;\n super(`Missing argument: ${argName}`, restOptions);\n this.argName = argName;\n }\n}\n\n/**\n * Translation argument type mismatch. Usually wrapped in {@link MessageError}.\n *\n * @since 0.1.7 (`@hi18n/core`)\n */\nexport class ArgumentTypeError extends MessageEvaluationError {\n public readonly argName: string | number;\n public readonly expectedType: string;\n public readonly got: unknown;\n static {\n this.prototype.name = \"ArgumentTypeError\";\n }\n constructor(\n options: ErrorOptions & {\n argName: string | number;\n expectedType: string;\n got: unknown;\n }\n ) {\n const { argName, expectedType, got, ...restOptions } = options;\n super(\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `Invalid argument ${argName}: expected ${expectedType}, got ${got}`,\n restOptions\n );\n this.argName = argName;\n this.expectedType = expectedType;\n this.got = got;\n }\n}\n"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMA,YAAN,SAA2BC,KAA3B,CAAiC;EAOtCC,WAAW,CACTC,OADS,EAMT;IAAA;;IACA,MAAM;MAAEC,MAAF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