41 lines
884 B
Go
41 lines
884 B
Go
|
package errors
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"github.com/google/uuid"
|
||
|
log "github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
type internalError struct {
|
||
|
data interface{}
|
||
|
}
|
||
|
|
||
|
func (e *internalError) Error() string {
|
||
|
data, err := json.Marshal(&e.data)
|
||
|
if err != nil {
|
||
|
return err.Error()
|
||
|
}
|
||
|
|
||
|
return string(data)
|
||
|
}
|
||
|
|
||
|
func newError(msg, cor string, code int) error {
|
||
|
return &internalError{data: map[string]interface{}{
|
||
|
"correlation": cor,
|
||
|
"message": msg,
|
||
|
"code": code,
|
||
|
}}
|
||
|
}
|
||
|
|
||
|
func WithCorrelationLog(message string, err error, code int) error {
|
||
|
id := uuid.New().String()
|
||
|
log.WithField("correlation", id).WithField("error", err.Error()).Error(message)
|
||
|
return newError(fmt.Sprintf("%s: %s", id, message), id, code)
|
||
|
}
|
||
|
|
||
|
func WithCorrelation(message string, code int) (string, error) {
|
||
|
id := uuid.New().String()
|
||
|
return id, newError(fmt.Sprintf("%s: %s", id, message), id, code)
|
||
|
}
|