Performance: Error handling in server actions
MEDIUM IMPACT
This affects the responsiveness and stability of server actions, impacting how quickly errors are detected and handled without blocking the UI.
export async function action(data) { try { const result = await fetch('/api/process', { method: 'POST', body: JSON.stringify(data) }); if (!result.ok) throw new Error('Failed request'); return await result.json(); } catch (error) { return { error: error.message }; } }
export async function action(data) { try { const result = await fetch('/api/process', { method: 'POST', body: JSON.stringify(data) }); if (!result.ok) throw new Error('Failed request'); return await result.json(); } catch (error) { throw error; // rethrow without handling } }
| Pattern | Server Blocking | UI Blocking | Error Feedback Speed | Verdict |
|---|---|---|---|---|
| Rethrow errors without handling | No blocking on server but unhandled promise rejection | Blocks UI update until error bubbles | Slow feedback, delayed error display | [X] Bad |
| Catch errors and return error object | No blocking | Immediate UI update with error | Fast feedback, smooth user experience | [OK] Good |
| Synchronous infinite loop on error | Blocks server event loop | Blocks all UI updates | No feedback, server freeze | [X] Bad |
| Log error and return controlled message | No blocking | Immediate UI update | Fast feedback, stable server | [OK] Good |