From e7d6c67c3915eaf419c0663cb9c8bc4039103c02 Mon Sep 17 00:00:00 2001 From: Michael Skrynski Date: Sat, 17 Jan 2026 21:29:10 +0100 Subject: [PATCH] fix: show specific validation error messages instead of generic 'Invalid input data' - Backend now returns first validation error message in 'message' field - Both POST and PUT commander endpoints updated - Frontend fallback to handle either message or details array format - Users now see 'Select at least one color' instead of generic error - Improved error response consistency across all validation errors --- backend/src/routes/commanders.js | 64 +++++++++++++++++--------------- frontend/public/js/commanders.js | 17 +++++++-- 2 files changed, 47 insertions(+), 34 deletions(-) diff --git a/backend/src/routes/commanders.js b/backend/src/routes/commanders.js index dc5e080..7065f46 100644 --- a/backend/src/routes/commanders.js +++ b/backend/src/routes/commanders.js @@ -237,21 +237,23 @@ export default async function commanderRoutes(fastify, options) { message: 'Commander created successfully', commander: transformCommander(commander) }) - } catch (error) { - if (error instanceof z.ZodError) { - return reply.code(400).send({ - error: 'Validation Error', - message: 'Invalid input data', - details: formatValidationErrors(error) - }) - } else { - fastify.log.error('Create commander error:', error) - reply.code(500).send({ - error: 'Internal Server Error', - message: 'Failed to create commander' - }) - } - } + } catch (error) { + if (error instanceof z.ZodError) { + const formattedErrors = formatValidationErrors(error) + const firstError = formattedErrors[0]?.message || 'Invalid input data' + return reply.code(400).send({ + error: 'Validation Error', + message: firstError, + details: formattedErrors + }) + } else { + fastify.log.error('Create commander error:', error) + reply.code(500).send({ + error: 'Internal Server Error', + message: 'Failed to create commander' + }) + } + } } ) @@ -301,21 +303,23 @@ export default async function commanderRoutes(fastify, options) { message: 'Commander updated successfully', commander: transformCommander(commander) }) - } catch (error) { - if (error instanceof z.ZodError) { - reply.code(400).send({ - error: 'Validation Error', - message: 'Invalid input data', - details: error.errors.map((e) => e.message) - }) - } else { - fastify.log.error('Update commander error:', error.message || error) - reply.code(500).send({ - error: 'Internal Server Error', - message: 'Failed to update commander' - }) - } - } + } catch (error) { + if (error instanceof z.ZodError) { + const formattedErrors = formatValidationErrors(error) + const firstError = formattedErrors[0]?.message || 'Invalid input data' + reply.code(400).send({ + error: 'Validation Error', + message: firstError, + details: formattedErrors + }) + } else { + fastify.log.error('Update commander error:', error.message || error) + reply.code(500).send({ + error: 'Internal Server Error', + message: 'Failed to update commander' + }) + } + } } ) diff --git a/frontend/public/js/commanders.js b/frontend/public/js/commanders.js index 5246aa0..1da939c 100644 --- a/frontend/public/js/commanders.js +++ b/frontend/public/js/commanders.js @@ -153,10 +153,19 @@ function commanderManager() { const data = await response.json() this.commanders.unshift(data.commander) this.resetAddForm() - } else { - const errorData = await response.json() - this.serverError = errorData.message || 'Failed to create commander' - } + } else { + const errorData = await response.json() + // Use message if available, otherwise extract from details array + if (errorData.message) { + this.serverError = errorData.message + } else if (errorData.details && Array.isArray(errorData.details)) { + this.serverError = errorData.details + .map((err) => err.message || err) + .join(', ') + } else { + this.serverError = 'Failed to create commander' + } + } } catch (error) { console.error('Add commander error:', error) this.serverError = 'Network error occurred'