javascript / expert
Snippet
Strukturierte Fehlerfortpflanzung mit benutzerdefinierter Stack-Bereinigung
Node.js ermöglicht das Trimming benutzerdefinierter Stack-Traces über Error.captureStackTrace, um interne Konstruktorlogik in Fehlerdumps auszublenden. Zusammen mit cause-Verkettung und Kontext-Metadaten wird eine präzise Fehlerbehandlung und domänenspezifische Verzweigung erreicht.
snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class DomainControlError extends Error {constructor(message, contextData, causeError) {super(message, { cause: causeError });this.name = this.constructor.name;this.context = contextData;if (Error.captureStackTrace) {Error.captureStackTrace(this, DomainControlError);}}}function processWorkflow(payload) {try {if (!payload.id) throw new TypeError('Missing payload identifier');} catch (err) {throw new DomainControlError('Workflow execution halted', { step: 'VALIDATION' }, err);}}try {processWorkflow({});} catch (err) {if (err instanceof DomainControlError && err.context.step === 'VALIDATION') {console.error(`Handled: ${err.message}, Root Cause: ${err.cause.message}`);}}
nodejs
Erklärung
1
super(message, { cause: causeError });
Verkettet den zugrundeliegenden Fehler mittels nativen cause-Kontextes.
2
Error.captureStackTrace(this, DomainControlError);
Entfernt Konstruktoraufrufe aus dem Stack-Trace in V8-Runtimes.
3
if (err instanceof DomainControlError && err.context.step === 'VALIDATION') {
Führt domänenspezifische Steuerfluss-Verzweigungen basierend auf Fehler-Kontextdaten aus.