Skip to main content

⚠️ 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:
Create an initial errors.json file with content like:

📦 Import


📘 Usage

✅ Load Errors from File

This reads all errors from the path specified in ERRORS_DEFINATION_FILE. If the file is missing or has bad syntax, the app will panic.

➕ Add a New Error (in memory)

Adds a new error to the in-memory error slice. You must call WriteErrorsToFile() to persist it.

🔍 Get an Error by Code

Returns a models.ErrorMessage struct with all fields.

💾 Write Errors to File

Overwrites the JSON file with the current in-memory list of errors.

🧪 Full Example


📌 Notes

  • Errors are stored in a JSON array of objects using the models.ErrorMessage struct.
  • Useful for APIs where structured error responses with user-friendly messages are critical.
  • Make sure the file path in .env is correct and writable by the application.

🧠 Best Practices

  • Use AddError() for dynamic additions, such as admin-facing tools.
  • Always validate your errors.json syntax to avoid startup panics.
  • Use UserMsg for frontend display and ErrorMsg for internal logs.