mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
43 lines
924 B
Go
43 lines
924 B
Go
// 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()
|
|
}
|