@deployanyway/error-translator 0.2.0 → 0.4.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/CHANGELOG.md +14 -0
- package/MIGRATION.md +11 -0
- package/README.md +109 -8
- package/bin/cli.js +1 -37
- package/dist/index.cjs +725 -0
- package/index.d.cts +25 -0
- package/index.d.ts +25 -0
- package/package.json +40 -9
- package/src/cli.js +74 -0
- package/src/definitions.js +2 -0
- package/src/extra-definitions.js +494 -0
- package/src/index.js +62 -0
- package/src/input.js +15 -0
package/index.d.cts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type ErrorInput =
|
|
2
|
+
string | Error | { code?: string; name?: string; message?: string };
|
|
3
|
+
export interface TranslationOptions {
|
|
4
|
+
mode?: "plain" | "rubber-duck";
|
|
5
|
+
}
|
|
6
|
+
export interface Translation {
|
|
7
|
+
code: string;
|
|
8
|
+
title: string;
|
|
9
|
+
explanation: string;
|
|
10
|
+
likelyCauses: string[];
|
|
11
|
+
suggestions: string[];
|
|
12
|
+
mode: "plain" | "rubber-duck";
|
|
13
|
+
}
|
|
14
|
+
export function translateError(
|
|
15
|
+
error: ErrorInput,
|
|
16
|
+
options?: TranslationOptions,
|
|
17
|
+
): Translation;
|
|
18
|
+
export function translateErrors(
|
|
19
|
+
errors: ErrorInput[],
|
|
20
|
+
options?: TranslationOptions,
|
|
21
|
+
): Translation[];
|
|
22
|
+
export function renderTranslation(result: Translation): string;
|
|
23
|
+
export function listErrors(): string[];
|
|
24
|
+
|
|
25
|
+
export function errorCatalog(options?: TranslationOptions): Translation[];
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type ErrorInput =
|
|
2
|
+
string | Error | { code?: string; name?: string; message?: string };
|
|
3
|
+
export interface TranslationOptions {
|
|
4
|
+
mode?: "plain" | "rubber-duck";
|
|
5
|
+
}
|
|
6
|
+
export interface Translation {
|
|
7
|
+
code: string;
|
|
8
|
+
title: string;
|
|
9
|
+
explanation: string;
|
|
10
|
+
likelyCauses: string[];
|
|
11
|
+
suggestions: string[];
|
|
12
|
+
mode: "plain" | "rubber-duck";
|
|
13
|
+
}
|
|
14
|
+
export function translateError(
|
|
15
|
+
error: ErrorInput,
|
|
16
|
+
options?: TranslationOptions,
|
|
17
|
+
): Translation;
|
|
18
|
+
export function translateErrors(
|
|
19
|
+
errors: ErrorInput[],
|
|
20
|
+
options?: TranslationOptions,
|
|
21
|
+
): Translation[];
|
|
22
|
+
export function renderTranslation(result: Translation): string;
|
|
23
|
+
export function listErrors(): string[];
|
|
24
|
+
|
|
25
|
+
export function errorCatalog(options?: TranslationOptions): Translation[];
|
package/package.json
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deployanyway/error-translator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Plain-English Node.js error explanations and debugging tips, because the stack trace has chosen violence.",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"exports":
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": {
|
|
9
|
+
"types": "./index.d.ts",
|
|
10
|
+
"default": "./src/index.js"
|
|
11
|
+
},
|
|
12
|
+
"require": {
|
|
13
|
+
"types": "./index.d.cts",
|
|
14
|
+
"default": "./dist/index.cjs"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
},
|
|
7
18
|
"bin": {
|
|
8
19
|
"error-translator": "./bin/cli.js"
|
|
9
20
|
},
|
|
@@ -12,16 +23,26 @@
|
|
|
12
23
|
"bin",
|
|
13
24
|
"README.md",
|
|
14
25
|
"LICENSE",
|
|
15
|
-
"CHANGELOG.md"
|
|
26
|
+
"CHANGELOG.md",
|
|
27
|
+
"dist",
|
|
28
|
+
"index.d.ts",
|
|
29
|
+
"index.d.cts",
|
|
30
|
+
"MIGRATION.md"
|
|
16
31
|
],
|
|
17
32
|
"engines": {
|
|
18
|
-
"node": ">=22"
|
|
33
|
+
"node": ">=22.13"
|
|
19
34
|
},
|
|
20
35
|
"scripts": {
|
|
21
|
-
"test": "node --test",
|
|
36
|
+
"test": "node --test test/*.test.js",
|
|
22
37
|
"lint": "eslint .",
|
|
23
38
|
"format": "prettier --write .",
|
|
24
|
-
"format:check": "prettier --check ."
|
|
39
|
+
"format:check": "prettier --check .",
|
|
40
|
+
"build": "node scripts/build.mjs",
|
|
41
|
+
"pretest": "npm run build",
|
|
42
|
+
"prepack": "npm run build",
|
|
43
|
+
"coverage": "c8 --all --include=src/**/*.js --reporter=text --reporter=json-summary --reporter=lcov --check-coverage --lines=90 --statements=90 --functions=90 --branches=85 node --test test/*.test.js",
|
|
44
|
+
"test:types": "tsc -p tsconfig.json",
|
|
45
|
+
"verify:package": "node scripts/verify-package.mjs"
|
|
25
46
|
},
|
|
26
47
|
"repository": {
|
|
27
48
|
"type": "git",
|
|
@@ -35,8 +56,12 @@
|
|
|
35
56
|
"access": "public"
|
|
36
57
|
},
|
|
37
58
|
"devDependencies": {
|
|
59
|
+
"@types/node": "^26.6.4",
|
|
60
|
+
"c8": "^12.0.0",
|
|
61
|
+
"esbuild": "^0.28.2",
|
|
38
62
|
"eslint": "^10.12.0",
|
|
39
|
-
"prettier": "^3.6.2"
|
|
63
|
+
"prettier": "^3.6.2",
|
|
64
|
+
"typescript": "^7.0.2"
|
|
40
65
|
},
|
|
41
66
|
"keywords": [
|
|
42
67
|
"nodejs",
|
|
@@ -46,6 +71,12 @@
|
|
|
46
71
|
"debugging",
|
|
47
72
|
"error-messages",
|
|
48
73
|
"error-translator",
|
|
49
|
-
"plain-english"
|
|
50
|
-
|
|
74
|
+
"plain-english",
|
|
75
|
+
"typescript",
|
|
76
|
+
"humor",
|
|
77
|
+
"deployanyway"
|
|
78
|
+
],
|
|
79
|
+
"main": "./dist/index.cjs",
|
|
80
|
+
"types": "./index.d.ts",
|
|
81
|
+
"homepage": "https://deployanyway.github.io/"
|
|
51
82
|
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { URL } from "node:url";
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import {
|
|
5
|
+
translateError,
|
|
6
|
+
renderTranslation,
|
|
7
|
+
translateErrors,
|
|
8
|
+
listErrors,
|
|
9
|
+
errorCatalog,
|
|
10
|
+
} from "./index.js";
|
|
11
|
+
|
|
12
|
+
import { readStdin } from "./input.js";
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
const { values, positionals } = parseArgs({
|
|
16
|
+
allowPositionals: true,
|
|
17
|
+
options: {
|
|
18
|
+
help: { type: "boolean", short: "h" },
|
|
19
|
+
version: { type: "boolean", short: "v" },
|
|
20
|
+
json: { type: "boolean" },
|
|
21
|
+
batch: { type: "boolean" },
|
|
22
|
+
list: { type: "boolean" },
|
|
23
|
+
catalog: { type: "boolean" },
|
|
24
|
+
mode: { type: "string", default: "plain" },
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
if (values.help) {
|
|
28
|
+
console.log(
|
|
29
|
+
'Usage: error-translator <error code or quoted message> [--json] [--mode plain|rubber-duck]\n\nTranslate errors into human-readable guidance. Without arguments, read stdin (256 KiB). --batch reads a JSON array of 1–100 errors. --list lists supported codes. --catalog prints all explanations and checks.\n\nOptions:\n -h, --help Show help\n -v, --version Show version\n --json Print a structured JSON result\n --mode plain|rubber-duck Keep useful guidance; add duck commentary\n\nExamples:\n error-translator ECONNREFUSED\n error-translator "TypeError: value is not a function" --json\n\nExit codes: 0 translation/help/version; 2 invalid arguments.',
|
|
30
|
+
);
|
|
31
|
+
} else if (values.version) {
|
|
32
|
+
console.log(
|
|
33
|
+
JSON.parse(
|
|
34
|
+
readFileSync(new URL("../package.json", import.meta.url), "utf8"),
|
|
35
|
+
).version,
|
|
36
|
+
);
|
|
37
|
+
} else if (values.catalog) {
|
|
38
|
+
if (positionals.length || values.batch || values.list)
|
|
39
|
+
throw new TypeError(
|
|
40
|
+
"--catalog does not accept input, --batch or --list.",
|
|
41
|
+
);
|
|
42
|
+
const catalog = errorCatalog({ mode: values.mode });
|
|
43
|
+
console.log(
|
|
44
|
+
values.json
|
|
45
|
+
? JSON.stringify(catalog, null, 2)
|
|
46
|
+
: catalog
|
|
47
|
+
.map((item) => item.code + " — " + renderTranslation(item))
|
|
48
|
+
.join("\n\n---\n\n"),
|
|
49
|
+
);
|
|
50
|
+
} else if (values.list) {
|
|
51
|
+
if (positionals.length || values.batch)
|
|
52
|
+
throw new TypeError("--list does not accept input or --batch.");
|
|
53
|
+
console.log(
|
|
54
|
+
values.json ? JSON.stringify(listErrors()) : listErrors().join("\n"),
|
|
55
|
+
);
|
|
56
|
+
} else {
|
|
57
|
+
const text = positionals.length ? positionals.join(" ") : await readStdin();
|
|
58
|
+
const result = values.batch
|
|
59
|
+
? translateErrors(JSON.parse(text), { mode: values.mode })
|
|
60
|
+
: translateError(text, { mode: values.mode });
|
|
61
|
+
console.log(
|
|
62
|
+
values.json
|
|
63
|
+
? JSON.stringify(result, null, 2)
|
|
64
|
+
: Array.isArray(result)
|
|
65
|
+
? result.map(renderTranslation).join("\n\n---\n\n")
|
|
66
|
+
: renderTranslation(result),
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.error(
|
|
71
|
+
`error-translator: ${error.message}\nRun with --help for usage.`,
|
|
72
|
+
);
|
|
73
|
+
process.exitCode = 2;
|
|
74
|
+
}
|
package/src/definitions.js
CHANGED