Skip to content

Commit 6956699

Browse files
authored
fix(cdk/table): show a real error message instead of a crash when a table column is missing (#33698)
Before: if a <cdk-table> row used a column that wasn't defined, the app would crash in production with a confusing error like "Cannot read properties of undefined (reading 'headerCell')" — no mention of which column was the problem. This message only showed up in dev mode; in production you just got a raw, useless crash. Now: the table always throws a clear error like `Could not find column with id "column_a".`, in both dev and production, so it's obvious what went wrong and which column to fix. Added a test that forces production mode and checks the table gives the helpful error instead of crashing.
1 parent 4358fb0 commit 6956699

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

src/cdk/table/table.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,10 +1287,10 @@ export class CdkTable<T>
12871287
private _addStickyColumnStyles(rows: HTMLElement[], rowDef: BaseRowDef) {
12881288
const columnDefs = Array.from(rowDef?.columns || []).map(columnName => {
12891289
const columnDef = this._columnDefsByName.get(columnName);
1290-
if (!columnDef && (typeof ngDevMode === 'undefined' || ngDevMode)) {
1290+
if (!columnDef) {
12911291
throw getTableUnknownColumnError(columnName);
12921292
}
1293-
return columnDef!;
1293+
return columnDef;
12941294
});
12951295
const stickyStartStates = columnDefs.map(columnDef => columnDef.sticky);
12961296
const stickyEndStates = columnDefs.map(columnDef => columnDef.stickyEnd);
@@ -1415,11 +1415,11 @@ export class CdkTable<T>
14151415
return Array.from(rowDef.columns, columnId => {
14161416
const column = this._columnDefsByName.get(columnId);
14171417

1418-
if (!column && (typeof ngDevMode === 'undefined' || ngDevMode)) {
1418+
if (!column) {
14191419
throw getTableUnknownColumnError(columnId);
14201420
}
14211421

1422-
return rowDef.extractCellTemplate(column!);
1422+
return rowDef.extractCellTemplate(column);
14231423
});
14241424
}
14251425

0 commit comments

Comments
 (0)