When catching and re-throwing errors, always preserve the original error context using the `cause` option of the Error constructor. This maintains the complete error chain for debugging and ensures no critical information is lost when translating errors between layers.
When catching and re-throwing errors, always preserve the original error context using the cause
option of the Error constructor. This maintains the complete error chain for debugging and ensures no critical information is lost when translating errors between layers.
Instead of:
try {
await deletePoints(filePaths)
} catch (error) {
throw new Error(`Failed to delete points: ${error.message}`)
}
Use:
try {
await deletePoints(filePaths)
} catch (error) {
throw new Error(
`Failed to delete points: ${error instanceof Error ? error.message : String(error)}`,
{ cause: error }
)
}
This practice:
When handling errors across system boundaries (e.g., API calls, file operations), this becomes especially important as it helps track down issues through multiple abstraction layers.
Enter the URL of a public GitHub repository