sitesmith: filter known-benign invariants from diagnostic shim
Craft.js uses several invariants as try/catched control-flow checks (notably isDraggable -> 'A top-level Node cannot be moved' for ROOT and linkedNode children). These fire on every render and are NOT errors — they're how Craft.js asks 'should I attach drag to this node?'. Filter them out of the shim's console.error so only genuinely-broken invariants show up.
This commit is contained in:
@@ -3,15 +3,26 @@
|
|||||||
* the failure message, so we lose the actual reason for any Craft.js
|
* the failure message, so we lose the actual reason for any Craft.js
|
||||||
* Invariant. Aliasing tiny-invariant -> this shim restores it.
|
* Invariant. Aliasing tiny-invariant -> this shim restores it.
|
||||||
*/
|
*/
|
||||||
|
/**
|
||||||
|
* Some Craft.js invariants are used as control-flow (try/catch wraps them
|
||||||
|
* to test a condition without explicit branching). Logging every one would
|
||||||
|
* drown out the actually-interesting failures. Suppress the well-known
|
||||||
|
* "is-it-X check" messages and log only the unfamiliar ones.
|
||||||
|
*/
|
||||||
|
const BENIGN = new Set<string>([
|
||||||
|
'A top-level Node cannot be moved',
|
||||||
|
'Cannot drag a non-canvas Node onto another Node',
|
||||||
|
]);
|
||||||
|
|
||||||
export default function invariant(condition: unknown, message?: string | (() => string)): asserts condition {
|
export default function invariant(condition: unknown, message?: string | (() => string)): asserts condition {
|
||||||
if (condition) return;
|
if (condition) return;
|
||||||
const provided = typeof message === 'function' ? message() : message;
|
const provided = typeof message === 'function' ? message() : message;
|
||||||
const value = provided ? `Invariant failed: ${provided}` : 'Invariant failed';
|
const value = provided ? `Invariant failed: ${provided}` : 'Invariant failed';
|
||||||
// Surface the message + stack to the console BEFORE throwing so even
|
if (!provided || !BENIGN.has(String(provided))) {
|
||||||
// an uncaught throw leaves a trace we can read.
|
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.error('[CRAFT INVARIANT]', value);
|
console.error('[CRAFT INVARIANT]', value);
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.error(new Error('stack-only').stack);
|
console.error(new Error('stack-only').stack);
|
||||||
|
}
|
||||||
throw new Error(value);
|
throw new Error(value);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user