Standardizing Error Messages for Improved Debugging
Learn to create uniform error messages that provide clarity during debugging, enhancing developer efficiency.
0 likes
183 views
Rule Content
---
description: Enforce standardized error messages in JavaScript to enhance debugging clarity and developer efficiency.
globs: "**/*.js"
tags: [error-handling, debugging, code-quality]
priority: 2
version: 1.0.0
---
# Standardizing Error Messages for Improved Debugging
## Context
- Applicable to all JavaScript projects.
- Aims to improve error message consistency and informativeness.
## Requirements
- **Use Specific Error Types**: Create and throw custom error classes that extend the native `Error` object to provide more context.
- **Provide Descriptive Error Messages**: Include clear, actionable information in error messages to aid in debugging.
- **Avoid Technical Jargon**: Use plain language in error messages to ensure they are understandable to all developers.
- **Include Relevant Context**: Add pertinent details such as function names, variable values, or operation descriptions to error messages.
- **Implement Consistent Formatting**: Adopt a uniform structure for error messages across the codebase.
## Examples
<example>
// Good: Custom error with descriptive message
class ValidationError extends Error {
constructor(field, message) {
super(`Validation failed for field '${field}': ${message}`);
this.name = 'ValidationError';
this.field = field;
}
}
function processUserInput(input) {
if (!input.email) {
throw new ValidationError('email', 'Email address is required.');
}
// Process input...
}
</example>
<example type="invalid">
// Bad: Generic error with vague message
function processUserInput(input) {
if (!input.email) {
throw new Error('Invalid input.');
}
// Process input...
}
</example>