// Handles errors gracefully with clear messages and appropriate recovery
# Proper Error Handling
Handle errors gracefully and provide meaningful error messages.
## Provide Context
```javascript
// Bad - Vague error
throw new Error("Failed");
// Good - Specific context
throw new Error(`Failed to fetch user with ID ${userId}: User not found`);
```
## Use Try-Catch Appropriately
```javascript
// Bad - Silent failure
try {
await saveData(data);
} catch (error) {
// Nothing - error is lost
}
// Good - Handle or propagate
try {
await saveData(data);
} catch (error) {
logger.error('Failed to save data:', error);
throw new DataSaveError('Could not save user data', { cause: error });
}
```
## Create Custom Error Classes
```javascript
class ValidationError extends Error {
constructor(field, message) {
super(`Validation failed for ${field}: ${message}`);
this.name = 'ValidationError';
this.field = field;
}
}
class NotFoundError extends Error {
constructor(resource, id) {
super(`${resource} with ID ${id} not found`);
this.name = 'NotFoundError';
this.statusCode = 404;
}
}
```
## Fail Fast
```javascript
// Good - Validate early
function processUser(user) {
if (!user) {
throw new ValidationError('user', 'User is required');
}
if (!user.email) {
throw new ValidationError('email', 'Email is required');
}
// Process with confidence
sendWelcomeEmail(user.email);
}
```
## Don't Catch Everything
```javascript
// Bad - Too broad
try {
await complexOperation();
} catch (error) {
return null; // Hides all errors
}
// Good - Catch specific errors
try {
await complexOperation();
} catch (error) {
if (error instanceof NetworkError) {
return retryWithBackoff();
}
throw error; // Propagate unexpected errors
}
```
## User-Friendly Messages
```javascript
// Bad - Technical error for users
alert("Uncaught TypeError: Cannot read property 'name' of undefined");
// Good - User-friendly message
alert("We couldn't load your profile. Please refresh the page.");
// Log technical details separately for debugging
logger.error(error);
```6 matches