errs: updates errs lib

This commit is contained in:
Tigor Hutasuhut 2024-04-06 21:11:22 +07:00
parent aec979048e
commit 95f5923779
2 changed files with 90 additions and 36 deletions

View file

@ -5,10 +5,11 @@ import (
"errors" "errors"
"fmt" "fmt"
"log/slog" "log/slog"
"os"
"reflect" "reflect"
"runtime"
"strings" "strings"
"github.com/tigorlazuardi/redmage/caller"
"github.com/tigorlazuardi/redmage/log"
) )
type Error interface { type Error interface {
@ -17,8 +18,8 @@ type Error interface {
GetMessage() string GetMessage() string
Code(status int) Error Code(status int) Error
GetCode() int GetCode() int
Caller(pc uintptr) Error Caller(pc caller.Caller) Error
GetCaller() uintptr GetCaller() caller.Caller
Details(...any) Error Details(...any) Error
GetDetails() []any GetDetails() []any
Log(ctx context.Context) Error Log(ctx context.Context) Error
@ -27,9 +28,9 @@ type Error interface {
var _ Error = (*Err)(nil) var _ Error = (*Err)(nil)
type Err struct { type Err struct {
msg string message string
code int code int
caller uintptr caller caller.Caller
details []any details []any
origin error origin error
} }
@ -37,33 +38,34 @@ type Err struct {
func (er *Err) LogValue() slog.Value { func (er *Err) LogValue() slog.Value {
values := make([]slog.Attr, 0, 5) values := make([]slog.Attr, 0, 5)
if er.msg != "" { if er.message != "" {
values = append(values, slog.String("message", er.msg)) values = append(values, slog.String("message", er.message))
} }
if er.code != 0 { if er.code != 0 {
values = append(values, slog.Int("code", er.code)) values = append(values, slog.Int("code", er.code))
} }
if er.caller != 0 { if er.caller.PC != 0 {
frame, _ := runtime.CallersFrames([]uintptr{er.caller}).Next() values = append(values, slog.Any("origin", er.caller))
split := strings.Split(frame.Function, string(os.PathSeparator))
fnName := split[len(split)-1]
values = append(values, slog.Group("origin",
slog.String("file", frame.File),
slog.Int("line", frame.Line),
slog.String("function", fnName),
))
} }
if len(er.details) > 0 { if len(er.details) > 0 {
values = append(values, slog.Group("details", er.details...)) values = append(values, slog.Group("details", er.details...))
} }
values = append(values, slog.Group("error", if er.origin == nil {
slog.String("type", reflect.TypeOf(er.origin).String()), values = append(values, slog.Any("error", er.origin))
slog.Any("data", er.origin), } else {
)) errGroupValues := make([]slog.Attr, 0, 3)
errGroupValues = append(errGroupValues, slog.String("type", reflect.TypeOf(er.origin).String()))
if lv, ok := er.origin.(slog.LogValuer); ok {
errGroupValues = append(errGroupValues, slog.Attr{Key: "data", Value: lv.LogValue()})
} else {
errGroupValues = append(errGroupValues, slog.String("message", er.origin.Error()), slog.Any("data", er.origin))
}
values = append(values, slog.Attr{Key: "error", Value: slog.GroupValue(errGroupValues...)})
}
return slog.GroupValue(values...) return slog.GroupValue(values...)
} }
@ -76,8 +78,8 @@ func (er *Err) Error() string {
unwrap = errors.Unwrap(source) unwrap = errors.Unwrap(source)
) )
if unwrap == nil { if unwrap == nil {
if er.msg != "" { if er.message != "" {
s.WriteString(er.msg) s.WriteString(er.message)
s.WriteString(": ") s.WriteString(": ")
} }
s.WriteString(msg) s.WriteString(msg)
@ -101,38 +103,75 @@ func (er *Err) Error() string {
} }
func (er *Err) Message(msg string, args ...any) Error { func (er *Err) Message(msg string, args ...any) Error {
er.msg = fmt.Sprintf(msg, args...) er.message = fmt.Sprintf(msg, args...)
return er return er
} }
func (er *Err) GetMessage() string { func (er *Err) GetMessage() string {
panic("not implemented") // TODO: Implement return er.message
} }
func (er *Err) Code(status int) Error { func (er *Err) Code(status int) Error {
panic("not implemented") // TODO: Implement er.code = status
return er
} }
func (er *Err) GetCode() int { func (er *Err) GetCode() int {
panic("not implemented") // TODO: Implement return er.code
} }
func (er *Err) Caller(pc uintptr) Error { func (er *Err) Caller(pc caller.Caller) Error {
panic("not implemented") // TODO: Implement er.caller = pc
return er
} }
func (er *Err) GetCaller() uintptr { func (er *Err) GetCaller() caller.Caller {
panic("not implemented") // TODO: Implement return er.caller
} }
func (er *Err) Details(_ ...any) Error { func (er *Err) Details(ctx ...any) Error {
panic("not implemented") // TODO: Implement er.details = ctx
return er
} }
func (er *Err) GetDetails() []any { func (er *Err) GetDetails() []any {
panic("not implemented") // TODO: Implement return er.details
} }
func (er *Err) Log(ctx context.Context) Error { func (er *Err) Log(ctx context.Context) Error {
panic("not implemented") // TODO: Implement log.Log(ctx).Caller(er.caller).Error(er.message, "error", er)
return er
}
func Wrap(err error, message string, details ...any) Error {
return &Err{
origin: err,
details: details,
message: message,
caller: caller.New(3),
}
}
func Wrapf(err error, message string, args ...any) Error {
message = fmt.Sprintf(message, args...)
return &Err{
origin: err,
message: message,
caller: caller.New(3),
}
}
func Fail(message string, details ...any) Error {
return &Err{
origin: errors.New(message),
details: details,
caller: caller.New(3),
}
}
func Failf(message string, args ...any) Error {
return &Err{
origin: fmt.Errorf(message, args...),
caller: caller.New(3),
}
} }

15
errs/query.go Normal file
View file

@ -0,0 +1,15 @@
package errs
import "errors"
func FindCodeOrDefault(err error, def int) int {
for unwrap := errors.Unwrap(err); unwrap != nil; err = unwrap {
if coder, ok := err.(interface{ GetCode() int }); ok {
code := coder.GetCode()
if code != 0 {
def = code
}
}
}
return def
}