{"_id":"@amirkhodam/error-handler","name":"@amirkhodam/error-handler","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@amirkhodam/error-handler","version":"1.0.0","type":"module","description":"A robust error handling system with i18n support for frontend applications","main":"dist/index.js","module":"dist/index.esm.js","types":"dist/index.d.ts","exports":{".":{"import":"./dist/index.esm.js","require":"./dist/index.js","types":"./dist/index.d.ts"},"./vue":{"import":"./dist/vue.esm.js","require":"./dist/vue.js","types":"./dist/vue.d.ts"}},"scripts":{"build":"rollup -c","dev":"rollup -c -w","clean":"rimraf dist","prebuild":"npm run clean","type-check":"tsc --noEmit","lint":"eslint src --ext .ts,.tsx","test":"jest","prepublishOnly":"npm run build"},"keywords":["error-handling","i18n","internationalization","frontend","typescript","vue","react","toast","notifications"],"author":{"name":"Your Name"},"license":"MIT","devDependencies":{"@rollup/plugin-commonjs":"^25.0.0","@rollup/plugin-node-resolve":"^15.0.0","@rollup/plugin-terser":"^0.4.0","@rollup/plugin-typescript":"^11.0.0","@types/jest":"^29.0.0","@typescript-eslint/eslint-plugin":"^6.0.0","@typescript-eslint/parser":"^6.0.0","eslint":"^8.0.0","jest":"^29.0.0","rimraf":"^5.0.0","rollup":"^3.0.0","rollup-plugin-dts":"^5.0.0","ts-jest":"^29.0.0","tslib":"^2.8.1","typescript":"^5.0.0"},"peerDependencies":{"vue":"^3.0.0"},"peerDependenciesMeta":{"vue":{"optional":true}},"repository":{"type":"git","url":"git+https://github.com/amirkhodam/error-handler.git"},"bugs":{"url":"https://github.com/amirkhodam/error-handler/issues"},"homepage":"https://github.com/amirkhodam/error-handler#readme","_id":"@amirkhodam/error-handler@1.0.0","gitHead":"1c24a3df6d1ec27e8e10685b11527df7f22d9cc8","_nodeVersion":"22.18.0","_npmVersion":"10.9.3","dist":{"integrity":"sha512-QkGyNT4CyHjMyX7C8QlIitWJ7eZBD/Q9tO7BGCKdioFghw/+HneeykKELhqxd37NmliZ2rH0Pinr24EfIMs8zA==","shasum":"e95f43d849bab66f64ca124dbe40d0086ad5e849","tarball":"https://registry.npmjs.org/@amirkhodam/error-handler/-/error-handler-1.0.0.tgz","fileCount":29,"unpackedSize":52960,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCID9GL89BhOlzx8msDAnBr02u9i7LuBMdjzyQy9/+CmMPAiBCQvDZW07SWsYOMe968/PzzOPhPHbP9SefElFwxlbU1Q=="}]},"_npmUser":{"name":"amirkhodam","email":"emami.192@gmail.com"},"directories":{},"maintainers":[{"name":"amirkhodam","email":"emami.192@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/error-handler_1.0.0_1755029291573_0.2824264299479109"},"_hasShrinkwrap":false}},"time":{"created":"2025-08-12T20:08:11.451Z","1.0.0":"2025-08-12T20:08:11.751Z","modified":"2025-08-12T20:08:12.049Z"},"maintainers":[{"name":"amirkhodam","email":"emami.192@gmail.com"}],"description":"A robust error handling system with i18n support for frontend applications","homepage":"https://github.com/amirkhodam/error-handler#readme","keywords":["error-handling","i18n","internationalization","frontend","typescript","vue","react","toast","notifications"],"repository":{"type":"git","url":"git+https://github.com/amirkhodam/error-handler.git"},"author":{"name":"Your Name"},"bugs":{"url":"https://github.com/amirkhodam/error-handler/issues"},"license":"MIT","readme":"# Error Handler Package\r\n\r\nA robust error handling system with i18n support for frontend applications. This package provides centralized error handling, translation capabilities, and multiple error handling strategies.\r\n\r\n## Features\r\n\r\n- 🎯 **Centralized Error Handling**: Single service to manage all errors across your application\r\n- 🌍 **i18n Support**: Built-in error translation capabilities\r\n- 🔄 **Multiple Strategies**: Toast notifications, console logging, silent handling, and translation-only\r\n- 🎨 **Framework Agnostic**: Works with any frontend framework\r\n- 🎭 **Vue.js Integration**: Special Vue composition API support\r\n- 📦 **TypeScript**: Full TypeScript support with type definitions\r\n- 🚀 **Lightweight**: Minimal dependencies, tree-shakeable\r\n\r\n## Installation\r\n\r\n```bash\r\nnpm install @amirkhodam/error-handler\r\n# or\r\nyarn add @amirkhodam/error-handler\r\n```\r\n\r\n## Quick Start\r\n\r\n### Basic Usage\r\n\r\n```typescript\r\nimport { errorHandler } from '@amirkhodam/error-handler'\r\n\r\n// Handle an error with toast notification\r\nerrorHandler.handleError(error, {\r\n  strategy: 'toast',\r\n  severity: 'error',\r\n  summary: 'Error Title',\r\n  detail: 'Error details'\r\n})\r\n\r\n// Handle an error silently\r\nerrorHandler.handleError(error, {\r\n  strategy: 'silent'\r\n})\r\n\r\n// Log error to console\r\nerrorHandler.handleError(error, {\r\n  strategy: 'console'\r\n})\r\n```\r\n\r\n### Vue.js Usage\r\n\r\n```typescript\r\nimport { useErrorHandler } from '@amirkhodam/error-handler/vue'\r\n\r\nexport default {\r\n  setup() {\r\n    const { handleError, handleApiError, handleValidationError } = useErrorHandler()\r\n\r\n    const someFunction = async () => {\r\n      try {\r\n        // Your code here\r\n      } catch (error) {\r\n        handleApiError(error, {\r\n          summary: 'API Error',\r\n          severity: 'error'\r\n        })\r\n      }\r\n    }\r\n\r\n    return {\r\n      someFunction\r\n    }\r\n  }\r\n}\r\n```\r\n\r\n## Error Handling Strategies\r\n\r\n### 1. Toast Strategy\r\nDisplay errors as toast notifications to users.\r\n\r\n```typescript\r\nerrorHandler.handleError(error, {\r\n  strategy: 'toast',\r\n  severity: 'error', // 'error' | 'warn' | 'info' | 'success'\r\n  summary: 'Error Title',\r\n  detail: 'Error details',\r\n  life: 5000, // Duration in milliseconds\r\n  closable: true,\r\n  group: 'api-errors'\r\n})\r\n```\r\n\r\n### 2. Console Strategy\r\nLog errors to the browser console for debugging.\r\n\r\n```typescript\r\nerrorHandler.handleError(error, {\r\n  strategy: 'console'\r\n})\r\n```\r\n\r\n### 3. Silent Strategy\r\nHandle errors without any user feedback or logging.\r\n\r\n```typescript\r\nerrorHandler.handleError(error, {\r\n  strategy: 'silent'\r\n})\r\n```\r\n\r\n### 4. Translate Strategy\r\nReturn translated error messages for custom handling.\r\n\r\n```typescript\r\nconst translatedError = errorHandler.handleError(error, {\r\n  strategy: 'translate',\r\n  prefix: 'api',\r\n  default: 'An error occurred'\r\n})\r\n// Returns: { message, status, error, translate, errorObject }\r\n```\r\n\r\n## Vue Composition API\r\n\r\nThe package provides a Vue composition API hook with convenient methods:\r\n\r\n```typescript\r\nimport { useErrorHandler } from '@amirkhodam/error-handler/vue'\r\n\r\nconst {\r\n  handleError,           // General error handler\r\n  handleApiError,        // API-specific error handler\r\n  handleValidationError, // Validation error handler\r\n  handleSilentError,     // Silent error handler\r\n  handleDebugError,      // Console debug handler\r\n  handleTranslateError   // Translation-only handler\r\n} = useErrorHandler()\r\n```\r\n\r\n### Predefined Error Handlers\r\n\r\n#### API Error Handler\r\n```typescript\r\nhandleApiError(error, {\r\n  strategy: 'toast',\r\n  severity: 'error',\r\n  summary: 'Service Error',\r\n  life: 5000\r\n})\r\n```\r\n\r\n#### Validation Error Handler\r\n```typescript\r\nhandleValidationError(error, {\r\n  summary: 'Please check your input'\r\n})\r\n```\r\n\r\n## Error Translation\r\n\r\nThe package includes built-in error translation capabilities:\r\n\r\n```typescript\r\nimport { translator } from '@amirkhodam/error-handler'\r\n\r\nconst translatedError = translator.translateError(error, {\r\n  prefix: 'api',\r\n  default: 'An error occurred'\r\n})\r\n```\r\n\r\n## Event Bus Integration\r\n\r\nThe package includes a simple event bus for toast notifications:\r\n\r\n```typescript\r\nimport { useEventBus } from '@amirkhodam/error-handler'\r\n\r\nconst eventBus = useEventBus()\r\n\r\n// Listen for toast events\r\neventBus.on('toast.add', (toastData) => {\r\n  // Handle toast notification\r\n  console.log('Toast:', toastData)\r\n})\r\n```\r\n\r\n## TypeScript Support\r\n\r\nFull TypeScript support with comprehensive type definitions:\r\n\r\n```typescript\r\nimport type { \r\n  ErrorHandlingOptions, \r\n  IErrorTranslate, \r\n  ErrorHandlingStrategy \r\n} from '@amirkhodam/error-handler'\r\n\r\nconst options: ErrorHandlingOptions = {\r\n  strategy: 'toast',\r\n  severity: 'error',\r\n  summary: 'Error'\r\n}\r\n```\r\n\r\n## Configuration\r\n\r\n### Custom Event Bus\r\n\r\nYou can provide your own event bus implementation:\r\n\r\n```typescript\r\nimport { EventBus } from '@amirkhodam/error-handler'\r\n\r\n// Create custom event bus\r\nconst customEventBus = new EventBus()\r\n\r\n// Use in your application\r\ncustomEventBus.emit('toast.add', toastData)\r\n```\r\n\r\n### Custom Error Translation\r\n\r\nExtend the translation service for your needs:\r\n\r\n```typescript\r\nimport { ErrorTranslator } from '@amirkhodam/error-handler'\r\n\r\nclass CustomTranslator extends ErrorTranslator {\r\n  translateError(error: any, options: ErrorHandlingOptions): IErrorTranslate {\r\n    // Custom translation logic\r\n    return super.translateError(error, options)\r\n  }\r\n}\r\n```\r\n\r\n## API Reference\r\n\r\n### ErrorHandlingService\r\n\r\n#### `handleError<T>(error: any, options: ErrorHandlingOptions & { strategy: T }): ErrorTranslateReturn<T>`\r\n\r\nMain method to handle errors with specified strategy.\r\n\r\n### useErrorHandler (Vue)\r\n\r\n#### `handleError(error: any, options: ErrorHandlingOptions)`\r\nGeneral error handler.\r\n\r\n#### `handleApiError(error: any, options?: Partial<ErrorHandlingOptions>)`\r\nAPI-specific error handler with default settings.\r\n\r\n#### `handleValidationError(error: any, options?: Partial<ErrorHandlingOptions>)`\r\nValidation error handler with warning severity.\r\n\r\n#### `handleSilentError(error: any)`\r\nSilent error handler.\r\n\r\n#### `handleDebugError(error: any)`\r\nConsole debug error handler.\r\n\r\n#### `handleTranslateError(error: any, options?: Partial<ErrorHandlingOptions>)`\r\nTranslation-only error handler.\r\n\r\n## Error Handling Options\r\n\r\n```typescript\r\ninterface ErrorHandlingOptions {\r\n  strategy: 'toast' | 'console' | 'silent' | 'translate'\r\n  severity?: 'error' | 'warn' | 'info' | 'success'\r\n  summary?: string\r\n  detail?: string\r\n  life?: number\r\n  closable?: boolean\r\n  group?: string\r\n  prefix?: string\r\n  default?: string\r\n  inline?: boolean\r\n}\r\n```\r\n\r\n## Error Response Structure\r\n\r\n```typescript\r\ninterface IErrorTranslate {\r\n  message: string\r\n  status: number\r\n  error: string\r\n  translate: string\r\n  errorObject: any\r\n}\r\n```\r\n\r\n## Development\r\n\r\n### Building\r\n\r\n```bash\r\nnpm run build\r\n```\r\n\r\n### Development Mode\r\n\r\n```bash\r\nnpm run dev\r\n```\r\n\r\n### Testing\r\n\r\n```bash\r\nnpm test\r\n```\r\n\r\n### Linting\r\n\r\n```bash\r\nnpm run lint\r\n```\r\n\r\n## Contributing\r\n\r\n1. Fork the repository\r\n2. Create a feature branch\r\n3. Make your changes\r\n4. Add tests\r\n5. Submit a pull request\r\n\r\n## License\r\n\r\nMIT License - see LICENSE file for details.\r\n\r\n## Support\r\n\r\nFor support and questions, please open an issue on GitHub.\r\n","readmeFilename":"README.md","_rev":"1-7e009ba2e61d131fd5cb10ecf673bb2f"}