You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
862 B
Go
48 lines
862 B
Go
7 months ago
|
package cerrors
|
||
|
|
||
|
import (
|
||
|
"git.blauwelle.com/go/crate/runtimehelper"
|
||
|
)
|
||
|
|
||
|
const maximumFrames = 16
|
||
|
|
||
|
type Error struct {
|
||
|
Code Code
|
||
|
Message string
|
||
|
Debug string
|
||
|
Traceback []runtimehelper.Frame
|
||
|
}
|
||
|
|
||
|
func (err *Error) Error() string {
|
||
|
if err.Message == "" {
|
||
|
return string(err.Code)
|
||
|
}
|
||
|
return string(err.Code) + ": " + err.Message
|
||
|
}
|
||
|
|
||
|
func New(code Code, message string) error {
|
||
|
e := &Error{
|
||
|
Code: code,
|
||
|
Message: message,
|
||
|
}
|
||
|
if FlagDebug {
|
||
|
e.Traceback = runtimehelper.Stack(1, maximumFrames)
|
||
|
}
|
||
|
return e
|
||
|
}
|
||
|
|
||
|
func Wrap(err error, code Code, message string) error {
|
||
|
e2 := &Error{
|
||
|
Code: code,
|
||
|
Message: message,
|
||
|
}
|
||
|
if e1, ok := err.(*Error); ok { //nolint:errorlint
|
||
|
e2.Debug = e1.Debug
|
||
|
e2.Traceback = e1.Traceback
|
||
|
} else if FlagDebug {
|
||
|
e2.Debug = err.Error()
|
||
|
e2.Traceback = runtimehelper.Stack(1, maximumFrames)
|
||
|
}
|
||
|
return e2
|
||
|
}
|