A robust error handling system with i18n support for frontend applications. This package provides centralized error handling, translation capabilities, and multiple error handling strategies.
- 🎯 Centralized Error Handling: Single service to manage all errors across your application
- 🌍 i18n Support: Built-in error translation capabilities
- 🔄 Multiple Strategies: Toast notifications, console logging, silent handling, and translation-only
- 🎨 Framework Agnostic: Works with any frontend framework
- 🎠Vue.js Integration: Special Vue composition API support
- 📦 TypeScript: Full TypeScript support with type definitions
- 🚀 Lightweight: Minimal dependencies, tree-shakeable
npm install @amirkhodam/error-handler
# or
yarn add @amirkhodam/error-handlerimport { errorHandler } from '@amirkhodam/error-handler'
// Handle an error with toast notification
errorHandler.handleError(error, {
strategy: 'toast',
severity: 'error',
summary: 'Error Title',
detail: 'Error details'
})
// Handle an error silently
errorHandler.handleError(error, {
strategy: 'silent'
})
// Log error to console
errorHandler.handleError(error, {
strategy: 'console'
})import { useErrorHandler } from '@amirkhodam/error-handler/vue'
export default {
setup() {
const { handleError, handleApiError, handleValidationError } = useErrorHandler()
const someFunction = async () => {
try {
// Your code here
} catch (error) {
handleApiError(error, {
summary: 'API Error',
severity: 'error'
})
}
}
return {
someFunction
}
}
}Display errors as toast notifications to users.
errorHandler.handleError(error, {
strategy: 'toast',
severity: 'error', // 'error' | 'warn' | 'info' | 'success'
summary: 'Error Title',
detail: 'Error details',
life: 5000, // Duration in milliseconds
closable: true,
group: 'api-errors'
})Log errors to the browser console for debugging.
errorHandler.handleError(error, {
strategy: 'console'
})Handle errors without any user feedback or logging.
errorHandler.handleError(error, {
strategy: 'silent'
})Return translated error messages for custom handling.
const translatedError = errorHandler.handleError(error, {
strategy: 'translate',
prefix: 'api',
default: 'An error occurred'
})
// Returns: { message, status, error, translate, errorObject }The package provides a Vue composition API hook with convenient methods:
import { useErrorHandler } from '@amirkhodam/error-handler/vue'
const {
handleError, // General error handler
handleApiError, // API-specific error handler
handleValidationError, // Validation error handler
handleSilentError, // Silent error handler
handleDebugError, // Console debug handler
handleTranslateError // Translation-only handler
} = useErrorHandler()handleApiError(error, {
strategy: 'toast',
severity: 'error',
summary: 'Service Error',
life: 5000
})handleValidationError(error, {
summary: 'Please check your input'
})The package includes built-in error translation capabilities:
import { translator } from '@amirkhodam/error-handler'
const translatedError = translator.translateError(error, {
prefix: 'api',
default: 'An error occurred'
})The package includes a simple event bus for toast notifications:
import { useEventBus } from '@amirkhodam/error-handler'
const eventBus = useEventBus()
// Listen for toast events
eventBus.on('toast.add', (toastData) => {
// Handle toast notification
console.log('Toast:', toastData)
})Full TypeScript support with comprehensive type definitions:
import type {
ErrorHandlingOptions,
IErrorTranslate,
ErrorHandlingStrategy
} from '@amirkhodam/error-handler'
const options: ErrorHandlingOptions = {
strategy: 'toast',
severity: 'error',
summary: 'Error'
}You can provide your own event bus implementation:
import { EventBus } from '@amirkhodam/error-handler'
// Create custom event bus
const customEventBus = new EventBus()
// Use in your application
customEventBus.emit('toast.add', toastData)Extend the translation service for your needs:
import { ErrorTranslator } from '@amirkhodam/error-handler'
class CustomTranslator extends ErrorTranslator {
translateError(error: any, options: ErrorHandlingOptions): IErrorTranslate {
// Custom translation logic
return super.translateError(error, options)
}
}handleError<T>(error: any, options: ErrorHandlingOptions & { strategy: T }): ErrorTranslateReturn<T>
Main method to handle errors with specified strategy.
General error handler.
API-specific error handler with default settings.
Validation error handler with warning severity.
Silent error handler.
Console debug error handler.
Translation-only error handler.
interface ErrorHandlingOptions {
strategy: 'toast' | 'console' | 'silent' | 'translate'
severity?: 'error' | 'warn' | 'info' | 'success'
summary?: string
detail?: string
life?: number
closable?: boolean
group?: string
prefix?: string
default?: string
inline?: boolean
}interface IErrorTranslate {
message: string
status: number
error: string
translate: string
errorObject: any
}npm run buildnpm run devnpm testnpm run lint- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
MIT License - see LICENSE file for details.
For support and questions, please open an issue on GitHub.