Always preserve error context by using specific error types, safe error handling patterns, and meaningful error messages. This helps with debugging and maintains a clear error handling chain.
Always preserve error context by using specific error types, safe error handling patterns, and meaningful error messages. This helps with debugging and maintains a clear error handling chain.
Key practices:
Example:
// โ Poor error handling
try {
const data = JSON.parse(response);
} catch (e) {
throw new Error("Failed to parse");
}
// โ
Good error handling
try {
const data = JSON.parse(response);
} catch (e) {
throw new ValidationError(
`Failed to parse response: ${e instanceof Error ? e.message : String(e)}.
Response: ${response.substring(0, 100)}...`
);
}
// โ
Good error handling with context
async function processItem(itemId: string) {
try {
const result = await backOff(
async () => await processWithRetries(itemId)
);
return result;
} catch (e) {
logger.error(
`Failed to process item ${itemId}: ${e instanceof Error ? e.message : String(e)}`
);
throw new ProcessingError(`Item processing failed: ${itemId}`, { cause: e });
}
}
Enter the URL of a public GitHub repository