To view the package, see npmjs.com/package/error-style
Transform technical error messages into human-friendly explanations for better debugging.
Technical error messages are confusing:
β Cannot read properties of undefined
Clear, human-friendly explanations:
β Cannot read properties of undefined
**Reason:**
You tried to use something before it existed.
**Fix:**
Check if the value exists first using optional chaining (?.) or if statements.
**Suggestions:**
β’ Try: `value?.property` instead of `value.property`
β’ Add: `if (value) { /* your code */ }`
β’ Initialize the variable before using it
import { prettyTry } from 'error-style';
prettyTry(() => {
users.map(u => u.name)
});Instead of crashing, you get:
success: false(sounds much nicer, huh? π)- Clear explanation of what went wrong (finally! now you can fix your project's errors from 4 years ago!)
- Actionable fix suggestions (that are not from Webster's Dictionary. You can understand them π)
import { prettyTryAsync } from 'error-style';
const result = await prettyTryAsync(async () => {
const response = await fetch('/api/users');
return response.json();
});
if (!result.success) {
console.log(result.error.reason);
console.log(result.error.fix);
}import { formatError, logError } from 'error-style';
const result = prettyTry(() => riskyCode());
if (!result.success) {
// Pretty format
console.log(formatError(result.error));
// Or directly log
logError(result.error);
}prettyTry(() => {
const users = undefined;
return users.map(u => u.name);
});Output:
β Cannot read properties of undefined
**Reason:**
You tried to use something before it existed.
**Fix:**
Check if the value exists first using optional chaining (?.) or if statements.
await prettyTryAsync(async () => {
const response = await fetch('/api/data');
return response.json(); // API returns HTML error page
});Output:
β Unexpected token
**Reason:**
Failed to parse JSON - the response isn't valid JSON.
**Fix:**
The API probably returned HTML or an error message instead of JSON.
**Suggestions:**
β’ Check `response.status` before parsing
β’ Log the raw response: `console.log(await response.text())`
β’ Verify the API endpoint is correct
await prettyTryAsync(async () => {
const response = await fetch('https://wrong-url.com/api');
return response.json();
});Output:
β Failed to fetch
**Reason:**
Network request failed - can't reach the server.
**Fix:**
Check your internet connection and the API URL.
- Undefined/Null errors - Property access on undefined/null
- Array errors -
map is not a functionand similar - JSON errors - Parsing failures, unexpected tokens
- Network errors - Failed fetch, CORS issues
- Async/await errors - Using await outside async functions
- Module errors - Missing imports, wrong paths
- Fallback - Generic helpful messages for unknown errors
- Beginners learning JavaScript - Understand what errors actually mean
- React developers - Debug component issues faster
- API builders - Handle network and parsing errors gracefully
- Students - Learn programming without frustration
- Hobby developers - Build without getting stuck
- Pros debugging fast - Get instant clarity on common issues that you forget how to fix at 3 in the morning. Your welcome.
npm install error-styleprettyTry<T>(fn: () => T): PrettyTryResult<T>Wraps a synchronous function and provides friendly error messages.
Returns:
{
success: boolean;
data?: T;
error?: ErrorExplanation;
}prettyTryAsync<T>(fn: () => Promise<T>): Promise<PrettyTryResult<T>>Same as prettyTry but for async functions.
formatError(error: ErrorExplanation): stringFormats an error explanation into a readable string.
logError(error: ErrorExplanation): voidLogs a formatted error to console.error.
Found an error that needs a better explanation? Open an issue or submit a PR!
MIT - slammers001
