@wener/utils 1.1.53 → 1.1.54
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/lib/errors/Errors.js +201 -2
- package/lib/index.js +9 -8
- package/lib/io/ArrayBuffers.js +559 -2
- package/lib/langs/parseDate.js +20 -0
- package/package.json +2 -2
- package/src/errors/Errors.ts +106 -1
- package/src/index.ts +10 -8
- package/src/io/ArrayBuffers.ts +676 -1
- package/src/langs/parseDate.ts +13 -0
- package/tsconfig.json +4 -13
- package/lib/errors/Errors.mod.js +0 -206
- package/lib/io/ArrayBuffers.mod.js +0 -531
- package/src/errors/Errors.mod.ts +0 -104
- package/src/io/ArrayBuffers.mod.ts +0 -670
package/src/errors/Errors.ts
CHANGED
|
@@ -1 +1,106 @@
|
|
|
1
|
-
|
|
1
|
+
import { DetailError, DetailHolder, type ErrorDetail, type ErrorDetailInit } from './DetailError';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
https://github.com/tc39/proposal-error-cause
|
|
5
|
+
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
|
|
6
|
+
Stage 3
|
|
7
|
+
Chrome 93, Safari 15, Node 16.9
|
|
8
|
+
|
|
9
|
+
https://www.npmjs.com/package/pony-cause
|
|
10
|
+
*/
|
|
11
|
+
export namespace Errors {
|
|
12
|
+
export const BadRequest: ErrorDetail = create({ status: 400 });
|
|
13
|
+
export const Unauthorized: ErrorDetail = create({ status: 403 });
|
|
14
|
+
export const Forbidden: ErrorDetail = create({ status: 403 });
|
|
15
|
+
export const NotFound: ErrorDetail = create({ status: 404 });
|
|
16
|
+
export const InternalServerError: ErrorDetail = create({ status: 500 });
|
|
17
|
+
export const NotImplemented: ErrorDetail = create({ status: 501 });
|
|
18
|
+
export const BadGateway = create({ status: 502, message: 'Bad Gateway' });
|
|
19
|
+
export const ServiceUnavailable: ErrorDetail = create({ status: 503 });
|
|
20
|
+
|
|
21
|
+
export const IllegalState = create({ status: 500, message: 'Illegal State' });
|
|
22
|
+
export const UnsupportedOperation = create({ status: 500, message: 'Unsupported Operation' });
|
|
23
|
+
export const IllegalArgument = create({ status: 400, message: 'Illegal Argument' });
|
|
24
|
+
export const InvalidType = create({ status: 400, message: 'Invalid Type' });
|
|
25
|
+
|
|
26
|
+
// known errors
|
|
27
|
+
// TypeError when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type.
|
|
28
|
+
// RangeError when a number is not within the correct range allowed.
|
|
29
|
+
// AggregateError when multiple errors need to be reported by an operation, for example by Promise.all().
|
|
30
|
+
// 例如 Promise.any() 会返回一个 AggregateError,其中包含所有 rejected 的 Promise 的错误。
|
|
31
|
+
// EvalError when an error occurs during the evaluation of JavaScript code.
|
|
32
|
+
// ReferenceError when a non-existent variable is referenced.
|
|
33
|
+
// SyntaxError when a syntax error occurs while parsing code in eval().
|
|
34
|
+
// URIError when encodeURI() or decodeURI() are passed invalid parameters.
|
|
35
|
+
// InternalError when an internal error in the JavaScript engine is thrown. E.g. "too much recursion".
|
|
36
|
+
// DOMException when an error occurs in the DOM.
|
|
37
|
+
|
|
38
|
+
export const resolvers: ((e: any) => ErrorDetail | void)[] = [];
|
|
39
|
+
|
|
40
|
+
export function create(init: ErrorDetailInit): ErrorDetail {
|
|
41
|
+
return new DetailHolder(init);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function ok(res: Response, o?: Partial<ErrorDetailInit>) {
|
|
45
|
+
if (res.ok) {
|
|
46
|
+
return res;
|
|
47
|
+
}
|
|
48
|
+
throw create({
|
|
49
|
+
description: res.statusText,
|
|
50
|
+
status: res.status,
|
|
51
|
+
...o,
|
|
52
|
+
metadata: { ...o?.metadata, response: res },
|
|
53
|
+
}).asError();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function resolve(e: any): ErrorDetail {
|
|
57
|
+
if (e instanceof DetailHolder) {
|
|
58
|
+
return e;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (e instanceof DetailError) {
|
|
62
|
+
return e.detail;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
for (const resolver of resolvers) {
|
|
66
|
+
const r = resolver(e);
|
|
67
|
+
if (r) {
|
|
68
|
+
return r;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (isError(e)) {
|
|
73
|
+
const { message, code, status } = e as any;
|
|
74
|
+
// can get status from NestJS HttpException
|
|
75
|
+
return new DetailHolder({ message, status: parseInt(status) || 500, code, cause: e });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return new DetailHolder({ message: e.message, status: 500, cause: e });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Check if the given value is an Error
|
|
83
|
+
* @see https://github.com/tc39/proposal-is-error
|
|
84
|
+
*/
|
|
85
|
+
export function isError(e: any): e is Error {
|
|
86
|
+
if ('isError' in Error) {
|
|
87
|
+
// will handle cross-realm
|
|
88
|
+
return (Error as any).isError(e);
|
|
89
|
+
}
|
|
90
|
+
return e instanceof Error;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function isAbort(e: any): e is Error {
|
|
94
|
+
if (!isError(e)) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return e.name === 'AbortError';
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function isTimeout(e: any): e is Error {
|
|
101
|
+
if (!isError(e)) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
return e.name === 'TimeoutError' || e.name === 'Timeout';
|
|
105
|
+
}
|
|
106
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -33,24 +33,26 @@ export const isPromise = Promises.isPromise;
|
|
|
33
33
|
export { timeout, TimeoutError } from './asyncs/timeout';
|
|
34
34
|
|
|
35
35
|
// langs
|
|
36
|
-
export {
|
|
37
|
-
export { shallowEqual } from './langs/shallowEqual';
|
|
36
|
+
export { classOf } from './langs/classOf';
|
|
38
37
|
export { deepEqual } from './langs/deepEqual';
|
|
39
38
|
export { deepFreeze } from './langs/deepFreeze';
|
|
40
|
-
export {
|
|
41
|
-
export {
|
|
39
|
+
export { getGlobalStates, setGlobalStates } from './langs/getGlobalStates';
|
|
40
|
+
export { getObjectId } from './langs/getObjectId';
|
|
41
|
+
export { ifPresent } from './langs/ifPresent';
|
|
42
42
|
export { isClass } from './langs/isClass';
|
|
43
43
|
export { isDefined } from './langs/isDefined';
|
|
44
44
|
export { isEmptyObject } from './langs/isEmptyObject';
|
|
45
|
+
export { isNil } from './langs/isNil';
|
|
45
46
|
export { isPlainObject } from './langs/isPlainObject';
|
|
46
|
-
export { ifPresent } from './langs/ifPresent';
|
|
47
|
-
export { parseBoolean } from './langs/parseBoolean';
|
|
48
47
|
export { maybeFunction, type MaybeFunction } from './langs/MaybeFunction';
|
|
49
48
|
export { memoize } from './langs/memoize';
|
|
50
49
|
export { mixin } from './langs/mixin';
|
|
50
|
+
export { parseBoolean } from './langs/parseBoolean';
|
|
51
|
+
export { parseDate } from './langs/parseDate';
|
|
52
|
+
export { shallowClone } from './langs/shallowClone';
|
|
53
|
+
export { shallowEqual } from './langs/shallowEqual';
|
|
54
|
+
|
|
51
55
|
export type { MixinFunction, MixinInstance, MixinReturnValue } from './langs/mixin';
|
|
52
|
-
export { getObjectId } from './langs/getObjectId';
|
|
53
|
-
export { getGlobalStates, setGlobalStates } from './langs/getGlobalStates';
|
|
54
56
|
|
|
55
57
|
export { AsyncCloser } from './langs/AsyncCloser';
|
|
56
58
|
export { Closer } from './langs/Closer';
|