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
This commit is contained in:
@@ -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'
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user