handle macro errors

This commit is contained in:
Jan Prochazka
2020-11-01 10:47:13 +01:00
parent 4efa87c3c8
commit cb0aee6476
5 changed files with 48 additions and 5 deletions

View File

@@ -25,9 +25,16 @@ export function runMacro(
macroArgs: {}, macroArgs: {},
data: FreeTableModel, data: FreeTableModel,
preview: boolean, preview: boolean,
selectedCells: MacroSelectedCell[] selectedCells: MacroSelectedCell[],
errors: string[] = []
): FreeTableModel { ): FreeTableModel {
const func = eval(getMacroFunction[macro.type](macro.code)); let func;
try {
func = eval(getMacroFunction[macro.type](macro.code));
} catch (err) {
errors.push(`Error compiling macro ${macro.name}: ${err.message}`);
return data;
}
if (macro.type == 'transformValue') { if (macro.type == 'transformValue') {
const selectedRows = _.groupBy(selectedCells, 'row'); const selectedRows = _.groupBy(selectedCells, 'row');
const rows = data.rows.map((row, rowIndex) => { const rows = data.rows.map((row, rowIndex) => {
@@ -38,7 +45,12 @@ export function runMacro(
for (const cell of selectedRow) { for (const cell of selectedRow) {
const { column } = cell; const { column } = cell;
const oldValue = row[column]; const oldValue = row[column];
const newValue = func(oldValue, macroArgs, modules, rowIndex, row, column); let newValue = oldValue;
try {
newValue = func(oldValue, macroArgs, modules, rowIndex, row, column);
} catch (err) {
errors.push(`Error processing column ${column} on row ${rowIndex}: ${err.message}`);
}
if (newValue != oldValue) { if (newValue != oldValue) {
if (res == null) { if (res == null) {
res = { ...row }; res = { ...row };

View File

@@ -298,6 +298,16 @@ export default function DataGridCore(props) {
return <ErrorInfo message={errorMessage} />; return <ErrorInfo message={errorMessage} />;
} }
if (grider.errors && grider.errors.length > 0) {
return (
<div>
{grider.errors.map((err, index) => (
<ErrorInfo message={err} key={index} isSmall />
))}
</div>
);
}
const handleRowScroll = (value) => { const handleRowScroll = (value) => {
setFirstVisibleRowScrollIndex(value); setFirstVisibleRowScrollIndex(value);
}; };

View File

@@ -50,6 +50,9 @@ export default abstract class Grider {
get disableLoadNextPage() { get disableLoadNextPage() {
return false; return false;
} }
get errors() {
return null;
}
updateRow(index, changeObject) { updateRow(index, changeObject) {
for (const key of Object.keys(changeObject)) { for (const key of Object.keys(changeObject)) {
this.setCellValue(index, key, changeObject[key]); this.setCellValue(index, key, changeObject[key]);

View File

@@ -3,9 +3,14 @@ import Grider, { GriderRowStatus } from '../datagrid/Grider';
export default class MacroPreviewGrider extends Grider { export default class MacroPreviewGrider extends Grider {
model: FreeTableModel; model: FreeTableModel;
_errors: string[] = [];
constructor(model: FreeTableModel, macro: MacroDefinition, macroArgs: {}, selectedCells: MacroSelectedCell[]) { constructor(model: FreeTableModel, macro: MacroDefinition, macroArgs: {}, selectedCells: MacroSelectedCell[]) {
super(); super();
this.model = runMacro(macro, macroArgs, model, true, selectedCells); this.model = runMacro(macro, macroArgs, model, true, selectedCells, this._errors);
}
get errors() {
return this._errors;
} }
getRowStatus(index): GriderRowStatus { getRowStatus(index): GriderRowStatus {

View File

@@ -14,7 +14,20 @@ const Icon = styled.div`
margin: 10px; margin: 10px;
`; `;
export default function ErrorInfo({ message, icon = 'fas fa-times-circle red' }) { const ContainerSmall = styled.div`
display: flex;
margin-right: 10px;
`;
export default function ErrorInfo({ message, icon = 'fas fa-times-circle red', isSmall = false }) {
if (isSmall) {
return (
<ContainerSmall>
<FontIcon icon={icon} />
{message}
</ContainerSmall>
);
}
return ( return (
<Container> <Container>
<Icon> <Icon>