@constructive-io/errors 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +23 -0
- package/README.md +139 -0
- package/classify.d.ts +10 -0
- package/classify.js +20 -0
- package/define.d.ts +16 -0
- package/define.js +11 -0
- package/error.d.ts +32 -0
- package/error.js +47 -0
- package/esm/classify.d.ts +10 -0
- package/esm/classify.js +16 -0
- package/esm/define.d.ts +16 -0
- package/esm/define.js +8 -0
- package/esm/error.d.ts +32 -0
- package/esm/error.js +43 -0
- package/esm/factory.d.ts +35 -0
- package/esm/factory.js +49 -0
- package/esm/format.d.ts +24 -0
- package/esm/format.js +54 -0
- package/esm/generated/registry.generated.d.ts +27 -0
- package/esm/generated/registry.generated.js +601 -0
- package/esm/index.d.ts +9 -0
- package/esm/index.js +9 -0
- package/esm/interpolate.d.ts +6 -0
- package/esm/interpolate.js +11 -0
- package/esm/parse.d.ts +30 -0
- package/esm/parse.js +199 -0
- package/esm/pg.d.ts +14 -0
- package/esm/pg.js +49 -0
- package/esm/registry.d.ts +147 -0
- package/esm/registry.js +468 -0
- package/esm/types.d.ts +81 -0
- package/esm/types.js +10 -0
- package/factory.d.ts +35 -0
- package/factory.js +55 -0
- package/format.d.ts +24 -0
- package/format.js +59 -0
- package/generated/registry.generated.d.ts +27 -0
- package/generated/registry.generated.js +604 -0
- package/index.d.ts +9 -0
- package/index.js +25 -0
- package/interpolate.d.ts +6 -0
- package/interpolate.js +14 -0
- package/package.json +45 -0
- package/parse.d.ts +30 -0
- package/parse.js +203 -0
- package/pg.d.ts +14 -0
- package/pg.js +53 -0
- package/registry.d.ts +147 -0
- package/registry.js +474 -0
- package/types.d.ts +81 -0
- package/types.js +11 -0
package/format.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_LOCALE = void 0;
|
|
4
|
+
exports.registerCatalog = registerCatalog;
|
|
5
|
+
exports.format = format;
|
|
6
|
+
const interpolate_1 = require("./interpolate");
|
|
7
|
+
const registry_1 = require("./registry");
|
|
8
|
+
/**
|
|
9
|
+
* Locale to use when `format` is called without one. This is a plain fallback
|
|
10
|
+
* label, not a special catalog: the untagged default copy lives in the registry
|
|
11
|
+
* itself, so there is no built-in `'en'` catalog to keep in sync.
|
|
12
|
+
*/
|
|
13
|
+
exports.DEFAULT_LOCALE = 'en';
|
|
14
|
+
/**
|
|
15
|
+
* Registered locale overlays. Empty by default — the base/default message for
|
|
16
|
+
* every code is the registry entry, so a locale only needs entries for the
|
|
17
|
+
* codes it actually translates.
|
|
18
|
+
*/
|
|
19
|
+
const catalogs = {};
|
|
20
|
+
/**
|
|
21
|
+
* Register or extend a locale catalog. Merges with any existing entries for
|
|
22
|
+
* that locale (new entries win), so hosts can override individual codes — pass
|
|
23
|
+
* the default locale to override the base copy.
|
|
24
|
+
*/
|
|
25
|
+
function registerCatalog(locale, catalog) {
|
|
26
|
+
catalogs[locale] = { ...catalogs[locale], ...catalog };
|
|
27
|
+
}
|
|
28
|
+
/** Humanize a code as a last-resort message, e.g. `LIMIT_REACHED` → `Limit reached`. */
|
|
29
|
+
function humanize(code) {
|
|
30
|
+
const lower = code.toLowerCase().replace(/_/g, ' ');
|
|
31
|
+
return lower.charAt(0).toUpperCase() + lower.slice(1);
|
|
32
|
+
}
|
|
33
|
+
/** The registry's default message for a code (string template or function). */
|
|
34
|
+
function defaultMessage(code, context) {
|
|
35
|
+
const def = (0, registry_1.getDefinition)(code);
|
|
36
|
+
if (def) {
|
|
37
|
+
if (typeof def.message === 'string')
|
|
38
|
+
return (0, interpolate_1.interpolate)(def.message, context);
|
|
39
|
+
if (typeof def.message === 'function') {
|
|
40
|
+
return def.message(context);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return humanize(code);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Render a localized message for a code.
|
|
47
|
+
*
|
|
48
|
+
* Resolution order:
|
|
49
|
+
* 1. requested-locale overlay entry (template) → interpolate
|
|
50
|
+
* 2. registry default message (template → interpolate, or function → call)
|
|
51
|
+
* 3. humanized code
|
|
52
|
+
*/
|
|
53
|
+
function format(code, context = {}, locale = exports.DEFAULT_LOCALE) {
|
|
54
|
+
const overlay = catalogs[locale]?.[code];
|
|
55
|
+
if (typeof overlay === 'string') {
|
|
56
|
+
return (0, interpolate_1.interpolate)(overlay, context);
|
|
57
|
+
}
|
|
58
|
+
return defaultMessage(code, context);
|
|
59
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GENERATED FILE — DO NOT EDIT BY HAND.
|
|
3
|
+
*
|
|
4
|
+
* Source of truth: the constructive-db error audit (289 distinct codes
|
|
5
|
+
* raised via EXCEPTION/THROW across deploy sources + generated output).
|
|
6
|
+
* Regenerate with `python3 scripts/generate-registry.py` (see README.md).
|
|
7
|
+
*
|
|
8
|
+
* Public copy is seeded from the dashboard error catalogs; developer/invariant
|
|
9
|
+
* codes carry their raw message (with %-args rendered as {{argN}}). Curated
|
|
10
|
+
* entries in `registry.ts` override anything here (typed context + refined copy).
|
|
11
|
+
*
|
|
12
|
+
* Counts: 289 total, 82 public, 207 internal.
|
|
13
|
+
*/
|
|
14
|
+
import { type DefinedError } from '../define';
|
|
15
|
+
import type { ErrorContext } from '../types';
|
|
16
|
+
export interface GeneratedCodeMeta {
|
|
17
|
+
class: 'public' | 'internal';
|
|
18
|
+
dynamic: boolean;
|
|
19
|
+
/** True when the code is only emitted by DB code generators (no hand source). */
|
|
20
|
+
generatedOnly: boolean;
|
|
21
|
+
}
|
|
22
|
+
/** Every constructive-db error code, keyed by code. */
|
|
23
|
+
export declare const generatedRegistry: Record<string, DefinedError<ErrorContext>>;
|
|
24
|
+
/** Audit metadata for each generated code. */
|
|
25
|
+
export declare const GENERATED_CODE_META: Record<string, GeneratedCodeMeta>;
|
|
26
|
+
/** Total number of codes collected from constructive-db. */
|
|
27
|
+
export declare const GENERATED_CODE_COUNT = 289;
|