mnemonica 1.2.1 → 1.2.3
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/.ai/PROTOTYPE-CHAIN.md +4 -2
- package/.ai/rules-error-system.md +10 -0
- package/.ai/rules-instance-methods.md +1 -1
- package/FOR_HUMANS.md +27 -14
- package/README.md +2 -2
- package/build/api/errors/exceptionConstructor.js +20 -47
- package/build/api/errors/throwModificationError.js +26 -61
- package/build/api/types/InstanceCreator.js +5 -1
- package/build/api/types/Props.js +24 -10
- package/build/types/index.d.ts +9 -4
- package/build/types/index.js +1 -1
- package/docs/async-constructors.md +2 -0
- package/package.json +1 -1
- package/src/api/errors/exceptionConstructor.ts +40 -83
- package/src/api/errors/throwModificationError.ts +64 -104
- package/src/api/types/InstanceCreator.ts +14 -0
- package/src/api/types/Props.ts +53 -18
- package/src/types/index.ts +15 -5
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
import type {
|
|
4
|
-
MnemonicaError, InstanceCreatorContext
|
|
4
|
+
MnemonicaError, ErrorProps, InstanceCreatorContext
|
|
5
5
|
} from '../../types';
|
|
6
6
|
|
|
7
7
|
import { constants } from '../../constants';
|
|
@@ -21,9 +21,9 @@ import {
|
|
|
21
21
|
import TypesUtils from '../utils';
|
|
22
22
|
const { makeErrorModificatorType } = TypesUtils;
|
|
23
23
|
|
|
24
|
-
import {
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
import {
|
|
25
|
+
getProps, setProps
|
|
26
|
+
} from '../types/Props';
|
|
27
27
|
|
|
28
28
|
import { makeInstanceModificator } from '../types/InstanceModificator';
|
|
29
29
|
|
|
@@ -45,51 +45,34 @@ export const throwModificationError = function ( this: InstanceCreatorContext, e
|
|
|
45
45
|
// debugger;
|
|
46
46
|
// }
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
// error data (exceptionReason, reasons, surplus, args, originalError,
|
|
49
|
+
// instance) is never defined on error objects — it lives in the external
|
|
50
|
+
// props storage (WeakMap), read it via getProps(error).
|
|
51
|
+
// NOTE: the public getProps is required here (not _getProps) because the
|
|
52
|
+
// error data sits in the additions slot of the record — _getProps returns
|
|
53
|
+
// the raw record only and would miss it.
|
|
54
|
+
const errorProps = getProps( error ) as unknown as ErrorProps | undefined;
|
|
55
|
+
|
|
56
|
+
const exceptionReason = (
|
|
57
|
+
errorProps !== undefined &&
|
|
58
|
+
errorProps.exceptionReason !== undefined
|
|
59
|
+
) ?
|
|
60
|
+
errorProps.exceptionReason :
|
|
61
|
+
error;
|
|
49
62
|
|
|
50
|
-
if (
|
|
63
|
+
if ( errorProps !== undefined && errorProps.exceptionReason !== undefined ) {
|
|
51
64
|
|
|
52
|
-
(
|
|
53
|
-
(
|
|
65
|
+
(errorProps.reasons as unknown[]).push( errorProps.exceptionReason );
|
|
66
|
+
(errorProps.surplus as unknown[]).push( error );
|
|
54
67
|
|
|
55
68
|
throw error;
|
|
56
69
|
|
|
57
70
|
}
|
|
58
71
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
get () {
|
|
64
|
-
return exceptionReason;
|
|
65
|
-
},
|
|
66
|
-
enumerable : true
|
|
67
|
-
}
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
const reasons: Error[] = [ exceptionReason ];
|
|
71
|
-
|
|
72
|
-
odp(
|
|
73
|
-
error,
|
|
74
|
-
'reasons',
|
|
75
|
-
{
|
|
76
|
-
get () {
|
|
77
|
-
return reasons;
|
|
78
|
-
},
|
|
79
|
-
enumerable : true
|
|
80
|
-
}
|
|
81
|
-
);
|
|
82
|
-
const surplus: Error[] = [];
|
|
83
|
-
odp(
|
|
84
|
-
error,
|
|
85
|
-
'surplus',
|
|
86
|
-
{
|
|
87
|
-
get () {
|
|
88
|
-
return surplus;
|
|
89
|
-
},
|
|
90
|
-
enumerable : true
|
|
91
|
-
}
|
|
92
|
-
);
|
|
72
|
+
// reasons/surplus may hold non-Error reason objects (bound method
|
|
73
|
+
// failures push { methodName, ... } shaped reasons), hence unknown[]
|
|
74
|
+
const reasons: unknown[] = [ exceptionReason ];
|
|
75
|
+
const surplus: unknown[] = [];
|
|
93
76
|
|
|
94
77
|
self.ModificatorType = makeErrorModificatorType( TypeName );
|
|
95
78
|
|
|
@@ -98,6 +81,17 @@ export const throwModificationError = function ( this: InstanceCreatorContext, e
|
|
|
98
81
|
// let erroredInstance = new self.InstanceModificator();
|
|
99
82
|
const erroredInstance = new self.InstanceModificator();
|
|
100
83
|
|
|
84
|
+
// the first portion of error data: stored against the failed
|
|
85
|
+
// construction layer props, so getProps(erroredInstance) exposes it
|
|
86
|
+
setProps(
|
|
87
|
+
erroredInstance,
|
|
88
|
+
{
|
|
89
|
+
exceptionReason,
|
|
90
|
+
reasons,
|
|
91
|
+
surplus
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
|
|
101
95
|
let errorProto: object | null = Reflect.getPrototypeOf( erroredInstance );
|
|
102
96
|
let isMnemonicaInstance = false;
|
|
103
97
|
while ( errorProto ) {
|
|
@@ -145,14 +139,26 @@ export const throwModificationError = function ( this: InstanceCreatorContext, e
|
|
|
145
139
|
|
|
146
140
|
const title = `\n<-- creation of [ ${TypeName} ] traced -->`;
|
|
147
141
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
142
|
+
if ( self.inheritedInstance instanceof Promise ) {
|
|
143
|
+
|
|
144
|
+
// async construction failure: this runs from makeAwaiter's .catch,
|
|
145
|
+
// long after the `new` call site has unwound — a fresh capture here
|
|
146
|
+
// would hold only rejection-processing frames. runAsyncHandling
|
|
147
|
+
// captured the creation stack at new-time instead; use it.
|
|
148
|
+
stack.push( ...(self.stack as string[]) );
|
|
149
|
+
|
|
150
|
+
} else {
|
|
151
|
+
|
|
152
|
+
getStack.call(
|
|
153
|
+
erroredInstance,
|
|
154
|
+
title,
|
|
155
|
+
[],
|
|
156
|
+
throwModificationError
|
|
157
|
+
);
|
|
154
158
|
|
|
155
|
-
|
|
159
|
+
stack.push( ...(erroredInstance as { stack: string[] }).stack );
|
|
160
|
+
|
|
161
|
+
}
|
|
156
162
|
|
|
157
163
|
const errorStack = (error.stack as string ).split( '\n' );
|
|
158
164
|
|
|
@@ -206,66 +212,20 @@ export const throwModificationError = function ( this: InstanceCreatorContext, e
|
|
|
206
212
|
|
|
207
213
|
// if hooks had some interception: stop
|
|
208
214
|
|
|
209
|
-
|
|
215
|
+
// the rest of error data, available only when the error is thrown
|
|
216
|
+
setProps(
|
|
210
217
|
erroredInstance,
|
|
211
|
-
'args',
|
|
212
218
|
{
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
219
|
+
args,
|
|
220
|
+
originalError : error,
|
|
221
|
+
instance : erroredInstance
|
|
216
222
|
}
|
|
217
223
|
);
|
|
218
224
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
get () {
|
|
224
|
-
return error;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
odp(
|
|
230
|
-
erroredInstance,
|
|
231
|
-
'instance',
|
|
232
|
-
{
|
|
233
|
-
get () {
|
|
234
|
-
return erroredInstance;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
);
|
|
238
|
-
|
|
239
|
-
odp(
|
|
240
|
-
erroredInstance,
|
|
241
|
-
'extract',
|
|
242
|
-
{
|
|
243
|
-
get () {
|
|
244
|
-
const extractGetter = () => {
|
|
245
|
-
// mnemonica instances always have a parent object,
|
|
246
|
-
// so the runtime value is guaranteed to be object here
|
|
247
|
-
const _parent = parent(erroredInstance) as object;
|
|
248
|
-
const extractResult = extract(_parent);
|
|
249
|
-
return extractResult;
|
|
250
|
-
};
|
|
251
|
-
return extractGetter;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
);
|
|
255
|
-
|
|
256
|
-
odp(
|
|
257
|
-
erroredInstance,
|
|
258
|
-
'parse',
|
|
259
|
-
{
|
|
260
|
-
get () {
|
|
261
|
-
const parseGetter = () => {
|
|
262
|
-
const parseResult = parse( erroredInstance );
|
|
263
|
-
return parseResult;
|
|
264
|
-
};
|
|
265
|
-
return parseGetter;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
);
|
|
225
|
+
// no bound .extract()/.parse() here: since v1.0.6 instances expose no
|
|
226
|
+
// methods, so errors follow — call utils.extract(...) / utils.parse(...)
|
|
227
|
+
// on the thrown error (or on utils.parent(error) for the pre-failure
|
|
228
|
+
// layer, which the old bound .extract() used to return)
|
|
269
229
|
}
|
|
270
230
|
|
|
271
231
|
throw erroredInstance;
|
|
@@ -410,6 +410,20 @@ const runBuild = function ( self: InstanceCreatorContext, args: unknown[] ) {
|
|
|
410
410
|
|
|
411
411
|
const runAsyncHandling = function ( self: InstanceCreatorContext, type: TypeDef ) {
|
|
412
412
|
if ( self.inheritedInstance instanceof Promise ) {
|
|
413
|
+
// Async construction: capture the creation stack NOW, while the `new`
|
|
414
|
+
// call site is still on the stack. When a rejection gets wrapped later
|
|
415
|
+
// (makeAwaiter .catch → throwModificationError), the call site has
|
|
416
|
+
// already unwound, and a fresh capture would hold nothing but
|
|
417
|
+
// rejection-processing frames. Skipped when runSetup already captured
|
|
418
|
+
// one (submitStack).
|
|
419
|
+
if ( !Array.isArray( self.stack ) ) {
|
|
420
|
+
const title = `\n<-- creation of [ ${type.TypeName} ] traced -->`;
|
|
421
|
+
getStack.call(
|
|
422
|
+
self,
|
|
423
|
+
title,
|
|
424
|
+
[]
|
|
425
|
+
);
|
|
426
|
+
}
|
|
413
427
|
const waiter = self.makeAwaiter( type );
|
|
414
428
|
odp(
|
|
415
429
|
waiter,
|
package/src/api/types/Props.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { constants } from '../../constants';
|
|
4
4
|
import type {
|
|
5
|
-
CollectionDef, InstanceCreatorContext, Props as PropsType, TypeDef
|
|
5
|
+
CollectionDef, InstanceCreatorContext, Props as PropsType, TypeDef
|
|
6
6
|
} from '../../types';
|
|
7
7
|
|
|
8
8
|
const { odp, } = constants;
|
|
@@ -11,7 +11,17 @@ const { odp, } = constants;
|
|
|
11
11
|
// Instance metadata (__type__, __parent__, __args__, etc.) is stored
|
|
12
12
|
// against the prototype object, not the instance itself, so it never
|
|
13
13
|
// shows up in for...in, Object.keys, or JSON.stringify.
|
|
14
|
+
//
|
|
15
|
+
// Records may also be keyed by an object itself (not its prototype):
|
|
16
|
+
// this is how error instances carry their data (args, originalError,
|
|
17
|
+
// instance, exceptionReason, reasons, surplus) — nothing is defined
|
|
18
|
+
// on the error object, getProps(error) finds the record directly.
|
|
19
|
+
// That second key-space lives in a separate WeakMap: memory layer
|
|
20
|
+
// objects are themselves keys in __props__, so sharing one map would
|
|
21
|
+
// conflate the two spaces (a layer would "find" its own record when
|
|
22
|
+
// the caller meant to reach the parent layer's one).
|
|
14
23
|
const __props__ = new WeakMap();
|
|
24
|
+
const __object_props__ = new WeakMap();
|
|
15
25
|
|
|
16
26
|
const nativeProps = new Set([
|
|
17
27
|
'__proto_proto__',
|
|
@@ -28,7 +38,7 @@ const nativeProps = new Set([
|
|
|
28
38
|
|
|
29
39
|
export const _addProps = function (this: InstanceCreatorContext): void {
|
|
30
40
|
|
|
31
|
-
|
|
41
|
+
|
|
32
42
|
const self = this;
|
|
33
43
|
|
|
34
44
|
const {
|
|
@@ -143,8 +153,8 @@ export const _addProps = function (this: InstanceCreatorContext): void {
|
|
|
143
153
|
|
|
144
154
|
// __props__.set(self, value);
|
|
145
155
|
__props__.set(
|
|
146
|
-
proto!,
|
|
147
|
-
value
|
|
156
|
+
proto!,
|
|
157
|
+
value
|
|
148
158
|
);
|
|
149
159
|
|
|
150
160
|
};
|
|
@@ -157,6 +167,13 @@ const isObjectNature = (instance: unknown) => {
|
|
|
157
167
|
|
|
158
168
|
export const _getProps = (instance: object, base?: object): PropsType | undefined => {
|
|
159
169
|
if (!isObjectNature(instance)) return undefined;
|
|
170
|
+
// object-keyed records (error props etc.) take precedence over
|
|
171
|
+
// the prototype-layer records looked up below
|
|
172
|
+
const ownRecord = __object_props__.get(instance);
|
|
173
|
+
if (ownRecord !== undefined) {
|
|
174
|
+
const ownResult = ownRecord as PropsType;
|
|
175
|
+
return ownResult;
|
|
176
|
+
}
|
|
160
177
|
const proto = Reflect.getPrototypeOf(instance) as object;
|
|
161
178
|
if (
|
|
162
179
|
base !== undefined &&
|
|
@@ -227,32 +244,50 @@ export const getProps = (instance: object): PropsType | undefined => {
|
|
|
227
244
|
|
|
228
245
|
export const setProps = (instance: object, _values: object): string[] | false => {
|
|
229
246
|
const props = _getProps(instance);
|
|
247
|
+
const prevAdditions = props ? __props__.get(props) : undefined;
|
|
248
|
+
const values = Object.getOwnPropertyDescriptors(_values);
|
|
249
|
+
const written: string[] = [];
|
|
250
|
+
const allowed = {};
|
|
251
|
+
if (prevAdditions instanceof Object) {
|
|
252
|
+
// repeated setProps calls accumulate: previous additions are the
|
|
253
|
+
// base, new values override per key
|
|
254
|
+
Object.defineProperties(
|
|
255
|
+
allowed,
|
|
256
|
+
Object.getOwnPropertyDescriptors(prevAdditions)
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
Object.entries(values).forEach(([ name, value ]) => {
|
|
260
|
+
if (!nativeProps.has(name)) {
|
|
261
|
+
written.push(name);
|
|
262
|
+
Object.defineProperty(
|
|
263
|
+
allowed,
|
|
264
|
+
name,
|
|
265
|
+
value
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
});
|
|
230
269
|
if (props) {
|
|
231
|
-
const values = Object.getOwnPropertyDescriptors(_values);
|
|
232
|
-
const written: string[] = [];
|
|
233
|
-
const allowed = {};
|
|
234
|
-
Object.entries(values).forEach(([ name, value ]) => {
|
|
235
|
-
if (!nativeProps.has(name)) {
|
|
236
|
-
written.push(name);
|
|
237
|
-
Object.defineProperty(
|
|
238
|
-
allowed,
|
|
239
|
-
name,
|
|
240
|
-
value
|
|
241
|
-
);
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
270
|
__props__.set(
|
|
245
271
|
props,
|
|
246
272
|
allowed
|
|
247
273
|
);
|
|
248
274
|
return written;
|
|
249
275
|
}
|
|
276
|
+
if (isObjectNature(instance)) {
|
|
277
|
+
// no props record anywhere in the chain (plain objects, Error
|
|
278
|
+
// objects, library errors) — start one, keyed by the object itself
|
|
279
|
+
__object_props__.set(
|
|
280
|
+
instance,
|
|
281
|
+
allowed
|
|
282
|
+
);
|
|
283
|
+
return written;
|
|
284
|
+
}
|
|
250
285
|
return false;
|
|
251
286
|
};
|
|
252
287
|
|
|
253
288
|
// Re-export types from centralized types
|
|
254
289
|
export type {
|
|
255
|
-
CollectionDef, TypeDef, PropsType as Props
|
|
290
|
+
CollectionDef, TypeDef, PropsType as Props
|
|
256
291
|
};
|
|
257
292
|
|
|
258
293
|
module.exports = {
|
package/src/types/index.ts
CHANGED
|
@@ -54,11 +54,21 @@ export interface MnemonicaErrorConstructor {
|
|
|
54
54
|
// Errors types map - indexable record of error constructors
|
|
55
55
|
export type ErrorsTypesMap = Record<string, MnemonicaErrorConstructor>;
|
|
56
56
|
|
|
57
|
-
// Mnemonica error
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
// Mnemonica-processed error. It carries no custom properties of its own:
|
|
58
|
+
// the error data lives in the external props storage (WeakMap) — read it
|
|
59
|
+
// via getProps(error), typed as ErrorProps below.
|
|
60
|
+
export type MnemonicaError = Error;
|
|
61
|
+
|
|
62
|
+
// Error-instance data props, stored externally (see api/types/Props.ts)
|
|
63
|
+
// and exposed through getProps(error) — never defined on the error object
|
|
64
|
+
export interface ErrorProps {
|
|
65
|
+
args?: unknown[];
|
|
66
|
+
originalError?: Error;
|
|
67
|
+
instance?: object;
|
|
68
|
+
exceptionReason?: unknown;
|
|
69
|
+
reasons?: unknown[];
|
|
70
|
+
surplus?: unknown[];
|
|
71
|
+
error?: unknown;
|
|
62
72
|
}
|
|
63
73
|
|
|
64
74
|
/**
|