add Is/Equal/Unwrap functions for package gerror

This commit is contained in:
John Guo
2022-04-12 15:45:26 +08:00
parent 2d6fcf5d06
commit e8581d4fd5
10 changed files with 330 additions and 149 deletions

View File

@ -0,0 +1,42 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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"
"fmt"
)
// Stack returns the stack callers as string.
// It returns an empty string if the `err` does not support stacks.
func (err *Error) Stack() string {
if err == nil {
return ""
}
var (
loop = err
index = 1
buffer = bytes.NewBuffer(nil)
)
for loop != nil {
buffer.WriteString(fmt.Sprintf("%d. %-v\n", index, loop))
index++
formatSubStack(loop.stack, buffer)
if loop.error != nil {
if e, ok := loop.error.(*Error); ok {
loop = e
} else {
buffer.WriteString(fmt.Sprintf("%d. %s\n", index, loop.error.Error()))
index++
break
}
} else {
break
}
}
return buffer.String()
}