Skip to content

https://www.npmjs.com/package/error-style - Transform ugly errors into human-friendly explanations with actionable fixes. Features beautiful formatting, clear explanations, and a CLI tool. Perfect for beginners trying to learn and pros who code at 3AM and forget how to fix a syntax error πŸ₯±πŸ’€

License

Notifications You must be signed in to change notification settings

slammers001/error-style

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

27 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Untitled design

ERROR-STYLE

To view the package, see npmjs.com/package/error-style

Transform technical error messages into human-friendly explanations for better debugging.

🎯 The Problem

Technical error messages are confusing:

❌ Cannot read properties of undefined

✨ The Solution

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

πŸš€ Usage

Basic Usage

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 πŸ™„)

Async Usage

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);
}

Formatting Errors

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);
}

πŸŽͺ Real Examples

Bad Array Usage

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.

JSON Parsing Error

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

Network Error

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.

🧩 Supported Errors

  • Undefined/Null errors - Property access on undefined/null
  • Array errors - map is not a function and 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

🎯 Who This Helps

  • 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.

πŸ“¦ Installation

npm install error-style

πŸ”§ API Reference

prettyTry<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): string

Formats an error explanation into a readable string.

logError(error: ErrorExplanation): void

Logs a formatted error to console.error.

🀝 Contributing

Found an error that needs a better explanation? Open an issue or submit a PR!

πŸ“„ License

MIT - slammers001

About

https://www.npmjs.com/package/error-style - Transform ugly errors into human-friendly explanations with actionable fixes. Features beautiful formatting, clear explanations, and a CLI tool. Perfect for beginners trying to learn and pros who code at 3AM and forget how to fix a syntax error πŸ₯±πŸ’€

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages