@futdevpro/fsm-dynamo 1.15.13 → 1.15.15
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/build/_collections/utils/extract-error-message.util.d.ts +71 -0
- package/build/_collections/utils/extract-error-message.util.d.ts.map +1 -0
- package/build/_collections/utils/extract-error-message.util.js +186 -0
- package/build/_collections/utils/extract-error-message.util.js.map +1 -0
- package/build/_collections/utils/require-env.util.d.ts +86 -0
- package/build/_collections/utils/require-env.util.d.ts.map +1 -0
- package/build/_collections/utils/require-env.util.js +94 -0
- package/build/_collections/utils/require-env.util.js.map +1 -0
- package/build/_models/interfaces/environment/global-settings.interface.d.ts +22 -0
- package/build/_models/interfaces/environment/global-settings.interface.d.ts.map +1 -1
- package/build/index.d.ts +2 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2 -0
- package/build/index.js.map +1 -1
- package/package.json +2 -2
- package/src/_collections/utils/extract-error-message.util.spec.ts +204 -0
- package/src/_collections/utils/extract-error-message.util.ts +223 -0
- package/src/_collections/utils/object.util.spec.ts +1 -1
- package/src/_collections/utils/require-env.util.spec.ts +231 -0
- package/src/_collections/utils/require-env.util.ts +138 -0
- package/src/_models/interfaces/environment/global-settings.interface.ts +23 -0
- package/src/index.ts +2 -0
- package/src/_collections/utils/math/box-bounds.util.spec.ts +0 -124
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { DyFM_Error } from '../../_models/control-models/error.control-model';
|
|
2
|
+
import {
|
|
3
|
+
DyFM_extractErrorMessage,
|
|
4
|
+
DyFM_formatError,
|
|
5
|
+
DyFM_registerMalformedErrorSink,
|
|
6
|
+
DyFM_reportMalformedError,
|
|
7
|
+
} from './extract-error-message.util';
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* FR-015-P0-fix — DyFM_extractErrorMessage specs.
|
|
12
|
+
*
|
|
13
|
+
* Coverage:
|
|
14
|
+
* - DyFM_Error with __userMessage / _message / both
|
|
15
|
+
* - HttpErrorResponse-shaped (status + statusText + error body)
|
|
16
|
+
* - Plain Error.message
|
|
17
|
+
* - String input
|
|
18
|
+
* - null / undefined → fallback (never 'undefined undefined')
|
|
19
|
+
* - Empty object → 'Malformed error: {}' OR fallback (depending on json)
|
|
20
|
+
* - Circular reference handled (no infinite loop / no crash)
|
|
21
|
+
* - prefix option
|
|
22
|
+
* - includeHttpStatus toggle
|
|
23
|
+
* - DyFM_formatError convenience wrapper
|
|
24
|
+
* - DyFM_reportMalformedError sink registration
|
|
25
|
+
*
|
|
26
|
+
* MUST never emit:
|
|
27
|
+
* - literal 'undefined' anywhere in the returned string
|
|
28
|
+
* - '[object Object]'
|
|
29
|
+
* - bare empty string
|
|
30
|
+
*/
|
|
31
|
+
describe('| DyFM_extractErrorMessage', (): void => {
|
|
32
|
+
|
|
33
|
+
describe('| DyFM_Error inputs', (): void => {
|
|
34
|
+
it('| uses __userMessage when present', (): void => {
|
|
35
|
+
const err: DyFM_Error = new DyFM_Error({
|
|
36
|
+
status: 401,
|
|
37
|
+
message: 'Internal debug',
|
|
38
|
+
userMessage: 'Bad credentials',
|
|
39
|
+
errorCode: 'AUTH-001',
|
|
40
|
+
});
|
|
41
|
+
// userMessage from settings maps to __userMessage on the DyFM_Error instance
|
|
42
|
+
// (deprecated but still copied).
|
|
43
|
+
const result: string = DyFM_extractErrorMessage(err);
|
|
44
|
+
expect(result).toContain('Bad credentials');
|
|
45
|
+
expect(result).not.toContain('undefined');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('| falls back to _message when no __userMessage', (): void => {
|
|
49
|
+
const err: DyFM_Error = new DyFM_Error({
|
|
50
|
+
status: 500,
|
|
51
|
+
message: 'Database connection lost',
|
|
52
|
+
errorCode: 'DB-001',
|
|
53
|
+
});
|
|
54
|
+
const result: string = DyFM_extractErrorMessage(err);
|
|
55
|
+
expect(result).toContain('Database connection lost');
|
|
56
|
+
expect(result).not.toContain('undefined');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('| appends HTTP status when present', (): void => {
|
|
60
|
+
const err: DyFM_Error = new DyFM_Error({
|
|
61
|
+
status: 401,
|
|
62
|
+
message: 'Token expired',
|
|
63
|
+
errorCode: 'AUTH-002',
|
|
64
|
+
});
|
|
65
|
+
const result: string = DyFM_extractErrorMessage(err);
|
|
66
|
+
expect(result).toContain('Token expired');
|
|
67
|
+
expect(result).toContain('401');
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('| HttpErrorResponse-shaped inputs', (): void => {
|
|
72
|
+
it('| extracts from nested .error.__userMessage', (): void => {
|
|
73
|
+
const httpErr: object = {
|
|
74
|
+
status: 401,
|
|
75
|
+
statusText: 'Unauthorized',
|
|
76
|
+
error: {
|
|
77
|
+
__userMessage: 'Invalid username or password',
|
|
78
|
+
_errorCode: 'FAS-ACS-LOG1',
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
const result: string = DyFM_extractErrorMessage(httpErr);
|
|
82
|
+
expect(result).toContain('Invalid username or password');
|
|
83
|
+
expect(result).toContain('401');
|
|
84
|
+
expect(result).toContain('Unauthorized');
|
|
85
|
+
expect(result).not.toContain('undefined');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('| extracts from nested .error.message (no DyFM extras)', (): void => {
|
|
89
|
+
const httpErr: object = {
|
|
90
|
+
status: 500,
|
|
91
|
+
statusText: 'Internal Server Error',
|
|
92
|
+
error: { message: 'ECONNREFUSED' },
|
|
93
|
+
};
|
|
94
|
+
const result: string = DyFM_extractErrorMessage(httpErr);
|
|
95
|
+
expect(result).toContain('ECONNREFUSED');
|
|
96
|
+
expect(result).toContain('500');
|
|
97
|
+
expect(result).not.toContain('undefined');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('| status without statusText still rendered cleanly', (): void => {
|
|
101
|
+
const httpErr: object = {
|
|
102
|
+
status: 502,
|
|
103
|
+
error: { __userMessage: 'Upstream down' },
|
|
104
|
+
};
|
|
105
|
+
const result: string = DyFM_extractErrorMessage(httpErr);
|
|
106
|
+
expect(result).toContain('Upstream down');
|
|
107
|
+
expect(result).toContain('502');
|
|
108
|
+
expect(result).not.toContain('undefined');
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe('| primitive + edge inputs (the "undefined undefined" guard)', (): void => {
|
|
113
|
+
it('| null → fallback, never literal "undefined"', (): void => {
|
|
114
|
+
const result: string = DyFM_extractErrorMessage(null);
|
|
115
|
+
expect(result).not.toContain('undefined');
|
|
116
|
+
expect(result.length).toBeGreaterThan(0);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('| undefined → fallback', (): void => {
|
|
120
|
+
const result: string = DyFM_extractErrorMessage(undefined);
|
|
121
|
+
expect(result).not.toContain('undefined');
|
|
122
|
+
expect(result.length).toBeGreaterThan(0);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('| string input returned as-is', (): void => {
|
|
126
|
+
const result: string = DyFM_extractErrorMessage('Network unreachable');
|
|
127
|
+
expect(result).toBe('Network unreachable');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('| plain Error.message', (): void => {
|
|
131
|
+
const result: string = DyFM_extractErrorMessage(new Error('boom'));
|
|
132
|
+
expect(result).toContain('boom');
|
|
133
|
+
expect(result).not.toContain('undefined');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('| empty object → "Malformed error: {}" or fallback, NEVER bare undefined', (): void => {
|
|
137
|
+
const result: string = DyFM_extractErrorMessage({});
|
|
138
|
+
expect(result.length).toBeGreaterThan(0);
|
|
139
|
+
expect(result).not.toContain('undefined undefined');
|
|
140
|
+
// Either Malformed-error report or fallback — both are acceptable.
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('| HttpErrorResponse with ALL undefined fields → fallback, never literal "undefined undefined"', (): void => {
|
|
144
|
+
// This is the EXACT shape that produced the bug today:
|
|
145
|
+
// errorRes?.status, errorRes?.error?.status, errorRes?.statusText,
|
|
146
|
+
// errorRes?.error?.statusText all undefined.
|
|
147
|
+
const httpErr: object = { error: {} };
|
|
148
|
+
const result: string = DyFM_extractErrorMessage(httpErr, { prefix: 'Login' });
|
|
149
|
+
expect(result.startsWith('Login:')).toBeTrue();
|
|
150
|
+
expect(result).not.toContain('undefined');
|
|
151
|
+
expect(result).not.toContain('[object Object]');
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe('| circular reference safety', (): void => {
|
|
156
|
+
it('| does not crash + does not loop', (): void => {
|
|
157
|
+
const a: { name: string; self?: object } = { name: 'a' };
|
|
158
|
+
a.self = a;
|
|
159
|
+
const result: string = DyFM_extractErrorMessage(a);
|
|
160
|
+
expect(result.length).toBeGreaterThan(0);
|
|
161
|
+
expect(result).not.toContain('undefined');
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
describe('| prefix + includeHttpStatus options', (): void => {
|
|
166
|
+
it('| prefix prepends with ": "', (): void => {
|
|
167
|
+
const result: string = DyFM_extractErrorMessage('boom', { prefix: 'Login' });
|
|
168
|
+
expect(result).toBe('Login: boom');
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('| includeHttpStatus=false skips the (HTTP …) suffix', (): void => {
|
|
172
|
+
const httpErr: object = { status: 500, statusText: 'X', error: { message: 'm' } };
|
|
173
|
+
const result: string = DyFM_extractErrorMessage(httpErr, { includeHttpStatus: false });
|
|
174
|
+
expect(result).toContain('m');
|
|
175
|
+
expect(result).not.toContain('HTTP');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('| custom fallback honored', (): void => {
|
|
179
|
+
const result: string = DyFM_extractErrorMessage(null, { fallback: 'Custom retry message' });
|
|
180
|
+
expect(result).toContain('Custom retry message');
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe('| DyFM_formatError convenience wrapper', (): void => {
|
|
185
|
+
it('| applies prefix', (): void => {
|
|
186
|
+
const result: string = DyFM_formatError('boom', 'Register');
|
|
187
|
+
expect(result).toBe('Register: boom');
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
describe('| DyFM_reportMalformedError sink', (): void => {
|
|
192
|
+
it('| calls registered sink with err + context', (): void => {
|
|
193
|
+
const calls: Array<{ err: unknown; context?: string }> = [];
|
|
194
|
+
DyFM_registerMalformedErrorSink((err: unknown, context?: string): void => {
|
|
195
|
+
calls.push({ err: err, context: context });
|
|
196
|
+
});
|
|
197
|
+
DyFM_reportMalformedError({ weirdShape: true }, 'login-flow');
|
|
198
|
+
expect(calls.length).toBe(1);
|
|
199
|
+
expect(calls[0].context).toBe('login-flow');
|
|
200
|
+
// Reset sink (no public unregister; pass an explicit no-op for cleanup).
|
|
201
|
+
DyFM_registerMalformedErrorSink((): void => { /* no-op */ });
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
});
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { DyFM_AnyError, DyFM_Error } from '../../_models/control-models/error.control-model';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Options for `DyFM_extractErrorMessage`.
|
|
6
|
+
*/
|
|
7
|
+
export interface DyFM_ExtractErrorMessage_Options {
|
|
8
|
+
/**
|
|
9
|
+
* Optional prefix prepended to the result with ': '. e.g. `prefix: 'Login'`
|
|
10
|
+
* → `'Login: <message>'`. Useful for distinguishing the UI surface.
|
|
11
|
+
*/
|
|
12
|
+
prefix?: string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Fallback string when no usable message can be extracted. Defaults to
|
|
16
|
+
* a human-readable diagnostic that mentions checking server logs.
|
|
17
|
+
*/
|
|
18
|
+
fallback?: string;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* When `true`, the returned string includes HTTP status + statusText after
|
|
22
|
+
* the main message (in parens) for HTTP errors. Default: `true`.
|
|
23
|
+
*/
|
|
24
|
+
includeHttpStatus?: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Universal error → human-readable string extractor. **Never** returns the
|
|
30
|
+
* literal string `'undefined'` (or `'undefined undefined'`), `'[object Object]'`,
|
|
31
|
+
* or any other JS-rendering artifact. Walks the full DyFM_Error pipeline plus
|
|
32
|
+
* common Angular HttpErrorResponse + plain Error shapes, falling back to a
|
|
33
|
+
* meaningful diagnostic string when the input is malformed.
|
|
34
|
+
*
|
|
35
|
+
* Order of attempts (first non-empty wins):
|
|
36
|
+
* 1. `err.__userMessage` (DyFM_Error admin-actionable user message)
|
|
37
|
+
* 2. `err._message` (DyFM_Error aggregated debug message)
|
|
38
|
+
* 3. `err.message` (plain Error.message)
|
|
39
|
+
* 4. `err.userMessage` (deprecated DyFM_Error_Settings field, still seen
|
|
40
|
+
* on some payloads)
|
|
41
|
+
* 5. `err.error.__userMessage` (HttpErrorResponse wrapping a DyFM_Error in
|
|
42
|
+
* its body)
|
|
43
|
+
* 6. `err.error._message`
|
|
44
|
+
* 7. `err.error.message`
|
|
45
|
+
* 8. `err.error.userMessage`
|
|
46
|
+
* 9. If `err` itself is a string, use as-is
|
|
47
|
+
* 10. JSON-stringified payload (circular-safe; first 200 chars) as a last
|
|
48
|
+
* resort, prefixed with 'Malformed error: '
|
|
49
|
+
* 11. `options.fallback` (or its default)
|
|
50
|
+
*
|
|
51
|
+
* When `includeHttpStatus` is true (default) and the input looks like a
|
|
52
|
+
* HttpErrorResponse, the status + statusText are appended in parens: e.g.
|
|
53
|
+
* `'Bad credentials (HTTP 401 Unauthorized)'`.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* try {
|
|
58
|
+
* await auth_CS.login(account);
|
|
59
|
+
* } catch (err) {
|
|
60
|
+
* this.formError = DyFM_extractErrorMessage(err, { prefix: 'Login' });
|
|
61
|
+
* // → 'Login: Bad credentials (HTTP 401 Unauthorized)'
|
|
62
|
+
* // OR
|
|
63
|
+
* // → 'Login: Network unreachable — check connection. Server logs may have detail.'
|
|
64
|
+
* // never 'Login: undefined undefined'
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export function DyFM_extractErrorMessage(
|
|
69
|
+
err: DyFM_AnyError | unknown,
|
|
70
|
+
options?: DyFM_ExtractErrorMessage_Options,
|
|
71
|
+
): string {
|
|
72
|
+
const includeHttpStatus: boolean = options?.includeHttpStatus !== false;
|
|
73
|
+
const fallback: string = options?.fallback
|
|
74
|
+
?? 'Unexpected error (no detail in payload — check server logs).';
|
|
75
|
+
const prefix: string = options?.prefix ? `${options.prefix}: ` : '';
|
|
76
|
+
|
|
77
|
+
// Null/undefined/empty
|
|
78
|
+
if (err === null || err === undefined) {
|
|
79
|
+
return `${prefix}${fallback}`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// String → use as-is
|
|
83
|
+
if (typeof err === 'string') {
|
|
84
|
+
return `${prefix}${err}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Walk the candidate fields in order. Cast to any to access optional
|
|
88
|
+
// properties without forcing each branch into a separate type guard.
|
|
89
|
+
const e: any = err as any;
|
|
90
|
+
const innerError: any = e?.error;
|
|
91
|
+
|
|
92
|
+
const candidate: string | undefined =
|
|
93
|
+
_nonEmptyString(e?.__userMessage)
|
|
94
|
+
?? _nonEmptyString(e?._message)
|
|
95
|
+
?? _nonEmptyString(e?.message)
|
|
96
|
+
?? _nonEmptyString(e?.userMessage)
|
|
97
|
+
?? _nonEmptyString(innerError?.__userMessage)
|
|
98
|
+
?? _nonEmptyString(innerError?._message)
|
|
99
|
+
?? _nonEmptyString(innerError?.message)
|
|
100
|
+
?? _nonEmptyString(innerError?.userMessage);
|
|
101
|
+
|
|
102
|
+
// HTTP status suffix (always-on by default)
|
|
103
|
+
const status: number | string | undefined =
|
|
104
|
+
e?.___status
|
|
105
|
+
?? e?.status
|
|
106
|
+
?? innerError?.___status
|
|
107
|
+
?? innerError?.status;
|
|
108
|
+
const statusText: string | undefined =
|
|
109
|
+
_nonEmptyString(e?.statusText)
|
|
110
|
+
?? _nonEmptyString(innerError?.statusText);
|
|
111
|
+
const httpSuffix: string = (includeHttpStatus && (status !== undefined || statusText !== undefined))
|
|
112
|
+
? ` (HTTP ${status ?? '?'}${statusText ? ' ' + statusText : ''})`
|
|
113
|
+
: '';
|
|
114
|
+
|
|
115
|
+
if (candidate !== undefined) {
|
|
116
|
+
return `${prefix}${candidate}${httpSuffix}`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// No usable text field. Try a JSON-stringify of the raw payload (circular-safe).
|
|
120
|
+
const jsonAttempt: string | undefined = _safeJsonStringify(err);
|
|
121
|
+
if (jsonAttempt !== undefined && jsonAttempt !== '{}' && jsonAttempt !== '[]') {
|
|
122
|
+
const trimmed: string = jsonAttempt.length > 200
|
|
123
|
+
? jsonAttempt.slice(0, 200) + '…'
|
|
124
|
+
: jsonAttempt;
|
|
125
|
+
return `${prefix}Malformed error: ${trimmed}${httpSuffix}`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Total fallback.
|
|
129
|
+
return `${prefix}${fallback}${httpSuffix}`;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Returns the value if it's a non-empty string, else undefined.
|
|
135
|
+
* Trims and treats whitespace-only as empty. Internal helper.
|
|
136
|
+
*/
|
|
137
|
+
function _nonEmptyString(v: unknown): string | undefined {
|
|
138
|
+
if (typeof v !== 'string') { return undefined; }
|
|
139
|
+
const trimmed: string = v.trim();
|
|
140
|
+
return trimmed.length > 0 ? trimmed : undefined;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* JSON.stringify wrapper that survives circular references + non-serializable
|
|
146
|
+
* values. Returns undefined if even the safe fallback fails (e.g. a Proxy
|
|
147
|
+
* whose getter throws).
|
|
148
|
+
*/
|
|
149
|
+
function _safeJsonStringify(v: unknown): string | undefined {
|
|
150
|
+
try {
|
|
151
|
+
const seen: WeakSet<object> = new WeakSet<object>();
|
|
152
|
+
return JSON.stringify(v, (_key: string, value: unknown): unknown => {
|
|
153
|
+
if (typeof value === 'object' && value !== null) {
|
|
154
|
+
if (seen.has(value as object)) { return '[Circular]'; }
|
|
155
|
+
seen.add(value as object);
|
|
156
|
+
}
|
|
157
|
+
if (typeof value === 'function') { return '[Function]'; }
|
|
158
|
+
if (typeof value === 'undefined') { return '[undefined]'; }
|
|
159
|
+
return value;
|
|
160
|
+
});
|
|
161
|
+
} catch {
|
|
162
|
+
try { return String(v); } catch { return undefined; }
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Convenience: same as DyFM_extractErrorMessage but always includes a default
|
|
169
|
+
* prefix derived from the calling context, useful for adding diagnostic
|
|
170
|
+
* breadcrumbs without per-call boilerplate.
|
|
171
|
+
*/
|
|
172
|
+
export function DyFM_formatError(
|
|
173
|
+
err: DyFM_AnyError | unknown,
|
|
174
|
+
prefix: string,
|
|
175
|
+
): string {
|
|
176
|
+
return DyFM_extractErrorMessage(err, { prefix: prefix });
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Reports a raw error payload via the universal client-error capture system
|
|
182
|
+
* (FR-029-style). Implementation is delegated to a consumer-registered
|
|
183
|
+
* handler so dynamo-fsm stays Angular-agnostic; if no handler is registered
|
|
184
|
+
* the error is logged via `DyFM_Log.error` (which is the default no-op-safe
|
|
185
|
+
* sink). Consumers (e.g. A_ErrorHandler_ControlService) call
|
|
186
|
+
* `DyFM_registerMalformedErrorSink(fn)` once at bootstrap.
|
|
187
|
+
*
|
|
188
|
+
* The intent: when DyFM_extractErrorMessage falls back to 'Malformed error:'
|
|
189
|
+
* or the generic fallback, the raw payload should also be captured for
|
|
190
|
+
* post-mortem analysis (server error-log via FR-029 pipeline).
|
|
191
|
+
*/
|
|
192
|
+
let _malformedErrorSink: ((err: unknown, context?: string) => void) | undefined;
|
|
193
|
+
|
|
194
|
+
export function DyFM_registerMalformedErrorSink(
|
|
195
|
+
fn: (err: unknown, context?: string) => void,
|
|
196
|
+
): void {
|
|
197
|
+
_malformedErrorSink = fn;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function DyFM_reportMalformedError(
|
|
201
|
+
err: unknown,
|
|
202
|
+
context?: string,
|
|
203
|
+
): void {
|
|
204
|
+
if (_malformedErrorSink !== undefined) {
|
|
205
|
+
try {
|
|
206
|
+
_malformedErrorSink(err, context);
|
|
207
|
+
return;
|
|
208
|
+
} catch {
|
|
209
|
+
/* sink threw — fall through to default */
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// Default: best-effort console.error wrapped in DyFM_Error for stack capture.
|
|
213
|
+
try {
|
|
214
|
+
// Lazy require to avoid a hard cycle through error.control-model -> log.util.
|
|
215
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
216
|
+
const { DyFM_Log }: { DyFM_Log: { error(msg: string, ...args: unknown[]): void } } =
|
|
217
|
+
require('./log.util');
|
|
218
|
+
DyFM_Log.error(`[DyFM_reportMalformedError] ${context ?? 'unknown context'}`, err);
|
|
219
|
+
} catch {
|
|
220
|
+
// Last-ditch — should never happen since log.util has no further deps.
|
|
221
|
+
/* swallow */
|
|
222
|
+
}
|
|
223
|
+
}
|
|
@@ -114,7 +114,7 @@ describe('| DyFM_Object', () => {
|
|
|
114
114
|
expect(result).toEqual(expected);
|
|
115
115
|
});
|
|
116
116
|
|
|
117
|
-
xit('should resolve circular
|
|
117
|
+
xit('should resolve self-circular root reference', () => {
|
|
118
118
|
const obj: any = {};
|
|
119
119
|
obj.self = obj;
|
|
120
120
|
const expected = { self: 'CIRCULATION:ROOT;Object.self' };
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { DyFM_global_settings } from '../constants/global-settings.const';
|
|
2
|
+
import { DyFM_Error } from '../../_models/control-models/error.control-model';
|
|
3
|
+
import { DyFM_requireEnv } from './require-env.util';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* FR-015 Phase 2 Wave 0 — DyFM_requireEnv specs.
|
|
8
|
+
*
|
|
9
|
+
* Coverage:
|
|
10
|
+
* - Node path: returns process.env value when set
|
|
11
|
+
* - Browser path: returns DyFM_global_settings.envOverrides value when
|
|
12
|
+
* process.env is unset
|
|
13
|
+
* - Fallback path: returns options.fallback when both above unset
|
|
14
|
+
* - Throw path: structured DyFM_Error when nothing resolves and no
|
|
15
|
+
* fallback. Verifies errorCode, message contains the env-var name +
|
|
16
|
+
* deploy-target checklist, userMessage is admin-actionable,
|
|
17
|
+
* issuerService matches.
|
|
18
|
+
* - Override priority: process.env wins over envOverrides; envOverrides
|
|
19
|
+
* wins over fallback.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Cleans the test fixture between specs so leak-state from one test doesn't
|
|
25
|
+
* pollute another. We touch the live `DyFM_global_settings.envOverrides`
|
|
26
|
+
* because the spec exercises the same surface the production code reads.
|
|
27
|
+
*/
|
|
28
|
+
function clearOverrides(): void {
|
|
29
|
+
DyFM_global_settings.envOverrides = undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
describe('| DyFM_requireEnv', (): void => {
|
|
34
|
+
|
|
35
|
+
afterEach((): void => {
|
|
36
|
+
clearOverrides();
|
|
37
|
+
delete process.env['DyFM_TEST_REQUIRE_ENV_KEY'];
|
|
38
|
+
delete process.env['DyFM_TEST_REQUIRE_ENV_OTHER'];
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
describe('| Node path (process.env)', (): void => {
|
|
43
|
+
it('| returns process.env value when set', (): void => {
|
|
44
|
+
process.env['DyFM_TEST_REQUIRE_ENV_KEY'] = 'node-value';
|
|
45
|
+
const value: string = DyFM_requireEnv('DyFM_TEST_REQUIRE_ENV_KEY', {
|
|
46
|
+
errorCode: 'TEST-001',
|
|
47
|
+
issuerService: 'spec',
|
|
48
|
+
});
|
|
49
|
+
expect(value).toBe('node-value');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('| empty-string process.env value is treated as unset', (): void => {
|
|
53
|
+
// Truthy guard — empty string means "not configured".
|
|
54
|
+
process.env['DyFM_TEST_REQUIRE_ENV_KEY'] = '';
|
|
55
|
+
DyFM_global_settings.envOverrides = { 'DyFM_TEST_REQUIRE_ENV_KEY': 'browser-value' };
|
|
56
|
+
const value: string = DyFM_requireEnv('DyFM_TEST_REQUIRE_ENV_KEY', {
|
|
57
|
+
errorCode: 'TEST-002',
|
|
58
|
+
issuerService: 'spec',
|
|
59
|
+
});
|
|
60
|
+
expect(value).toBe('browser-value');
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
describe('| browser path (envOverrides)', (): void => {
|
|
66
|
+
it('| returns envOverrides value when process.env unset', (): void => {
|
|
67
|
+
DyFM_global_settings.envOverrides = { 'DyFM_TEST_REQUIRE_ENV_KEY': 'browser-value' };
|
|
68
|
+
const value: string = DyFM_requireEnv('DyFM_TEST_REQUIRE_ENV_KEY', {
|
|
69
|
+
errorCode: 'TEST-003',
|
|
70
|
+
issuerService: 'spec',
|
|
71
|
+
});
|
|
72
|
+
expect(value).toBe('browser-value');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('| handles other entries in the same map without interference', (): void => {
|
|
76
|
+
DyFM_global_settings.envOverrides = {
|
|
77
|
+
'DyFM_TEST_REQUIRE_ENV_KEY': 'value-a',
|
|
78
|
+
'DyFM_TEST_REQUIRE_ENV_OTHER': 'value-b',
|
|
79
|
+
};
|
|
80
|
+
expect(DyFM_requireEnv('DyFM_TEST_REQUIRE_ENV_KEY', { errorCode: 'X', issuerService: 's' }))
|
|
81
|
+
.toBe('value-a');
|
|
82
|
+
expect(DyFM_requireEnv('DyFM_TEST_REQUIRE_ENV_OTHER', { errorCode: 'X', issuerService: 's' }))
|
|
83
|
+
.toBe('value-b');
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
describe('| fallback path', (): void => {
|
|
89
|
+
it('| returns fallback when neither process.env nor envOverrides resolves', (): void => {
|
|
90
|
+
const value: string = DyFM_requireEnv('DyFM_TEST_REQUIRE_ENV_KEY', {
|
|
91
|
+
errorCode: 'TEST-004',
|
|
92
|
+
issuerService: 'spec',
|
|
93
|
+
fallback: 'fallback-value',
|
|
94
|
+
});
|
|
95
|
+
expect(value).toBe('fallback-value');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('| empty-string fallback is honored (explicit empty intent)', (): void => {
|
|
99
|
+
const value: string = DyFM_requireEnv('DyFM_TEST_REQUIRE_ENV_KEY', {
|
|
100
|
+
errorCode: 'TEST-005',
|
|
101
|
+
issuerService: 'spec',
|
|
102
|
+
fallback: '',
|
|
103
|
+
});
|
|
104
|
+
expect(value).toBe('');
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
describe('| throw path — rich DyFM_Error', (): void => {
|
|
110
|
+
it('| throws DyFM_Error when nothing resolves and no fallback', (): void => {
|
|
111
|
+
let caught: unknown = null;
|
|
112
|
+
try {
|
|
113
|
+
DyFM_requireEnv('DyFM_TEST_REQUIRE_ENV_KEY', {
|
|
114
|
+
errorCode: 'TEST-MISSING-001',
|
|
115
|
+
issuerService: 'dynamo-fsm-spec',
|
|
116
|
+
});
|
|
117
|
+
} catch (error: unknown) {
|
|
118
|
+
caught = error;
|
|
119
|
+
}
|
|
120
|
+
expect(caught).toBeInstanceOf(DyFM_Error);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('| error has the caller-supplied errorCode', (): void => {
|
|
124
|
+
try {
|
|
125
|
+
DyFM_requireEnv('DyFM_TEST_REQUIRE_ENV_KEY', {
|
|
126
|
+
errorCode: 'CUSTOM-CODE-XYZ',
|
|
127
|
+
issuerService: 's',
|
|
128
|
+
});
|
|
129
|
+
fail('should have thrown');
|
|
130
|
+
} catch (error: unknown) {
|
|
131
|
+
const err: DyFM_Error = error as DyFM_Error;
|
|
132
|
+
expect(err._errorCode).toBe('CUSTOM-CODE-XYZ');
|
|
133
|
+
// Also present in the aggregated _errorCodes list.
|
|
134
|
+
expect(err._errorCodes).toContain('CUSTOM-CODE-XYZ');
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('| error has status 500 (config error)', (): void => {
|
|
139
|
+
try {
|
|
140
|
+
DyFM_requireEnv('DyFM_TEST_REQUIRE_ENV_KEY', {
|
|
141
|
+
errorCode: 'C', issuerService: 's',
|
|
142
|
+
});
|
|
143
|
+
fail('should have thrown');
|
|
144
|
+
} catch (error: unknown) {
|
|
145
|
+
const err: DyFM_Error = error as DyFM_Error;
|
|
146
|
+
expect(err.___status).toBe(500);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('| error message names the env-var + lists deploy targets', (): void => {
|
|
151
|
+
try {
|
|
152
|
+
DyFM_requireEnv('SOME_SPECIFIC_VAR_NAME', {
|
|
153
|
+
errorCode: 'C', issuerService: 's',
|
|
154
|
+
});
|
|
155
|
+
fail('should have thrown');
|
|
156
|
+
} catch (error: unknown) {
|
|
157
|
+
const err: DyFM_Error = error as DyFM_Error;
|
|
158
|
+
const msg: string = err._message || '';
|
|
159
|
+
// Names the specific var
|
|
160
|
+
expect(msg).toContain('SOME_SPECIFIC_VAR_NAME');
|
|
161
|
+
// Lists each deploy target so an operator knows where to look
|
|
162
|
+
expect(msg).toContain('host .env');
|
|
163
|
+
expect(msg).toContain('docker-compose');
|
|
164
|
+
expect(msg).toContain('Overseer');
|
|
165
|
+
expect(msg).toContain('environment.ts');
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('| error has admin-actionable userMessage (custom or default)', (): void => {
|
|
170
|
+
// Default userMessage
|
|
171
|
+
try {
|
|
172
|
+
DyFM_requireEnv('DEFAULT_MSG_VAR', { errorCode: 'C', issuerService: 's' });
|
|
173
|
+
fail('should have thrown');
|
|
174
|
+
} catch (error: unknown) {
|
|
175
|
+
const err: DyFM_Error = error as DyFM_Error;
|
|
176
|
+
expect(err.__userMessage).toContain('DEFAULT_MSG_VAR');
|
|
177
|
+
expect(err.__userMessage).toContain('operator');
|
|
178
|
+
}
|
|
179
|
+
// Custom userMessage
|
|
180
|
+
try {
|
|
181
|
+
DyFM_requireEnv('CUSTOM_MSG_VAR', {
|
|
182
|
+
errorCode: 'C',
|
|
183
|
+
issuerService: 's',
|
|
184
|
+
userMessage: 'My custom admin instruction',
|
|
185
|
+
});
|
|
186
|
+
fail('should have thrown');
|
|
187
|
+
} catch (error: unknown) {
|
|
188
|
+
const err: DyFM_Error = error as DyFM_Error;
|
|
189
|
+
expect(err.__userMessage).toBe('My custom admin instruction');
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('| error has issuerService set', (): void => {
|
|
194
|
+
try {
|
|
195
|
+
DyFM_requireEnv('V', { errorCode: 'C', issuerService: 'my-package' });
|
|
196
|
+
fail('should have thrown');
|
|
197
|
+
} catch (error: unknown) {
|
|
198
|
+
const err: DyFM_Error = error as DyFM_Error;
|
|
199
|
+
expect(err.___issuerService).toBe('my-package');
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
describe('| lookup priority', (): void => {
|
|
206
|
+
it('| process.env wins over envOverrides', (): void => {
|
|
207
|
+
process.env['DyFM_TEST_REQUIRE_ENV_KEY'] = 'from-process';
|
|
208
|
+
DyFM_global_settings.envOverrides = { 'DyFM_TEST_REQUIRE_ENV_KEY': 'from-overrides' };
|
|
209
|
+
const value: string = DyFM_requireEnv('DyFM_TEST_REQUIRE_ENV_KEY', {
|
|
210
|
+
errorCode: 'C', issuerService: 's',
|
|
211
|
+
});
|
|
212
|
+
expect(value).toBe('from-process');
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('| envOverrides wins over fallback', (): void => {
|
|
216
|
+
DyFM_global_settings.envOverrides = { 'DyFM_TEST_REQUIRE_ENV_KEY': 'from-overrides' };
|
|
217
|
+
const value: string = DyFM_requireEnv('DyFM_TEST_REQUIRE_ENV_KEY', {
|
|
218
|
+
errorCode: 'C', issuerService: 's', fallback: 'from-fallback',
|
|
219
|
+
});
|
|
220
|
+
expect(value).toBe('from-overrides');
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('| process.env wins over fallback', (): void => {
|
|
224
|
+
process.env['DyFM_TEST_REQUIRE_ENV_KEY'] = 'from-process';
|
|
225
|
+
const value: string = DyFM_requireEnv('DyFM_TEST_REQUIRE_ENV_KEY', {
|
|
226
|
+
errorCode: 'C', issuerService: 's', fallback: 'from-fallback',
|
|
227
|
+
});
|
|
228
|
+
expect(value).toBe('from-process');
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
});
|