-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
error.go
38 lines (31 loc) · 784 Bytes
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright (c) 2021, Peter Ohler, All rights reserved.
package ojg
import (
"fmt"
"runtime/debug"
)
// ErrorWithStack if true the Error() call will include the stack.
var ErrorWithStack = false
// Error struct to hold an error message and a stack trace.
type Error struct {
msg string
stack []byte
}
// NewError creates a new Error instance, capturing the stack when created.
func NewError(r any) *Error {
return &Error{
msg: fmt.Sprintf("%v", r),
stack: debug.Stack(),
}
}
// Error returns a string representation of the instance.
func (err *Error) Error() string {
if ErrorWithStack {
return string(append(append([]byte(err.msg), '\n'), err.stack...))
}
return err.msg
}
// Stack returns the stack.
func (err *Error) Stack() []byte {
return err.stack
}