⚠️ errors – Karma Error Management
The karma/errors package provides a structured way to manage and serve application error messages from a centralized JSON file. It helps teams maintain a clean, editable repository of error definitions—ideal for standardized error handling and localization.
🔧 Setup
Ensure your.env file includes the path to the error definition JSON file:
errors.json file with content like:
📦 Import
📘 Usage
✅ Load Errors from File
ERRORS_DEFINATION_FILE. If the file is missing or has bad syntax, the app will panic.
➕ Add a New Error (in memory)
WriteErrorsToFile() to persist it.
🔍 Get an Error by Code
models.ErrorMessage struct with all fields.
💾 Write Errors to File
🧪 Full Example
📌 Notes
- Errors are stored in a JSON array of objects using the
models.ErrorMessagestruct. - Useful for APIs where structured error responses with user-friendly messages are critical.
- Make sure the file path in
.envis correct and writable by the application.
🧠 Best Practices
- Use
AddError()for dynamic additions, such as admin-facing tools. - Always validate your
errors.jsonsyntax to avoid startup panics. - Use
UserMsgfor frontend display andErrorMsgfor internal logs.