Error handling
Errors come in two kinds. An invalid call throws a GridError. An exception thrown by your own callback is swallowed by the grid and reported through the error event.
The error class is documented in the API reference as GridError.
How invalid input is rejected
Wrong shapes in options, columns, rows or models throw invalid-option (never a bare TypeError) and leave the grid state unchanged.
- Unknown keys: a typo such as
rowHeigthis reported with the closest known key inerror.hintand the message (Did you mean "rowHeight"?). Applies to options, column definitions,messages, filter/sort items and transactions. SavedcolumnStateignores unknown properties so newer saves still load. - Types and ranges:
rowHeight: 0,width: -1,align: 'left'report the path (columns[1].width) and the received value. - Rows: a non-array
rowsornull/primitive rows are rejected.addRowsworks with very large inputs (200k rows). - Prototype pollution guard:
__proto__,constructorandprototypecannot be used as a columnid/key,rowKey, or as a column id in sort, filter or column state. Read such a data property withgetValue. - Circular references: model and value comparison terminates on circular filter or cell values.
- Language: messages follow
locale(ko/en); see Language of error messages. Branch oncode.
Branch on GridError codes
message is a sentence in the grid's locale (ko or en) and may change. Branch on code.
import { GridError } from '@maxgrid/core';
try {
api.setOptions({ rows });
} catch (error) {
if (error instanceof GridError && error.code === 'duplicate-row-key') {
showToast('Two rows share the same employee ID.');
return;
}
throw error;
}| code | When it is thrown |
|---|---|
duplicate-row-key | Two rows share a row key |
missing-row-key | A row key is undefined, null or an object |
duplicate-column-id | Two columns share an id |
invalid-option | A column id is empty, a sort or filter model is invalid, and similar |
destroyed | You call an API that changes data or options after destroy() |
not-implemented | Reserved value. No method throws this error today |
State is unchanged when an error is thrown. setOptions, setSortModel and setFilterModel keep the existing value if validation fails, and transact rolls everything back if one step fails.
How is HTML handled?
The grid puts cell values into the DOM as text (textContent) only. A value containing <script> or <img onerror=...> is shown as text and never runs. Turn on cellRenderer: 'html' explicitly only for columns that need markup. That renderer draws only what passed an allowlist sanitizer and removes tags, attributes and addresses outside the list. The rules are in HTML in the built-in renderers.
Language of error messages
The message of a GridError, the message of an error event, console notes and the text of an error cell all follow the grid's locale ('ko' by default, or 'en'). Every message lives in one table as a Korean and English pair, so one language cannot be left out.
| Where | locale: 'ko' | locale: 'en' |
|---|---|---|
| Thrown error | 존재하지 않는 컬럼입니다: "zz" | Column does not exist: "zz" |
error event | 컬럼 "qty"의 집계에서 … 건너뛰었습니다. | Aggregation of column "qty" skipped … |
| Error cell | ⚠ 오류 | ⚠ Error |
| Console note | [grid] … 오류로 표시합니다. | [grid] … shown as an error. |
- Changing
localewithsetOptions({ locale })applies to the errors that happen afterwards. - Exports are called separately from the grid, so pass the option yourself:
exportCsv(api, { locale: 'en' }). Without it the text is Korean. - Only developer diagnostics stay Korean whatever the locale: internal state checks in development and test mode (
invariant-violation),module-errorraised while the grid assembles its own features, and exceptions for misusing a wrapper (a cell helper used outside<mx-grid>, for example). These are not errors your input causes in production. messagemay change. Branch oncode.
Receive the error event
If getValue, formatValue, cellClass or rowClass throws, the grid keeps rendering. You cannot catch these errors with try/catch, so listen for the error event.
api.on('error', ({ code, message, cause, where }) => {
// where, for example: 'getValue(salary)'
reportToMyLogger({ code, message, where, cause });
});| code | Delivered when |
|---|---|
callback-error | One of your callbacks threw |
reentrancy-limit | Handlers keep calling the API and the event chain passes 16 levels |
module-error | Cleaning up an internal part of the grid failed. Your input did not cause it |
invariant-violation | An internal state check failed in development or test mode. Not in production bundles |
- A cell whose
getValuethrew is treated as empty: it sorts last, and filters apply the empty-value rules. - A cell whose
formatValueor similar threw shows⚠ 오류(⚠ Errorwithlocale: 'en'). Other cells and rows are fine. - A callback that throws on each of 100,000 rows does not flood you with events. Within one computation, the same
whereis reported once, andmessageincludes the count.causeis the first value thrown. causeis not always anError.- If you see
reentrancy-limit, look for a handler that keeps setting the same model again.
An exception thrown by an event handler does not block other handlers. It is logged with console.error.
api.on('rowClicked', () => {
throw new Error('my handler failed');
});
api.on('rowClicked', ({ rowKey }) => console.log(rowKey)); // still calledClean up a grid
Call destroy() when you no longer need the grid.
const api = createGrid(container, options);
api.destroy();
api.isDestroyed(); // truedestroyedfires and every listener added withonis removed. The DOM and any scheduled render are cleaned up.- A second call is ignored.
- After that, mutating methods such as
setRowsandsetOptionsthrowGridError('destroyed'). - The React, Vue and Angular wrappers call it automatically when the component goes away.
- Timers and external subscriptions your app created are yours to clean up.
Several grids on one page
Create each grid in its own container. Grids do not share state or events, so changing one leaves the others alone. Import the theme CSS once.
<div style={{ height: 240 }}>
<Grid options={ordersOptions} />
</div>
<div style={{ height: 240 }}>
<Grid options={customersOptions} />
</div>Do not call createGrid twice on the same container. Keep one api per grid.