wrappederror/pkg.go

117 lines
2.3 KiB
Go
Raw Permalink Normal View History

2023-09-26 08:34:09 +02:00
package WrappedError
import (
// Standard
"fmt"
"path"
"runtime"
)
type Error struct {
2023-09-26 09:33:46 +02:00
Wrapped error
ErrStr string // wrapped error isn't necessarily json encodable
2024-03-29 11:14:01 +01:00
Code string `json:",omitempty"`
2023-09-26 09:37:09 +02:00
File string
Line int
2024-03-29 11:14:01 +01:00
Data any `json:",omitempty"`
}
type CodableError interface {
error
WithCode(string) CodableError
WithData(any) CodableError
2023-09-26 08:34:09 +02:00
}
type LogCallback func(Error)
var (
2023-09-26 09:37:09 +02:00
logCallback LogCallback
2023-09-26 08:34:09 +02:00
baseDirLength int
)
2023-09-26 09:17:17 +02:00
// Init only works if called from the main package and sets the length of code base path
// to later be removed in file paths to receive relative paths.
2023-09-26 09:11:10 +02:00
func Init() {
_, file, _, _ := runtime.Caller(1)
2023-09-26 09:12:57 +02:00
dirBase := path.Dir(file)
2023-09-26 08:34:09 +02:00
baseDirLength = len(dirBase)
}
2023-09-26 09:17:17 +02:00
// SetLogCallback gives a possibility to automatically run code to handle any errors.
2023-09-26 08:34:09 +02:00
func SetLogCallback(cbk LogCallback) {
logCallback = cbk
}
func callback(wrapped Error) {
if logCallback != nil {
logCallback(wrapped)
}
}
2023-09-26 09:17:17 +02:00
// Error implements the error inteface and adds filename and line to the error.
2023-09-26 08:34:09 +02:00
func (wrapped Error) Error() string {
2024-03-29 11:14:01 +01:00
var code string
if wrapped.Code != "" {
code = wrapped.Code + " "
}
2023-09-26 08:34:09 +02:00
return fmt.Sprintf(
2024-03-29 11:14:01 +01:00
"%s[%s:%d] %s",
code,
2023-09-26 09:31:18 +02:00
wrapped.File,
wrapped.Line,
2023-09-26 09:33:46 +02:00
wrapped.Wrapped.Error(),
2023-09-26 08:34:09 +02:00
)
}
2024-03-29 11:14:01 +01:00
func create(err error, data interface{}) *Error {
2023-09-27 07:32:50 +02:00
if err == nil {
return nil
}
2023-09-26 08:34:09 +02:00
_, file, line, _ := runtime.Caller(2)
file = file[baseDirLength+1:]
wrapped := Error{
2023-09-26 09:33:46 +02:00
Wrapped: err,
ErrStr: err.Error(),
2023-09-26 09:37:09 +02:00
File: file,
Line: line,
Data: data,
2023-09-26 08:34:09 +02:00
}
callback(wrapped)
2024-03-29 11:14:01 +01:00
return &wrapped
2023-09-26 08:34:09 +02:00
}
2023-09-26 09:17:17 +02:00
// Wrap wraps an existing error with file and line.
2024-03-29 11:14:01 +01:00
func Wrap(err error) *Error {
return create(err, nil)
2023-09-26 09:31:18 +02:00
}
2024-03-29 11:14:01 +01:00
func WrapData(err error, data interface{}) *Error {
2023-09-26 09:31:18 +02:00
return create(err, data)
2023-09-26 08:34:09 +02:00
}
2023-09-26 09:17:17 +02:00
// New creates a new wrapped error with file and line.
2024-03-29 11:14:01 +01:00
func New(msg string, params ...any) *Error {
err := fmt.Errorf(msg, params...)
2024-03-29 11:14:01 +01:00
wrapped := create(err, nil)
2023-09-26 09:31:18 +02:00
return wrapped
}
2024-03-29 11:14:01 +01:00
// NewData creates a new WrappedError with associated data.
func NewData(msg string, data interface{}, params ...any) *Error {
err := fmt.Errorf(msg, params...)
wrapped := create(err, data)
2023-09-26 08:34:09 +02:00
return wrapped
}
2024-03-29 11:14:01 +01:00
func (e *Error) WithCode(code string) CodableError {
e.Code = code
return e
}
func (e *Error) WithData(data any) CodableError {
e.Data = data
return e
}