Handle errors with explicit type checking and intentional decisions rather than generic catch-all approaches. This improves error handling precision and makes error recovery logic clearer.
Key practices:
Example of explicit error type checking:
// Good: Explicit check for specific error type
if (e instanceof DOMException && e.name === 'AbortError') {
// Handle as normal control flow
return handleAbortError()
}
// Good: Specific error detection
function isRecoverableError(error: unknown): boolean {
if (error instanceof Error) {
return error.message.startsWith(`${RECOVERABLE_ERROR}: `)
}
return false
}
// Good: Explicit about silent error handling with documentation
try {
const context = defaultTransformer.parse(serializedContext)
return { context, data: formData }
} catch (e) {
// Intentionally return fallback when context parsing fails
// to allow request processing to continue
return { data: formData }
}
This approach prevents unexpected error swallowing, makes error handling intentions clear to other developers, and enables more precise error recovery strategies.
Enter the URL of a public GitHub repository