fix: display specific validation errors when publish fails with 422
The handlePublish catch block was showing a generic "Failed to publish" message instead of parsing the 422 response body for validation details. Now matches the handleSaveDraft error handling pattern. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -349,8 +349,23 @@ export function TreeEditorPage() {
|
||||
toast.success('Tree published successfully')
|
||||
navigate(`/trees/${newTree.id}/edit`, { replace: true })
|
||||
}
|
||||
} catch (err) {
|
||||
} catch (err: unknown) {
|
||||
console.error('Failed to publish tree:', err)
|
||||
if (err && typeof err === 'object' && 'response' in err) {
|
||||
const axiosErr = err as { response?: { status?: number; data?: { detail?: string | { message?: string; errors?: Array<string | { message?: string }> } } } }
|
||||
if (axiosErr.response?.status === 422) {
|
||||
const detail = axiosErr.response.data?.detail
|
||||
if (typeof detail === 'object' && detail?.errors) {
|
||||
const messages = detail.errors.map(e => typeof e === 'string' ? e : e.message || 'Unknown error')
|
||||
toast.error(`Cannot publish: ${messages.join(', ')}`)
|
||||
} else if (typeof detail === 'string') {
|
||||
toast.error(`Cannot publish: ${detail}`)
|
||||
} else {
|
||||
toast.error('Tree has validation errors. Fix them before publishing.')
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
toast.error('Failed to publish tree. Please try again.')
|
||||
} finally {
|
||||
setSaving(false)
|
||||
|
||||
Reference in New Issue
Block a user