@thi.ng/errors 2.4.5 → 2.4.6
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 +1 -1
- package/README.md +1 -1
- package/assert.js +13 -23
- package/deferror.js +7 -4
- package/ensure.js +46 -113
- package/illegal-arguments.js +7 -3
- package/illegal-arity.js +7 -3
- package/illegal-state.js +7 -3
- package/io.js +12 -6
- package/out-of-bounds.js +11 -21
- package/package.json +6 -3
- package/unsupported.js +9 -3
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
package/assert.js
CHANGED
|
@@ -1,24 +1,14 @@
|
|
|
1
1
|
import { defError } from "./deferror.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
? import.meta.env.MODE !== "production" ||
|
|
16
|
-
!!import.meta.env.UMBRELLA_ASSERTS ||
|
|
17
|
-
!!import.meta.env.VITE_UMBRELLA_ASSERTS
|
|
18
|
-
: true)
|
|
19
|
-
? (test, msg) => {
|
|
20
|
-
if ((typeof test === "function" && !test()) || !test) {
|
|
21
|
-
throw new AssertionError(typeof msg === "function" ? msg() : msg);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
: () => { };
|
|
2
|
+
const AssertionError = defError(() => "Assertion failed");
|
|
3
|
+
const assert = (typeof process !== "undefined" && process.env !== void 0 ? process.env.NODE_ENV !== "production" || !!process.env.UMBRELLA_ASSERTS : import.meta.env ? import.meta.env.MODE !== "production" || !!import.meta.env.UMBRELLA_ASSERTS || !!import.meta.env.VITE_UMBRELLA_ASSERTS : true) ? (test, msg) => {
|
|
4
|
+
if (typeof test === "function" && !test() || !test) {
|
|
5
|
+
throw new AssertionError(
|
|
6
|
+
typeof msg === "function" ? msg() : msg
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
} : () => {
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
AssertionError,
|
|
13
|
+
assert
|
|
14
|
+
};
|
package/deferror.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
const defError = (prefix, suffix = (msg) => msg !== void 0 ? ": " + msg : "") => class extends Error {
|
|
2
|
+
constructor(msg) {
|
|
3
|
+
super(prefix(msg) + suffix(msg));
|
|
4
|
+
}
|
|
5
|
+
};
|
|
6
|
+
export {
|
|
7
|
+
defError
|
|
5
8
|
};
|
package/ensure.js
CHANGED
|
@@ -1,115 +1,48 @@
|
|
|
1
1
|
import { assert } from "./assert.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
2
|
+
const defEnsure = (pred, expected) => (x, msg) => {
|
|
3
|
+
x != null ? assert(
|
|
4
|
+
() => pred(x),
|
|
5
|
+
msg || `expected ${expected}, got ${typeof x}`
|
|
6
|
+
) : assert(false, `expected ${expected}, got ${x}`);
|
|
7
|
+
return x;
|
|
8
|
+
};
|
|
9
|
+
const ensureArray = defEnsure((x) => Array.isArray(x), `array`);
|
|
10
|
+
const ensureBigInt = defEnsure(
|
|
11
|
+
(x) => typeof x === "bigint",
|
|
12
|
+
"bigint"
|
|
13
|
+
);
|
|
14
|
+
const ensureBoolean = defEnsure(
|
|
15
|
+
(x) => typeof x === "boolean",
|
|
16
|
+
"boolean"
|
|
17
|
+
);
|
|
18
|
+
const ensureFunction = defEnsure(
|
|
19
|
+
(x) => typeof x === "function",
|
|
20
|
+
"function"
|
|
21
|
+
);
|
|
22
|
+
const ensureIterable = defEnsure(
|
|
23
|
+
(x) => typeof x[Symbol.iterator] === "function",
|
|
24
|
+
"iterable"
|
|
25
|
+
);
|
|
26
|
+
const ensureNumber = defEnsure(
|
|
27
|
+
(x) => typeof x === "number",
|
|
28
|
+
"number"
|
|
29
|
+
);
|
|
30
|
+
const ensureString = defEnsure(
|
|
31
|
+
(x) => typeof x === "string",
|
|
32
|
+
"string"
|
|
33
|
+
);
|
|
34
|
+
const ensureSymbol = defEnsure(
|
|
35
|
+
(x) => typeof x === "symbol",
|
|
36
|
+
"symbol"
|
|
37
|
+
);
|
|
38
|
+
export {
|
|
39
|
+
defEnsure,
|
|
40
|
+
ensureArray,
|
|
41
|
+
ensureBigInt,
|
|
42
|
+
ensureBoolean,
|
|
43
|
+
ensureFunction,
|
|
44
|
+
ensureIterable,
|
|
45
|
+
ensureNumber,
|
|
46
|
+
ensureString,
|
|
47
|
+
ensureSymbol
|
|
19
48
|
};
|
|
20
|
-
/**
|
|
21
|
-
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
|
|
22
|
-
* a JS array and if not throws {@link AssertionError}, optionally with given
|
|
23
|
-
* `msg`.
|
|
24
|
-
*
|
|
25
|
-
* @remarks
|
|
26
|
-
* See {@link defEnsure} for details.
|
|
27
|
-
*
|
|
28
|
-
* @param x
|
|
29
|
-
* @param msg
|
|
30
|
-
*/
|
|
31
|
-
export const ensureArray = defEnsure((x) => Array.isArray(x), `array`);
|
|
32
|
-
/**
|
|
33
|
-
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
|
|
34
|
-
* a bigint and if not throws {@link AssertionError}, optionally with given
|
|
35
|
-
* `msg`.
|
|
36
|
-
*
|
|
37
|
-
* @remarks
|
|
38
|
-
* See {@link defEnsure} for details.
|
|
39
|
-
*
|
|
40
|
-
* @param x
|
|
41
|
-
* @param msg
|
|
42
|
-
*/
|
|
43
|
-
export const ensureBigInt = defEnsure((x) => typeof x === "bigint", "bigint");
|
|
44
|
-
/**
|
|
45
|
-
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
|
|
46
|
-
* a boolean and if not throws {@link AssertionError}, optionally with given
|
|
47
|
-
* `msg`.
|
|
48
|
-
*
|
|
49
|
-
* @remarks
|
|
50
|
-
* See {@link defEnsure} for details.
|
|
51
|
-
*
|
|
52
|
-
* @param x
|
|
53
|
-
* @param msg
|
|
54
|
-
*/
|
|
55
|
-
export const ensureBoolean = defEnsure((x) => typeof x === "boolean", "boolean");
|
|
56
|
-
/**
|
|
57
|
-
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
|
|
58
|
-
* a function and if not throws {@link AssertionError}, optionally with given
|
|
59
|
-
* `msg`.
|
|
60
|
-
*
|
|
61
|
-
* @remarks
|
|
62
|
-
* See {@link defEnsure} for details.
|
|
63
|
-
*
|
|
64
|
-
* @param x
|
|
65
|
-
* @param msg
|
|
66
|
-
*/
|
|
67
|
-
export const ensureFunction = defEnsure((x) => typeof x === "function", "function");
|
|
68
|
-
/**
|
|
69
|
-
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
|
|
70
|
-
* an ES6 iterable and if not throws {@link AssertionError}, optionally with
|
|
71
|
-
* given `msg`.
|
|
72
|
-
*
|
|
73
|
-
* @remarks
|
|
74
|
-
* See {@link defEnsure} for details.
|
|
75
|
-
*
|
|
76
|
-
* @param x
|
|
77
|
-
* @param msg
|
|
78
|
-
*/
|
|
79
|
-
export const ensureIterable = defEnsure((x) => typeof x[Symbol.iterator] === "function", "iterable");
|
|
80
|
-
/**
|
|
81
|
-
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
|
|
82
|
-
* a number and if not throws {@link AssertionError}, optionally with given
|
|
83
|
-
* `msg`.
|
|
84
|
-
*
|
|
85
|
-
* @remarks
|
|
86
|
-
* See {@link defEnsure} for details.
|
|
87
|
-
*
|
|
88
|
-
* @param x
|
|
89
|
-
* @param msg
|
|
90
|
-
*/
|
|
91
|
-
export const ensureNumber = defEnsure((x) => typeof x === "number", "number");
|
|
92
|
-
/**
|
|
93
|
-
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
|
|
94
|
-
* a string and if not throws {@link AssertionError}, optionally with given
|
|
95
|
-
* `msg`.
|
|
96
|
-
*
|
|
97
|
-
* @remarks
|
|
98
|
-
* See {@link defEnsure} for details.
|
|
99
|
-
*
|
|
100
|
-
* @param x
|
|
101
|
-
* @param msg
|
|
102
|
-
*/
|
|
103
|
-
export const ensureString = defEnsure((x) => typeof x === "string", "string");
|
|
104
|
-
/**
|
|
105
|
-
* Only enabled if {@link assert} is enabled (otherwise no-op). Checks if `x` is
|
|
106
|
-
* a symbol and if not throws {@link AssertionError}, optionally with given
|
|
107
|
-
* `msg`.
|
|
108
|
-
*
|
|
109
|
-
* @remarks
|
|
110
|
-
* See {@link defEnsure} for details.
|
|
111
|
-
*
|
|
112
|
-
* @param x
|
|
113
|
-
* @param msg
|
|
114
|
-
*/
|
|
115
|
-
export const ensureSymbol = defEnsure((x) => typeof x === "symbol", "symbol");
|
package/illegal-arguments.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { defError } from "./deferror.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
const IllegalArgumentError = defError(() => "illegal argument(s)");
|
|
3
|
+
const illegalArgs = (msg) => {
|
|
4
|
+
throw new IllegalArgumentError(msg);
|
|
5
|
+
};
|
|
6
|
+
export {
|
|
7
|
+
IllegalArgumentError,
|
|
8
|
+
illegalArgs
|
|
5
9
|
};
|
package/illegal-arity.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { defError } from "./deferror.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
const IllegalArityError = defError(() => "illegal arity");
|
|
3
|
+
const illegalArity = (n) => {
|
|
4
|
+
throw new IllegalArityError(n);
|
|
5
|
+
};
|
|
6
|
+
export {
|
|
7
|
+
IllegalArityError,
|
|
8
|
+
illegalArity
|
|
5
9
|
};
|
package/illegal-state.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { defError } from "./deferror.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
const IllegalStateError = defError(() => "illegal state");
|
|
3
|
+
const illegalState = (msg) => {
|
|
4
|
+
throw new IllegalStateError(msg);
|
|
5
|
+
};
|
|
6
|
+
export {
|
|
7
|
+
IllegalStateError,
|
|
8
|
+
illegalState
|
|
5
9
|
};
|
package/io.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { defError } from "./deferror.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
const IOError = defError(() => "IO error");
|
|
3
|
+
const ioerror = (msg) => {
|
|
4
|
+
throw new IOError(msg);
|
|
5
5
|
};
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
const FileNotFoundError = defError(() => "File not found");
|
|
7
|
+
const fileNotFound = (path) => {
|
|
8
|
+
throw new FileNotFoundError(path);
|
|
9
|
+
};
|
|
10
|
+
export {
|
|
11
|
+
FileNotFoundError,
|
|
12
|
+
IOError,
|
|
13
|
+
fileNotFound,
|
|
14
|
+
ioerror
|
|
9
15
|
};
|
package/out-of-bounds.js
CHANGED
|
@@ -1,23 +1,13 @@
|
|
|
1
1
|
import { defError } from "./deferror.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
const OutOfBoundsError = defError(() => "index out of bounds");
|
|
3
|
+
const outOfBounds = (index) => {
|
|
4
|
+
throw new OutOfBoundsError(index);
|
|
5
|
+
};
|
|
6
|
+
const ensureIndex = (index, min, max) => (index < min || index >= max) && outOfBounds(index);
|
|
7
|
+
const ensureIndex2 = (x, y, maxX, maxY) => (x < 0 || x >= maxX || y < 0 || y >= maxY) && outOfBounds([x, y]);
|
|
8
|
+
export {
|
|
9
|
+
OutOfBoundsError,
|
|
10
|
+
ensureIndex,
|
|
11
|
+
ensureIndex2,
|
|
12
|
+
outOfBounds
|
|
5
13
|
};
|
|
6
|
-
/**
|
|
7
|
-
* Throws an {@link OutOfBoundsError} if `index` outside the `[min..max)` range.
|
|
8
|
-
*
|
|
9
|
-
* @param index -
|
|
10
|
-
* @param min -
|
|
11
|
-
* @param max -
|
|
12
|
-
*/
|
|
13
|
-
export const ensureIndex = (index, min, max) => (index < min || index >= max) && outOfBounds(index);
|
|
14
|
-
/**
|
|
15
|
-
* Throws an {@link OutOfBoundsError} if either `x` or `y` is outside their
|
|
16
|
-
* respective `[0..max)` range.
|
|
17
|
-
*
|
|
18
|
-
* @param x -
|
|
19
|
-
* @param y -
|
|
20
|
-
* @param maxX -
|
|
21
|
-
* @param maxY -
|
|
22
|
-
*/
|
|
23
|
-
export const ensureIndex2 = (x, y, maxX, maxY) => (x < 0 || x >= maxX || y < 0 || y >= maxY) && outOfBounds([x, y]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/errors",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.6",
|
|
4
4
|
"description": "Custom error types and error factory functions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -24,7 +24,9 @@
|
|
|
24
24
|
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
25
|
"license": "Apache-2.0",
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "yarn
|
|
27
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
28
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
29
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
28
30
|
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
|
|
29
31
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
30
32
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
@@ -35,6 +37,7 @@
|
|
|
35
37
|
"devDependencies": {
|
|
36
38
|
"@microsoft/api-extractor": "^7.38.3",
|
|
37
39
|
"@types/node": "^20.10.2",
|
|
40
|
+
"esbuild": "^0.19.8",
|
|
38
41
|
"rimraf": "^5.0.5",
|
|
39
42
|
"tools": "^0.0.1",
|
|
40
43
|
"typedoc": "^0.25.4",
|
|
@@ -94,5 +97,5 @@
|
|
|
94
97
|
"thi.ng": {
|
|
95
98
|
"year": 2018
|
|
96
99
|
},
|
|
97
|
-
"gitHead": "
|
|
100
|
+
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
|
|
98
101
|
}
|
package/unsupported.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { defError } from "./deferror.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
const UnsupportedOperationError = defError(
|
|
3
|
+
() => "unsupported operation"
|
|
4
|
+
);
|
|
5
|
+
const unsupported = (msg) => {
|
|
6
|
+
throw new UnsupportedOperationError(msg);
|
|
7
|
+
};
|
|
8
|
+
export {
|
|
9
|
+
UnsupportedOperationError,
|
|
10
|
+
unsupported
|
|
5
11
|
};
|