2021-04-26 13:52:50 +02:00
|
|
|
package ierrors
|
2017-10-04 21:44:58 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
type Error struct {
|
2017-10-04 21:44:58 +02:00
|
|
|
StatusCode int
|
|
|
|
Message string
|
|
|
|
PublicMessage string
|
2019-08-15 15:15:37 +02:00
|
|
|
Unexpected bool
|
2019-08-19 14:05:34 +02:00
|
|
|
|
|
|
|
stack []uintptr
|
2017-10-04 21:44:58 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
func (e *Error) Error() string {
|
2017-10-04 21:44:58 +02:00
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
func (e *Error) FormatStack() string {
|
2019-08-19 14:05:34 +02:00
|
|
|
if e.stack == nil {
|
2019-09-16 11:53:45 +02:00
|
|
|
return ""
|
2019-08-19 14:05:34 +02:00
|
|
|
}
|
|
|
|
|
2019-09-16 11:53:45 +02:00
|
|
|
return formatStack(e.stack)
|
2019-08-19 14:05:34 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
func (e *Error) StackTrace() []uintptr {
|
2019-08-19 14:05:34 +02:00
|
|
|
return e.stack
|
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
func New(status int, msg string, pub string) *Error {
|
|
|
|
return &Error{
|
2019-08-15 15:15:37 +02:00
|
|
|
StatusCode: status,
|
|
|
|
Message: msg,
|
|
|
|
PublicMessage: pub,
|
|
|
|
}
|
2017-10-04 21:44:58 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
func NewUnexpected(msg string, skip int) *Error {
|
|
|
|
return &Error{
|
2019-08-15 15:15:37 +02:00
|
|
|
StatusCode: 500,
|
2019-08-19 14:05:34 +02:00
|
|
|
Message: msg,
|
2019-08-15 15:15:37 +02:00
|
|
|
PublicMessage: "Internal error",
|
|
|
|
Unexpected: true,
|
2019-08-19 14:05:34 +02:00
|
|
|
|
|
|
|
stack: callers(skip + 3),
|
2019-05-08 16:37:26 +02:00
|
|
|
}
|
2017-10-04 21:44:58 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
func Wrap(err error, skip int) *Error {
|
|
|
|
if ierr, ok := err.(*Error); ok {
|
2021-03-22 18:45:37 +02:00
|
|
|
return ierr
|
|
|
|
}
|
2021-04-26 13:52:50 +02:00
|
|
|
return NewUnexpected(err.Error(), skip+1)
|
2021-03-22 18:45:37 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 13:59:46 +02:00
|
|
|
func WrapWithPrefix(err error, skip int, prefix string) *Error {
|
2021-09-29 12:23:54 +02:00
|
|
|
if ierr, ok := err.(*Error); ok {
|
|
|
|
newErr := *ierr
|
2021-10-13 13:59:46 +02:00
|
|
|
newErr.Message = fmt.Sprintf("%s: %s", prefix, ierr.Message)
|
2021-09-29 12:23:54 +02:00
|
|
|
return &newErr
|
|
|
|
}
|
2021-10-13 13:59:46 +02:00
|
|
|
return NewUnexpected(fmt.Sprintf("%s: %s", prefix, err), skip+1)
|
2021-09-29 12:23:54 +02:00
|
|
|
}
|
|
|
|
|
2019-08-19 14:05:34 +02:00
|
|
|
func callers(skip int) []uintptr {
|
|
|
|
stack := make([]uintptr, 10)
|
|
|
|
n := runtime.Callers(skip, stack)
|
|
|
|
return stack[:n]
|
|
|
|
}
|
2017-10-04 21:44:58 +02:00
|
|
|
|
2019-08-19 14:05:34 +02:00
|
|
|
func formatStack(stack []uintptr) string {
|
|
|
|
lines := make([]string, len(stack))
|
|
|
|
for i, pc := range stack {
|
2017-10-04 21:44:58 +02:00
|
|
|
f := runtime.FuncForPC(pc)
|
|
|
|
file, line := f.FileLine(pc)
|
|
|
|
lines[i] = fmt.Sprintf("%s:%d %s", file, line, f.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(lines, "\n")
|
|
|
|
}
|