zen/core/zerr/wrap.go

66 lines
1.2 KiB
Go

package zerr
import (
"context"
"time"
"gitlab.bareksa.com/backend/zen/core/zoptions"
)
type WrapInput struct {
Errors []error
Message string
PC uintptr
Time time.Time
Logger Logger
Notifier Notifier
Details []any
}
type Wrapper interface {
// Wrap creates a zerr.Error with inputs
// generated by global entry points.
Wrap(input WrapInput) Error
}
type Logger interface {
LogError(ctx context.Context, err Error)
}
type Notifier interface {
NotifyError(ctx context.Context, err Error, opts ...zoptions.NotifyOption)
}
type WrapperFunc func(input WrapInput) Error
func (wr WrapperFunc) Wrap(input WrapInput) Error {
return wr(input)
}
var DefaultWrapper Wrapper = WrapperFunc(func(input WrapInput) Error {
e := &Err{
message: input.Message,
errs: input.Errors,
caller: input.PC,
time: input.Time,
logger: input.Logger,
notifier: input.Notifier,
details: input.Details,
}
e.sequence = &sequence{err: e}
for _, err := range input.Errors {
if err, ok := err.(Error); ok {
err.Sequence().WrapBy(e.sequence)
}
}
return e
})
func Wrap(input WrapInput) Error {
return DefaultWrapper.Wrap(input)
}
func SetDefaultWrapper(w Wrapper) {
DefaultWrapper = w
}