From 5716b232eb1e976417d1f8122b84629174ff173f Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 26 Apr 2024 13:19:11 +0100 Subject: [PATCH] Improve grid handling of API errors which don't have the normal JSON validation error shape --- .../src/components/grid/stores/rows.js | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/frontend-core/src/components/grid/stores/rows.js b/packages/frontend-core/src/components/grid/stores/rows.js index 0ac923fede..fc941ecc7f 100644 --- a/packages/frontend-core/src/components/grid/stores/rows.js +++ b/packages/frontend-core/src/components/grid/stores/rows.js @@ -196,6 +196,20 @@ export const createActions = context => { // Handles validation errors from the rows API and updates local validation // state, storing error messages against relevant cells const handleValidationError = (rowId, error) => { + // If the server doesn't reply with a valid error, assume that the source + // of the error is the focused cell's column + if (!error?.json?.validationErrors && error?.message) { + const focusedColumn = get(focusedCellId)?.split("-")[1] + if (focusedColumn) { + error = { + json: { + validationErrors: { + [focusedColumn]: error.message, + }, + }, + } + } + } if (error?.json?.validationErrors) { // Normal validation errors const keys = Object.keys(error.json.validationErrors) @@ -214,11 +228,19 @@ export const createActions = context => { // Process errors for columns that we have for (let column of erroredColumns) { + // Ensure we have a valid error to display + let err = error.json.validationErrors[column] + if (Array.isArray(err)) { + err = err[0] + } + if (typeof err !== "string" || !err.length) { + error = "Something went wrong" + } + // Set error against the cell validation.actions.setError( `${rowId}-${column}`, - `${column} ${error.json.validationErrors[column]}` + Helpers.capitalise(err) ) - // Ensure the column is visible const index = $columns.findIndex(x => x.name === column) if (index !== -1 && !$columns[index].visible) {