Added error string and better data default

This commit is contained in:
Magnus Åhall 2024-03-29 09:38:32 +01:00
parent 776917b9c0
commit 16d41de1ae

7
pkg.go
View File

@ -9,9 +9,10 @@ import (
type Error struct { type Error struct {
Wrapped error Wrapped error
ErrStr string // wrapped error isn't necessarily json encodable
File string File string
Line int Line int
Data interface{} Data interface{} `json:",omitempty"`
} }
type LogCallback func(Error) type LogCallback func(Error)
@ -57,9 +58,9 @@ func create(err error, data interface{}) error {
_, file, line, _ := runtime.Caller(2) _, file, line, _ := runtime.Caller(2)
file = file[baseDirLength+1:] file = file[baseDirLength+1:]
wrapped := Error{ wrapped := Error{
Wrapped: err, Wrapped: err,
ErrStr: err.Error(),
File: file, File: file,
Line: line, Line: line,
Data: data, Data: data,
@ -71,7 +72,7 @@ func create(err error, data interface{}) error {
// Wrap wraps an existing error with file and line. // Wrap wraps an existing error with file and line.
func Wrap(err error) error { func Wrap(err error) error {
return create(err, "") return create(err, nil)
} }
func WrapData(err error, data interface{}) error { func WrapData(err error, data interface{}) error {