zen/core/zerr/wrap.go

60 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-08-22 17:26:53 +07:00
package zerr
import (
"context"
"time"
)
type WrapInput struct {
Errors []error
2024-08-22 17:26:53 +07:00
Message string
PC uintptr
Time time.Time
Logger Logger
2024-09-03 13:38:45 +07:00
Notifier NotificationHandler
Details []any
2024-08-22 17:26:53 +07:00
}
type Wrapper interface {
// Wrap creates a zerr.Error with inputs
// generated by global entry points.
Wrap(input WrapInput) Error
2024-08-22 17:26:53 +07:00
}
type Logger interface {
2024-08-23 10:18:27 +07:00
LogError(ctx context.Context, err Error)
2024-08-22 17:26:53 +07:00
}
type WrapperFunc func(input WrapInput) Error
2024-08-22 17:26:53 +07:00
func (wr WrapperFunc) Wrap(input WrapInput) Error {
2024-08-22 17:26:53 +07:00
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
2024-08-22 17:26:53 +07:00
})
func Wrap(input WrapInput) Error {
return DefaultWrapper.Wrap(input)
}
func SetDefaultWrapper(w Wrapper) {
DefaultWrapper = w
}