2024-08-22 17:26:53 +07:00
|
|
|
package zerr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-08-27 17:08:52 +07:00
|
|
|
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
|
2024-08-27 17:08:52 +07:00
|
|
|
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.
|
2024-08-27 17:08:52 +07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-08-27 17:08:52 +07:00
|
|
|
type WrapperFunc func(input WrapInput) Error
|
2024-08-22 17:26:53 +07:00
|
|
|
|
2024-08-27 17:08:52 +07:00
|
|
|
func (wr WrapperFunc) Wrap(input WrapInput) Error {
|
2024-08-22 17:26:53 +07:00
|
|
|
return wr(input)
|
|
|
|
}
|
|
|
|
|
2024-08-27 17:08:52 +07:00
|
|
|
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
|
|
|
})
|
2024-08-27 17:08:52 +07:00
|
|
|
|
|
|
|
func Wrap(input WrapInput) Error {
|
|
|
|
return DefaultWrapper.Wrap(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetDefaultWrapper(w Wrapper) {
|
|
|
|
DefaultWrapper = w
|
|
|
|
}
|