2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-06-30 22:21:08 +08:00
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
|
|
package gerror
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2020-11-23 16:32:57 +08:00
|
|
|
"errors"
|
2019-06-30 22:21:08 +08:00
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"runtime"
|
|
|
|
|
"strings"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
2019-06-30 22:21:08 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Error is custom error for additional features.
|
|
|
|
|
type Error struct {
|
2021-08-24 21:18:59 +08:00
|
|
|
error error // Wrapped error.
|
|
|
|
|
stack stack // Stack array, which records the stack information when this error is created or wrapped.
|
2021-11-14 17:47:21 +08:00
|
|
|
text string // Custom Error text when Error is created, might be empty when its code is not nil.
|
2021-08-24 21:18:59 +08:00
|
|
|
code gcode.Code // Error code if necessary.
|
2019-06-30 22:21:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
2021-02-04 00:10:13 +08:00
|
|
|
// Filtering key for current error module paths.
|
|
|
|
|
stackFilterKeyLocal = "/errors/gerror/gerror"
|
2019-06-30 22:21:08 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2021-11-14 17:47:21 +08:00
|
|
|
// goRootForFilter is used for stack filtering in development environment purpose.
|
2019-06-30 22:21:08 +08:00
|
|
|
goRootForFilter = runtime.GOROOT()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
if goRootForFilter != "" {
|
|
|
|
|
goRootForFilter = strings.Replace(goRootForFilter, "\\", "/", -1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 17:17:10 +08:00
|
|
|
// Error implements the interface of Error, it returns all the error as string.
|
2019-06-30 22:21:08 +08:00
|
|
|
func (err *Error) Error() string {
|
2020-10-17 17:17:10 +08:00
|
|
|
if err == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2020-12-11 00:45:15 +08:00
|
|
|
errStr := err.text
|
2021-08-24 21:18:59 +08:00
|
|
|
if errStr == "" && err.code != nil {
|
|
|
|
|
errStr = err.code.Message()
|
2021-08-05 11:40:31 +08:00
|
|
|
}
|
2020-12-10 23:33:24 +08:00
|
|
|
if err.error != nil {
|
2021-08-07 10:44:57 +08:00
|
|
|
if errStr != "" {
|
2020-12-11 00:45:15 +08:00
|
|
|
errStr += ": "
|
|
|
|
|
}
|
|
|
|
|
errStr += err.error.Error()
|
2020-12-10 23:33:24 +08:00
|
|
|
}
|
2020-12-11 00:45:15 +08:00
|
|
|
return errStr
|
2020-12-10 23:33:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Code returns the error code.
|
2021-08-04 20:50:45 +08:00
|
|
|
// It returns CodeNil if it has no error code.
|
2021-08-24 21:18:59 +08:00
|
|
|
func (err *Error) Code() gcode.Code {
|
2020-12-10 23:33:24 +08:00
|
|
|
if err == nil {
|
2021-08-24 21:18:59 +08:00
|
|
|
return gcode.CodeNil
|
2020-12-10 23:33:24 +08:00
|
|
|
}
|
2022-01-25 20:43:57 +08:00
|
|
|
if err.code == gcode.CodeNil {
|
|
|
|
|
return Code(err.Next())
|
|
|
|
|
}
|
2020-12-10 23:33:24 +08:00
|
|
|
return err.code
|
2019-06-30 22:21:08 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-09 10:40:26 +08:00
|
|
|
// Cause returns the root cause error.
|
|
|
|
|
func (err *Error) Cause() error {
|
2020-10-17 17:17:10 +08:00
|
|
|
if err == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-07-09 10:40:26 +08:00
|
|
|
loop := err
|
|
|
|
|
for loop != nil {
|
|
|
|
|
if loop.error != nil {
|
|
|
|
|
if e, ok := loop.error.(*Error); ok {
|
2020-11-23 16:32:57 +08:00
|
|
|
// Internal Error struct.
|
2019-07-09 10:40:26 +08:00
|
|
|
loop = e
|
2021-09-17 19:26:56 +08:00
|
|
|
} else if e, ok := loop.error.(iCause); ok {
|
2020-11-23 16:32:57 +08:00
|
|
|
// Other Error that implements ApiCause interface.
|
|
|
|
|
return e.Cause()
|
2019-07-09 10:40:26 +08:00
|
|
|
} else {
|
|
|
|
|
return loop.error
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-11-23 16:32:57 +08:00
|
|
|
// return loop
|
|
|
|
|
// To be compatible with Case of https://github.com/pkg/errors.
|
|
|
|
|
return errors.New(loop.text)
|
2019-07-09 10:40:26 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 22:21:08 +08:00
|
|
|
// Format formats the frame according to the fmt.Formatter interface.
|
|
|
|
|
//
|
2020-10-17 17:17:10 +08:00
|
|
|
// %v, %s : Print all the error string;
|
|
|
|
|
// %-v, %-s : Print current level error string;
|
2019-06-30 22:21:08 +08:00
|
|
|
// %+s : Print full stack error list;
|
|
|
|
|
// %+v : Print the error string and full stack error list;
|
|
|
|
|
func (err *Error) Format(s fmt.State, verb rune) {
|
|
|
|
|
switch verb {
|
|
|
|
|
case 's', 'v':
|
|
|
|
|
switch {
|
|
|
|
|
case s.Flag('-'):
|
|
|
|
|
if err.text != "" {
|
2021-11-14 17:47:21 +08:00
|
|
|
_, _ = io.WriteString(s, err.text)
|
2019-06-30 22:21:08 +08:00
|
|
|
} else {
|
2021-11-14 17:47:21 +08:00
|
|
|
_, _ = io.WriteString(s, err.Error())
|
2019-06-30 22:21:08 +08:00
|
|
|
}
|
|
|
|
|
case s.Flag('+'):
|
|
|
|
|
if verb == 's' {
|
2021-11-14 17:47:21 +08:00
|
|
|
_, _ = io.WriteString(s, err.Stack())
|
2019-06-30 22:21:08 +08:00
|
|
|
} else {
|
2021-11-14 17:47:21 +08:00
|
|
|
_, _ = io.WriteString(s, err.Error()+"\n"+err.Stack())
|
2019-06-30 22:21:08 +08:00
|
|
|
}
|
|
|
|
|
default:
|
2021-11-14 17:47:21 +08:00
|
|
|
_, _ = io.WriteString(s, err.Error())
|
2019-06-30 22:21:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stack returns the stack callers as string.
|
2021-09-16 20:57:59 +08:00
|
|
|
// It returns an empty string if the `err` does not support stacks.
|
2019-06-30 22:21:08 +08:00
|
|
|
func (err *Error) Stack() string {
|
|
|
|
|
if err == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2020-11-23 16:32:57 +08:00
|
|
|
var (
|
|
|
|
|
loop = err
|
|
|
|
|
index = 1
|
|
|
|
|
buffer = bytes.NewBuffer(nil)
|
|
|
|
|
)
|
2019-06-30 22:21:08 +08:00
|
|
|
for loop != nil {
|
2019-07-16 19:49:21 +08:00
|
|
|
buffer.WriteString(fmt.Sprintf("%d. %-v\n", index, loop))
|
2019-06-30 22:21:08 +08:00
|
|
|
index++
|
|
|
|
|
formatSubStack(loop.stack, buffer)
|
|
|
|
|
if loop.error != nil {
|
|
|
|
|
if e, ok := loop.error.(*Error); ok {
|
|
|
|
|
loop = e
|
|
|
|
|
} else {
|
2019-07-16 19:49:21 +08:00
|
|
|
buffer.WriteString(fmt.Sprintf("%d. %s\n", index, loop.error.Error()))
|
2019-06-30 22:21:08 +08:00
|
|
|
index++
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return buffer.String()
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 17:17:10 +08:00
|
|
|
// Current creates and returns the current level error.
|
|
|
|
|
// It returns nil if current level error is nil.
|
|
|
|
|
func (err *Error) Current() error {
|
|
|
|
|
if err == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return &Error{
|
|
|
|
|
error: nil,
|
|
|
|
|
stack: err.stack,
|
|
|
|
|
text: err.text,
|
2021-08-04 20:50:45 +08:00
|
|
|
code: err.code,
|
2020-10-17 17:17:10 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Next returns the next level error.
|
|
|
|
|
// It returns nil if current level error or the next level error is nil.
|
|
|
|
|
func (err *Error) Next() error {
|
|
|
|
|
if err == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return err.error
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-14 17:47:21 +08:00
|
|
|
// SetCode updates the internal code with given code.
|
|
|
|
|
func (err *Error) SetCode(code gcode.Code) {
|
|
|
|
|
if err == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err.code = code
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-14 00:05:15 +08:00
|
|
|
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
|
|
|
|
// Note that do not use pointer as its receiver here.
|
2022-01-19 16:55:57 +08:00
|
|
|
func (err Error) MarshalJSON() ([]byte, error) {
|
2021-01-14 00:05:15 +08:00
|
|
|
return []byte(`"` + err.Error() + `"`), nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 22:21:08 +08:00
|
|
|
// formatSubStack formats the stack for error.
|
|
|
|
|
func formatSubStack(st stack, buffer *bytes.Buffer) {
|
2021-07-20 23:02:02 +08:00
|
|
|
if st == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-06-30 22:21:08 +08:00
|
|
|
index := 1
|
2019-07-16 19:49:21 +08:00
|
|
|
space := " "
|
2019-06-30 22:21:08 +08:00
|
|
|
for _, p := range st {
|
|
|
|
|
if fn := runtime.FuncForPC(p - 1); fn != nil {
|
|
|
|
|
file, line := fn.FileLine(p - 1)
|
2021-02-04 00:10:13 +08:00
|
|
|
// Custom filtering.
|
2021-12-04 23:07:54 +08:00
|
|
|
if strings.Contains(file, stackFilterKeyLocal) {
|
|
|
|
|
continue
|
2019-06-30 22:21:08 +08:00
|
|
|
}
|
2021-09-16 20:57:59 +08:00
|
|
|
// Avoid stack string like "`autogenerated`"
|
2019-11-21 21:49:00 +08:00
|
|
|
if strings.Contains(file, "<") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2021-02-04 00:10:13 +08:00
|
|
|
// Ignore GO ROOT paths.
|
|
|
|
|
if goRootForFilter != "" &&
|
|
|
|
|
len(file) >= len(goRootForFilter) &&
|
|
|
|
|
file[0:len(goRootForFilter)] == goRootForFilter {
|
2019-06-30 22:21:08 +08:00
|
|
|
continue
|
|
|
|
|
}
|
2021-02-04 00:10:13 +08:00
|
|
|
// Graceful indent.
|
2019-07-16 19:49:21 +08:00
|
|
|
if index > 9 {
|
|
|
|
|
space = " "
|
|
|
|
|
}
|
2021-02-04 00:10:13 +08:00
|
|
|
buffer.WriteString(fmt.Sprintf(
|
|
|
|
|
" %d).%s%s\n \t%s:%d\n",
|
|
|
|
|
index, space, fn.Name(), file, line,
|
|
|
|
|
))
|
2019-06-30 22:21:08 +08:00
|
|
|
index++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|