javascript / expert
Snippet
Symbol-Tagged AggregateError Dispatching in React Error Catchers
This snippet utilizes custom Object-Oriented Error subclasses, global Symbols for private metadata tagging, and ES2021 `AggregateError` to aggregate multiple concurrent async failures. It presents a robust pattern for error taxonomy classification inside React error surfaces.
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
27
28
29
30
31
32
33
34
35
import React from 'react';const ERROR_TAXONOMY = Symbol.for('APP_ERROR_TAXONOMY');export class DomainError extends Error {constructor(message, code) {super(message);this.name = 'DomainError';this[ERROR_TAXONOMY] = code;}}export function processBatchErrors(errors) {const combined = new AggregateError(errors, 'Batch operation encountered multiple errors');combined[ERROR_TAXONOMY] = 'BATCH_FAILURE';return combined;}export function ErrorInspector({ error }) {if (!error) return null;const taxonomyCode = error[ERROR_TAXONOMY] || 'UNKNOWN_ERROR';const subErrors = error instanceof AggregateError ? error.errors : [error];return (<div role="alert" data-taxonomy={taxonomyCode}><h2>Error Classification: {String(taxonomyCode)}</h2><ul>{subErrors.map((err, idx) => (<li key={idx}>{err.name}: {err.message}</li>))}</ul></div>);}
react
Breakdown
1
const ERROR_TAXONOMY = Symbol.for('APP_ERROR_TAXONOMY');
Creates a unique, globally accessible Symbol key to attach non-enumerable diagnostic tags onto error instances.
2
export class DomainError extends Error {
Extends the built-in Error class to establish specialized domain exception boundaries with custom stack structures.
3
const combined = new AggregateError(errors, '...');
Instantiates ES2021 AggregateError to package multiple concurrent operation failures into a single object.
4
const subErrors = error instanceof AggregateError ? error.errors : [error];
Evaluates error types dynamically using instanceof to extract nested sub-error arrays.