mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
add Is/Equal/Unwrap functions for package gerror
This commit is contained in:
42
errors/gerror/gerror_error_stack.go
Normal file
42
errors/gerror/gerror_error_stack.go
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user