Server Actions có thể throw errors sẽ được catch bởi nearest error.tsx.
- Với useFormState/useActionState, return error object thay vì throw:
return { error: 'Invalid input' }. - Dùng try/catch trong action để handle expected errors (validation, not found) và return user-friendly error states.
Server Actions can throw errors, which will be caught by the nearest error.tsx boundary.
- When using useFormState/useActionState, return an error object instead of throwing:
return { error: 'Invalid input' }. - Use try/catch inside the action to handle expected errors (validation failures, not-found cases) and return user-friendly error state objects.