Documentation
¶
Overview ¶
Package reporter turns ordinary Go errors into structured reports that are suitable for local logs, production JSON logs, and Kafka-based alerting.
A report includes the caller file path, line number, function name, service, environment, timestamp, raw error, error type, and human-readable description. In production, reports can be published to Kafka so a separate worker can forward the alert to Telegram or another notification channel.
Initialize the package once during application startup:
reporter.Init(reporter.Config{
AppName: "payment-service",
AppEnv: "development",
})
defer reporter.Close()
Use AutoWrap when reporter should classify the error from its text:
if err := run(); err != nil {
return reporter.AutoWrap(err)
}
Use Wrap when the application can provide better business context:
if err := repository.SaveOrder(order); err != nil {
return reporter.Wrap(err, "Failed to save checkout order after payment was confirmed")
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AutoWrap ¶
AutoWrap converts an ordinary error into a structured report with automatic classification.
AutoWrap returns nil when err is nil. Otherwise it inspects err.Error() and assigns an error type and description for known patterns such as connection failures, duplicate keys, deadline timeouts, and missing data. The returned error captures the caller file path, line number, and function name. In production, the report is also published to Kafka when Init configured a writer.
Example:
if err := repository.FindUser(id); err != nil {
return reporter.AutoWrap(err)
}
func Close ¶
func Close()
Close releases the Kafka writer used by reporter.
Call Close during graceful shutdown after Init has been called. It is safe to call even when Kafka publishing is not enabled.
Example:
defer reporter.Close()
func Init ¶
func Init(cfg Config)
Init applies reporter configuration for the current service.
Call Init once during application startup before using AutoWrap or Wrap. It stores the service name, environment, and optional Kafka settings. When EnablePublishing is true and Kafka settings are complete, Init prepares a Kafka writer so every reported error can be published asynchronously.
Example:
reporter.Init(reporter.Config{
AppName: "payment-service",
AppEnv: "development",
})
defer reporter.Close()
func Wrap ¶
Wrap converts an ordinary error into a structured report with a custom description.
Wrap returns nil when err is nil. Use Wrap when the application can provide better business context than automatic classification. The original error is preserved in RawError, while customDesc is stored in Description. The returned error captures the caller file path, line number, and function name. In production, the report is also published to Kafka when Init configured a writer.
Example:
if err := repository.SaveOrder(order); err != nil {
return reporter.Wrap(err, "Failed to save checkout order after payment was confirmed")
}
Types ¶
type Config ¶ added in v1.0.4
type Config struct {
AppName string
AppEnv string
KafkaBrokers []string
KafkaTopic string
EnablePublishing bool
}
Config holds all the configuration parameters required to initialize the reporter. Passing this struct explicitly via function parameters provides better flexibility and decouples the package from direct environment variable access.
type CustomError ¶
type CustomError struct {
Timestamp string `json:"timestamp"`
Environment string `json:"environment"`
Service string `json:"service"`
ErrorType string `json:"error_type"`
Description string `json:"description"`
RawError string `json:"raw_error"`
File string `json:"file"`
Line int `json:"line"`
FunctionName string `json:"function"`
}
CustomError is the structured error payload produced by this package.
It contains the information needed for logs and alerts: timestamp, environment, service name, error type, human-readable description, raw error text, caller file path, caller line number, and caller function name. In production, this structure is serialized to JSON and can be published to Kafka for downstream alert delivery, such as Telegram notifications.
func (*CustomError) Error ¶
func (e *CustomError) Error() string
Error returns a formatted representation of the structured error.
In non-production environments it returns a colored terminal-friendly string containing the timestamp, file, line, error type, description, and raw error. In production it returns the JSON representation of CustomError, which is suitable for logs, Kafka messages, and alert consumers.