zen/core/zerr/wrap.go

66 lines
1.2 KiB
Go
Raw Normal View History

2024-08-22 17:26:53 +07:00
package zerr
import (
"context"
"time"
"gitlab.bareksa.com/backend/zen/core/zoptions"
)
type WrapInput struct {
Errors []error
2024-08-22 17:26:53 +07:00
Message string
PC uintptr
Time time.Time
Logger Logger
Notifier Notifier
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 Notifier interface {
2024-08-23 10:18:27 +07:00
NotifyError(ctx context.Context, err Error, opts ...zoptions.NotifyOption)
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
}