@object-ui/core 2.0.0 โ 3.0.1
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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +23 -0
- package/dist/__benchmarks__/core.bench.d.ts +8 -0
- package/dist/__benchmarks__/core.bench.js +53 -0
- package/dist/errors/index.d.ts +75 -0
- package/dist/errors/index.js +224 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/registry/Registry.js +7 -1
- package/dist/theme/ThemeEngine.d.ts +23 -0
- package/dist/theme/ThemeEngine.js +69 -0
- package/dist/theme/index.d.ts +1 -1
- package/dist/theme/index.js +1 -1
- package/dist/utils/debug.d.ts +31 -0
- package/dist/utils/debug.js +62 -0
- package/dist/validation/validation-engine.js +7 -1
- package/package.json +3 -3
- package/src/__benchmarks__/core.bench.ts +64 -0
- package/src/errors/__tests__/errors.test.ts +292 -0
- package/src/errors/index.ts +270 -0
- package/src/index.ts +2 -0
- package/src/registry/Registry.ts +7 -1
- package/src/theme/ThemeEngine.ts +78 -0
- package/src/theme/__tests__/ThemeEngine.test.ts +62 -0
- package/src/theme/index.ts +2 -0
- package/src/utils/__tests__/debug.test.ts +83 -0
- package/src/utils/debug.ts +66 -0
- package/src/validation/validation-engine.ts +9 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Performance benchmark suite for @object-ui/core.
|
|
11
|
+
*
|
|
12
|
+
* Part of Q1 2026 roadmap ยง1.4 Test Coverage Improvement.
|
|
13
|
+
*
|
|
14
|
+
* Run with: npx vitest bench packages/core/src/__benchmarks__/
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { bench, describe } from 'vitest';
|
|
18
|
+
import { ExpressionEvaluator } from '@object-ui/core';
|
|
19
|
+
import { ComponentRegistry } from '@object-ui/core';
|
|
20
|
+
import { contrastRatio, meetsContrastLevel, hexToHSL } from '@object-ui/core';
|
|
21
|
+
|
|
22
|
+
describe('ExpressionEvaluator performance', () => {
|
|
23
|
+
const evaluator = new ExpressionEvaluator({ data: { name: 'Alice', age: 30, active: true } });
|
|
24
|
+
|
|
25
|
+
bench('evaluate simple string', () => {
|
|
26
|
+
evaluator.evaluate('Hello ${data.name}');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
bench('evaluate 100 expressions', () => {
|
|
30
|
+
for (let i = 0; i < 100; i++) {
|
|
31
|
+
evaluator.evaluate('Hello ${data.name}');
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe('ComponentRegistry performance', () => {
|
|
37
|
+
bench('get registered component', () => {
|
|
38
|
+
ComponentRegistry.get('button');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
bench('has check', () => {
|
|
42
|
+
ComponentRegistry.has('button');
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('Theme utilities performance', () => {
|
|
47
|
+
bench('hexToHSL conversion', () => {
|
|
48
|
+
hexToHSL('#336699');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
bench('contrastRatio calculation', () => {
|
|
52
|
+
contrastRatio('#000000', '#ffffff');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
bench('meetsContrastLevel check', () => {
|
|
56
|
+
meetsContrastLevel('#000000', '#ffffff', 'AA');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
bench('100 contrast checks', () => {
|
|
60
|
+
for (let i = 0; i < 100; i++) {
|
|
61
|
+
meetsContrastLevel('#336699', '#ffffff', 'AA');
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, it, expect } from 'vitest';
|
|
10
|
+
import {
|
|
11
|
+
ObjectUIError,
|
|
12
|
+
SchemaError,
|
|
13
|
+
RegistryError,
|
|
14
|
+
ExpressionError,
|
|
15
|
+
PluginError,
|
|
16
|
+
FieldValidationError,
|
|
17
|
+
ERROR_CODES,
|
|
18
|
+
createError,
|
|
19
|
+
formatErrorMessage,
|
|
20
|
+
isObjectUIError,
|
|
21
|
+
isErrorCode,
|
|
22
|
+
} from '../index';
|
|
23
|
+
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
// ObjectUIError base class
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
|
|
28
|
+
describe('ObjectUIError', () => {
|
|
29
|
+
it('should create an error with code and message', () => {
|
|
30
|
+
const err = new ObjectUIError('something broke', 'OBJUI-001');
|
|
31
|
+
|
|
32
|
+
expect(err).toBeInstanceOf(Error);
|
|
33
|
+
expect(err).toBeInstanceOf(ObjectUIError);
|
|
34
|
+
expect(err.name).toBe('ObjectUIError');
|
|
35
|
+
expect(err.message).toBe('something broke');
|
|
36
|
+
expect(err.code).toBe('OBJUI-001');
|
|
37
|
+
expect(err.details).toBeUndefined();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should accept optional details', () => {
|
|
41
|
+
const details = { type: 'fancy-button' };
|
|
42
|
+
const err = new ObjectUIError('oops', 'OBJUI-001', details);
|
|
43
|
+
|
|
44
|
+
expect(err.details).toEqual(details);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should serialize to JSON', () => {
|
|
48
|
+
const err = new ObjectUIError('oops', 'OBJUI-001', { a: 1 });
|
|
49
|
+
const json = err.toJSON();
|
|
50
|
+
|
|
51
|
+
expect(json).toEqual(
|
|
52
|
+
expect.objectContaining({
|
|
53
|
+
name: 'ObjectUIError',
|
|
54
|
+
message: 'oops',
|
|
55
|
+
code: 'OBJUI-001',
|
|
56
|
+
details: { a: 1 },
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
expect(json.stack).toBeDefined();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should have a proper stack trace', () => {
|
|
63
|
+
const err = new ObjectUIError('trace me', 'OBJUI-001');
|
|
64
|
+
expect(err.stack).toBeDefined();
|
|
65
|
+
expect(err.stack).toContain('trace me');
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
// ERROR_CODES registry
|
|
71
|
+
// ---------------------------------------------------------------------------
|
|
72
|
+
|
|
73
|
+
describe('ERROR_CODES', () => {
|
|
74
|
+
it('should contain all 10 defined error codes', () => {
|
|
75
|
+
const expectedCodes = [
|
|
76
|
+
'OBJUI-001',
|
|
77
|
+
'OBJUI-002',
|
|
78
|
+
'OBJUI-003',
|
|
79
|
+
'OBJUI-004',
|
|
80
|
+
'OBJUI-005',
|
|
81
|
+
'OBJUI-006',
|
|
82
|
+
'OBJUI-007',
|
|
83
|
+
'OBJUI-008',
|
|
84
|
+
'OBJUI-009',
|
|
85
|
+
'OBJUI-010',
|
|
86
|
+
];
|
|
87
|
+
|
|
88
|
+
for (const code of expectedCodes) {
|
|
89
|
+
expect(ERROR_CODES[code]).toBeDefined();
|
|
90
|
+
expect(ERROR_CODES[code].code).toBe(code);
|
|
91
|
+
expect(ERROR_CODES[code].message).toBeTruthy();
|
|
92
|
+
expect(ERROR_CODES[code].suggestion).toBeTruthy();
|
|
93
|
+
expect(ERROR_CODES[code].docUrl).toContain(code);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// ---------------------------------------------------------------------------
|
|
99
|
+
// isErrorCode type guard
|
|
100
|
+
// ---------------------------------------------------------------------------
|
|
101
|
+
|
|
102
|
+
describe('isErrorCode', () => {
|
|
103
|
+
it('should return true for known codes', () => {
|
|
104
|
+
expect(isErrorCode('OBJUI-001')).toBe(true);
|
|
105
|
+
expect(isErrorCode('OBJUI-010')).toBe(true);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('should return false for unknown codes', () => {
|
|
109
|
+
expect(isErrorCode('OBJUI-999')).toBe(false);
|
|
110
|
+
expect(isErrorCode(42)).toBe(false);
|
|
111
|
+
expect(isErrorCode(null)).toBe(false);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
// isObjectUIError type guard
|
|
117
|
+
// ---------------------------------------------------------------------------
|
|
118
|
+
|
|
119
|
+
describe('isObjectUIError', () => {
|
|
120
|
+
it('should return true for ObjectUIError instances', () => {
|
|
121
|
+
expect(isObjectUIError(new ObjectUIError('a', 'OBJUI-001'))).toBe(true);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should return true for subclass instances', () => {
|
|
125
|
+
expect(isObjectUIError(new SchemaError('bad schema'))).toBe(true);
|
|
126
|
+
expect(isObjectUIError(new ExpressionError('bad expr'))).toBe(true);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('should return false for plain errors', () => {
|
|
130
|
+
expect(isObjectUIError(new Error('plain'))).toBe(false);
|
|
131
|
+
expect(isObjectUIError('string')).toBe(false);
|
|
132
|
+
expect(isObjectUIError(null)).toBe(false);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// ---------------------------------------------------------------------------
|
|
137
|
+
// Specialized error classes
|
|
138
|
+
// ---------------------------------------------------------------------------
|
|
139
|
+
|
|
140
|
+
describe('SchemaError', () => {
|
|
141
|
+
it('should default to OBJUI-002', () => {
|
|
142
|
+
const err = new SchemaError('bad schema');
|
|
143
|
+
expect(err.name).toBe('SchemaError');
|
|
144
|
+
expect(err.code).toBe('OBJUI-002');
|
|
145
|
+
expect(err).toBeInstanceOf(ObjectUIError);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
describe('RegistryError', () => {
|
|
150
|
+
it('should accept a custom code', () => {
|
|
151
|
+
const err = new RegistryError('not found', 'OBJUI-001');
|
|
152
|
+
expect(err.name).toBe('RegistryError');
|
|
153
|
+
expect(err.code).toBe('OBJUI-001');
|
|
154
|
+
expect(err).toBeInstanceOf(ObjectUIError);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe('ExpressionError', () => {
|
|
159
|
+
it('should default to OBJUI-003', () => {
|
|
160
|
+
const err = new ExpressionError('eval failed');
|
|
161
|
+
expect(err.name).toBe('ExpressionError');
|
|
162
|
+
expect(err.code).toBe('OBJUI-003');
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
describe('PluginError', () => {
|
|
167
|
+
it('should accept a custom code', () => {
|
|
168
|
+
const err = new PluginError('missing dep', 'OBJUI-004');
|
|
169
|
+
expect(err.name).toBe('PluginError');
|
|
170
|
+
expect(err.code).toBe('OBJUI-004');
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe('FieldValidationError', () => {
|
|
175
|
+
it('should default to OBJUI-009', () => {
|
|
176
|
+
const err = new FieldValidationError('invalid field');
|
|
177
|
+
expect(err.name).toBe('FieldValidationError');
|
|
178
|
+
expect(err.code).toBe('OBJUI-009');
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// ---------------------------------------------------------------------------
|
|
183
|
+
// createError factory
|
|
184
|
+
// ---------------------------------------------------------------------------
|
|
185
|
+
|
|
186
|
+
describe('createError', () => {
|
|
187
|
+
it('should create a RegistryError for OBJUI-001', () => {
|
|
188
|
+
const err = createError('OBJUI-001', { type: 'fancy-button' });
|
|
189
|
+
expect(err).toBeInstanceOf(RegistryError);
|
|
190
|
+
expect(err.message).toBe('Unknown component type: "fancy-button"');
|
|
191
|
+
expect(err.code).toBe('OBJUI-001');
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('should create a SchemaError for OBJUI-002', () => {
|
|
195
|
+
const err = createError('OBJUI-002', { reason: 'missing type' });
|
|
196
|
+
expect(err).toBeInstanceOf(SchemaError);
|
|
197
|
+
expect(err.message).toContain('missing type');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('should create an ExpressionError for OBJUI-003', () => {
|
|
201
|
+
const err = createError('OBJUI-003', { expression: '${bad}' });
|
|
202
|
+
expect(err).toBeInstanceOf(ExpressionError);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('should create a PluginError for OBJUI-004', () => {
|
|
206
|
+
const err = createError('OBJUI-004', { dependency: 'chart-lib' });
|
|
207
|
+
expect(err).toBeInstanceOf(PluginError);
|
|
208
|
+
expect(err.message).toContain('chart-lib');
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('should create a PluginError for OBJUI-005', () => {
|
|
212
|
+
const err = createError('OBJUI-005', { plugin: 'grid' });
|
|
213
|
+
expect(err).toBeInstanceOf(PluginError);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('should create a PluginError for OBJUI-006', () => {
|
|
217
|
+
const err = createError('OBJUI-006', { plugin: 'kanban' });
|
|
218
|
+
expect(err).toBeInstanceOf(PluginError);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('should create a RegistryError for OBJUI-007', () => {
|
|
222
|
+
const err = createError('OBJUI-007', { namespace: 'old-ns' });
|
|
223
|
+
expect(err).toBeInstanceOf(RegistryError);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it('should create a FieldValidationError for OBJUI-009', () => {
|
|
227
|
+
const err = createError('OBJUI-009', { field: 'email' });
|
|
228
|
+
expect(err).toBeInstanceOf(FieldValidationError);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('should create a base ObjectUIError for OBJUI-008', () => {
|
|
232
|
+
const err = createError('OBJUI-008', { scope: 'readOnlyScope' });
|
|
233
|
+
expect(err).toBeInstanceOf(ObjectUIError);
|
|
234
|
+
expect(err.message).toContain('readOnlyScope');
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('should create a base ObjectUIError for OBJUI-010', () => {
|
|
238
|
+
const err = createError('OBJUI-010', { action: 'save' });
|
|
239
|
+
expect(err).toBeInstanceOf(ObjectUIError);
|
|
240
|
+
expect(err.message).toContain('save');
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('should handle unknown error codes gracefully', () => {
|
|
244
|
+
const err = createError('OBJUI-999');
|
|
245
|
+
expect(err).toBeInstanceOf(ObjectUIError);
|
|
246
|
+
expect(err.message).toContain('Unknown error code');
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('should pass through details', () => {
|
|
250
|
+
const err = createError('OBJUI-001', { type: 'x' }, { extra: true });
|
|
251
|
+
expect(err.details).toEqual({ extra: true });
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// ---------------------------------------------------------------------------
|
|
256
|
+
// formatErrorMessage
|
|
257
|
+
// ---------------------------------------------------------------------------
|
|
258
|
+
|
|
259
|
+
describe('formatErrorMessage', () => {
|
|
260
|
+
it('should include code and message', () => {
|
|
261
|
+
const err = new ObjectUIError('something broke', 'OBJUI-001');
|
|
262
|
+
const formatted = formatErrorMessage(err, false);
|
|
263
|
+
|
|
264
|
+
expect(formatted).toBe('[OBJUI-001] something broke');
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it('should include suggestion and docs in dev mode', () => {
|
|
268
|
+
const err = createError('OBJUI-001', { type: 'magic-box' });
|
|
269
|
+
const formatted = formatErrorMessage(err, true);
|
|
270
|
+
|
|
271
|
+
expect(formatted).toContain('[OBJUI-001]');
|
|
272
|
+
expect(formatted).toContain('๐ก Suggestion:');
|
|
273
|
+
expect(formatted).toContain('๐ Docs:');
|
|
274
|
+
expect(formatted).toContain('https://objectui.dev/docs/errors/OBJUI-001');
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('should not include suggestion in production mode', () => {
|
|
278
|
+
const err = createError('OBJUI-002', { reason: 'bad' });
|
|
279
|
+
const formatted = formatErrorMessage(err, false);
|
|
280
|
+
|
|
281
|
+
expect(formatted).not.toContain('๐ก Suggestion:');
|
|
282
|
+
expect(formatted).not.toContain('๐ Docs:');
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('should handle errors with unknown codes gracefully', () => {
|
|
286
|
+
const err = new ObjectUIError('custom', 'CUSTOM-001');
|
|
287
|
+
const formatted = formatErrorMessage(err, true);
|
|
288
|
+
|
|
289
|
+
// No entry in ERROR_CODES so no suggestion appended
|
|
290
|
+
expect(formatted).toBe('[CUSTOM-001] custom');
|
|
291
|
+
});
|
|
292
|
+
});
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Error Code Registry
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
export interface ErrorCodeEntry {
|
|
14
|
+
code: string;
|
|
15
|
+
message: string;
|
|
16
|
+
suggestion: string;
|
|
17
|
+
docUrl: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const BASE_DOC_URL = 'https://objectui.dev/docs/errors';
|
|
21
|
+
|
|
22
|
+
export const ERROR_CODES: Record<string, ErrorCodeEntry> = {
|
|
23
|
+
'OBJUI-001': {
|
|
24
|
+
code: 'OBJUI-001',
|
|
25
|
+
message: 'Unknown component type: "${type}"',
|
|
26
|
+
suggestion:
|
|
27
|
+
'Ensure the component is registered via registry.register() before rendering. Check for typos in the component type name.',
|
|
28
|
+
docUrl: `${BASE_DOC_URL}/OBJUI-001`,
|
|
29
|
+
},
|
|
30
|
+
'OBJUI-002': {
|
|
31
|
+
code: 'OBJUI-002',
|
|
32
|
+
message: 'Schema validation failed: ${reason}',
|
|
33
|
+
suggestion:
|
|
34
|
+
'Verify the schema against the ObjectUI JSON spec. Run the built-in schema validator for details.',
|
|
35
|
+
docUrl: `${BASE_DOC_URL}/OBJUI-002`,
|
|
36
|
+
},
|
|
37
|
+
'OBJUI-003': {
|
|
38
|
+
code: 'OBJUI-003',
|
|
39
|
+
message: 'Expression evaluation failed: ${expression}',
|
|
40
|
+
suggestion:
|
|
41
|
+
'Check the expression syntax and ensure all referenced variables exist in the current data scope.',
|
|
42
|
+
docUrl: `${BASE_DOC_URL}/OBJUI-003`,
|
|
43
|
+
},
|
|
44
|
+
'OBJUI-004': {
|
|
45
|
+
code: 'OBJUI-004',
|
|
46
|
+
message: 'Plugin dependency missing: "${dependency}"',
|
|
47
|
+
suggestion:
|
|
48
|
+
'Install the required dependency and register it before loading this plugin.',
|
|
49
|
+
docUrl: `${BASE_DOC_URL}/OBJUI-004`,
|
|
50
|
+
},
|
|
51
|
+
'OBJUI-005': {
|
|
52
|
+
code: 'OBJUI-005',
|
|
53
|
+
message: 'Plugin already loaded: "${plugin}"',
|
|
54
|
+
suggestion:
|
|
55
|
+
'A plugin with the same name is already registered. Remove the duplicate or use a unique plugin name.',
|
|
56
|
+
docUrl: `${BASE_DOC_URL}/OBJUI-005`,
|
|
57
|
+
},
|
|
58
|
+
'OBJUI-006': {
|
|
59
|
+
code: 'OBJUI-006',
|
|
60
|
+
message: 'Plugin not found: "${plugin}"',
|
|
61
|
+
suggestion:
|
|
62
|
+
'Verify the plugin name and ensure it has been registered before access.',
|
|
63
|
+
docUrl: `${BASE_DOC_URL}/OBJUI-006`,
|
|
64
|
+
},
|
|
65
|
+
'OBJUI-007': {
|
|
66
|
+
code: 'OBJUI-007',
|
|
67
|
+
message: 'Registry namespace deprecated: "${namespace}"',
|
|
68
|
+
suggestion:
|
|
69
|
+
'Migrate to the new namespace format. See the migration guide for details.',
|
|
70
|
+
docUrl: `${BASE_DOC_URL}/OBJUI-007`,
|
|
71
|
+
},
|
|
72
|
+
'OBJUI-008': {
|
|
73
|
+
code: 'OBJUI-008',
|
|
74
|
+
message: 'Read-only scope violation: "${scope}"',
|
|
75
|
+
suggestion:
|
|
76
|
+
'You are trying to mutate a read-only data scope. Use a writable scope or clone the data before modifying.',
|
|
77
|
+
docUrl: `${BASE_DOC_URL}/OBJUI-008`,
|
|
78
|
+
},
|
|
79
|
+
'OBJUI-009': {
|
|
80
|
+
code: 'OBJUI-009',
|
|
81
|
+
message: 'Invalid field configuration: "${field}"',
|
|
82
|
+
suggestion:
|
|
83
|
+
'Check the field schema for required properties (type, name). Ensure the field widget is registered.',
|
|
84
|
+
docUrl: `${BASE_DOC_URL}/OBJUI-009`,
|
|
85
|
+
},
|
|
86
|
+
'OBJUI-010': {
|
|
87
|
+
code: 'OBJUI-010',
|
|
88
|
+
message: 'Action execution failed: "${action}"',
|
|
89
|
+
suggestion:
|
|
90
|
+
'Verify the action handler is registered and that all required parameters are provided.',
|
|
91
|
+
docUrl: `${BASE_DOC_URL}/OBJUI-010`,
|
|
92
|
+
},
|
|
93
|
+
} as const;
|
|
94
|
+
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
// Type Guards
|
|
97
|
+
// ---------------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Check whether a string is a known ObjectUI error code.
|
|
101
|
+
*/
|
|
102
|
+
export function isErrorCode(code: unknown): code is keyof typeof ERROR_CODES {
|
|
103
|
+
return typeof code === 'string' && code in ERROR_CODES;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ---------------------------------------------------------------------------
|
|
107
|
+
// Base Error Class
|
|
108
|
+
// ---------------------------------------------------------------------------
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Base error class for all ObjectUI errors.
|
|
112
|
+
*/
|
|
113
|
+
export class ObjectUIError extends Error {
|
|
114
|
+
constructor(
|
|
115
|
+
message: string,
|
|
116
|
+
public code: string,
|
|
117
|
+
public details?: Record<string, unknown>,
|
|
118
|
+
) {
|
|
119
|
+
super(message);
|
|
120
|
+
this.name = 'ObjectUIError';
|
|
121
|
+
|
|
122
|
+
// Maintains proper stack trace for where error was thrown (only in V8)
|
|
123
|
+
if (Error.captureStackTrace) {
|
|
124
|
+
Error.captureStackTrace(this, this.constructor);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Convert error to JSON for logging / debugging.
|
|
130
|
+
*/
|
|
131
|
+
toJSON() {
|
|
132
|
+
return {
|
|
133
|
+
name: this.name,
|
|
134
|
+
message: this.message,
|
|
135
|
+
code: this.code,
|
|
136
|
+
details: this.details,
|
|
137
|
+
stack: this.stack,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Type guard to check if an error is an ObjectUIError.
|
|
144
|
+
*/
|
|
145
|
+
export function isObjectUIError(error: unknown): error is ObjectUIError {
|
|
146
|
+
return error instanceof ObjectUIError;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ---------------------------------------------------------------------------
|
|
150
|
+
// Specialized Error Classes
|
|
151
|
+
// ---------------------------------------------------------------------------
|
|
152
|
+
|
|
153
|
+
/** Thrown when a schema is invalid or fails validation. */
|
|
154
|
+
export class SchemaError extends ObjectUIError {
|
|
155
|
+
constructor(message: string, details?: Record<string, unknown>) {
|
|
156
|
+
super(message, 'OBJUI-002', details);
|
|
157
|
+
this.name = 'SchemaError';
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Thrown when a registry operation fails. */
|
|
162
|
+
export class RegistryError extends ObjectUIError {
|
|
163
|
+
constructor(message: string, code: string, details?: Record<string, unknown>) {
|
|
164
|
+
super(message, code, details);
|
|
165
|
+
this.name = 'RegistryError';
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Thrown when expression evaluation fails. */
|
|
170
|
+
export class ExpressionError extends ObjectUIError {
|
|
171
|
+
constructor(message: string, details?: Record<string, unknown>) {
|
|
172
|
+
super(message, 'OBJUI-003', details);
|
|
173
|
+
this.name = 'ExpressionError';
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Thrown when a plugin operation fails. */
|
|
178
|
+
export class PluginError extends ObjectUIError {
|
|
179
|
+
constructor(message: string, code: string, details?: Record<string, unknown>) {
|
|
180
|
+
super(message, code, details);
|
|
181
|
+
this.name = 'PluginError';
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Thrown when validation of user input / field config fails. */
|
|
186
|
+
export class FieldValidationError extends ObjectUIError {
|
|
187
|
+
constructor(message: string, details?: Record<string, unknown>) {
|
|
188
|
+
super(message, 'OBJUI-009', details);
|
|
189
|
+
this.name = 'FieldValidationError';
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// ---------------------------------------------------------------------------
|
|
194
|
+
// Factory & Formatting Helpers
|
|
195
|
+
// ---------------------------------------------------------------------------
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Interpolate a template string with the given params.
|
|
199
|
+
* Template variables use the `${key}` syntax.
|
|
200
|
+
*/
|
|
201
|
+
function interpolate(
|
|
202
|
+
template: string,
|
|
203
|
+
params: Record<string, string>,
|
|
204
|
+
): string {
|
|
205
|
+
return template.replace(/\$\{(\w+)\}/g, (_match, key: string) => {
|
|
206
|
+
if (!(key in params) && typeof process !== 'undefined' && process.env?.NODE_ENV !== 'production') {
|
|
207
|
+
console.warn(`[ObjectUI] Missing interpolation parameter "${key}" in error message template.`);
|
|
208
|
+
}
|
|
209
|
+
return params[key] ?? `\${${key}}`;
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Create an `ObjectUIError` (or subclass) from a known error code.
|
|
215
|
+
*
|
|
216
|
+
* @param code - A registered error code (e.g. `"OBJUI-001"`).
|
|
217
|
+
* @param params - Values to interpolate into the message template.
|
|
218
|
+
* @param details - Optional extra details attached to the error.
|
|
219
|
+
*/
|
|
220
|
+
export function createError(
|
|
221
|
+
code: string,
|
|
222
|
+
params: Record<string, string> = {},
|
|
223
|
+
details?: Record<string, unknown>,
|
|
224
|
+
): ObjectUIError {
|
|
225
|
+
const entry = ERROR_CODES[code];
|
|
226
|
+
const message = entry
|
|
227
|
+
? interpolate(entry.message, params)
|
|
228
|
+
: `Unknown error code: ${code}`;
|
|
229
|
+
|
|
230
|
+
switch (code) {
|
|
231
|
+
case 'OBJUI-002':
|
|
232
|
+
return new SchemaError(message, details);
|
|
233
|
+
case 'OBJUI-003':
|
|
234
|
+
return new ExpressionError(message, details);
|
|
235
|
+
case 'OBJUI-004':
|
|
236
|
+
case 'OBJUI-005':
|
|
237
|
+
case 'OBJUI-006':
|
|
238
|
+
return new PluginError(message, code, details);
|
|
239
|
+
case 'OBJUI-007':
|
|
240
|
+
case 'OBJUI-001':
|
|
241
|
+
return new RegistryError(message, code, details);
|
|
242
|
+
case 'OBJUI-009':
|
|
243
|
+
return new FieldValidationError(message, details);
|
|
244
|
+
default:
|
|
245
|
+
return new ObjectUIError(message, code, details);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Format an error message with actionable fix suggestions in development mode.
|
|
251
|
+
*
|
|
252
|
+
* @param error - The `ObjectUIError` to format.
|
|
253
|
+
* @param isDev - When `true`, appends the suggestion and documentation link.
|
|
254
|
+
*/
|
|
255
|
+
export function formatErrorMessage(
|
|
256
|
+
error: ObjectUIError,
|
|
257
|
+
isDev: boolean = typeof process !== 'undefined' &&
|
|
258
|
+
process.env?.NODE_ENV !== 'production',
|
|
259
|
+
): string {
|
|
260
|
+
const entry = ERROR_CODES[error.code];
|
|
261
|
+
|
|
262
|
+
let formatted = `[${error.code}] ${error.message}`;
|
|
263
|
+
|
|
264
|
+
if (isDev && entry) {
|
|
265
|
+
formatted += `\n\n๐ก Suggestion: ${entry.suggestion}`;
|
|
266
|
+
formatted += `\n๐ Docs: ${entry.docUrl}`;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return formatted;
|
|
270
|
+
}
|
package/src/index.ts
CHANGED
package/src/registry/Registry.ts
CHANGED
|
@@ -86,7 +86,13 @@ export class Registry<T = any> {
|
|
|
86
86
|
if (!meta?.namespace) {
|
|
87
87
|
console.warn(
|
|
88
88
|
`Registering component "${type}" without a namespace is deprecated. ` +
|
|
89
|
-
`Please provide a namespace in the meta parameter
|
|
89
|
+
`Please provide a namespace in the meta parameter.\n\n` +
|
|
90
|
+
` Migration:\n` +
|
|
91
|
+
` // Before (deprecated):\n` +
|
|
92
|
+
` registry.register('${type}', MyComponent);\n\n` +
|
|
93
|
+
` // After:\n` +
|
|
94
|
+
` registry.register('${type}', MyComponent, { namespace: 'my-plugin' });\n\n` +
|
|
95
|
+
` See: https://github.com/objectstack-ai/objectui/blob/main/MIGRATION_GUIDE.md`
|
|
90
96
|
);
|
|
91
97
|
}
|
|
92
98
|
|