2019-06-29 18:17:33 +08:00
|
|
|
// Copyright 2019 gf Author(https://github.com/gogf/gf). 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 errors provides simple functions to manipulate errors.
|
|
|
|
package gerror
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-07-13 14:24:44 +08:00
|
|
|
|
2019-06-29 18:17:33 +08:00
|
|
|
"github.com/gogf/gf/g/util/gconv"
|
|
|
|
)
|
|
|
|
|
2019-07-13 14:24:44 +08:00
|
|
|
type ApiStack interface {
|
|
|
|
Stack() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ApiCause interface {
|
|
|
|
Cause() error
|
|
|
|
}
|
|
|
|
|
2019-06-29 18:17:33 +08:00
|
|
|
// New returns an error that formats as the given value.
|
|
|
|
func New(value interface{}) error {
|
|
|
|
if value == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return NewText(gconv.String(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewText returns an error that formats as the given text.
|
|
|
|
func NewText(text string) error {
|
|
|
|
if text == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-30 22:21:08 +08:00
|
|
|
return &Error{
|
|
|
|
stack: callers(),
|
|
|
|
text: text,
|
2019-06-30 12:54:06 +08:00
|
|
|
}
|
2019-06-29 18:17:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap wraps error with text.
|
2019-06-30 22:21:08 +08:00
|
|
|
// It returns nil if given err is nil.
|
2019-06-29 18:17:33 +08:00
|
|
|
func Wrap(err error, text string) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-30 22:21:08 +08:00
|
|
|
return &Error{
|
|
|
|
error: err,
|
|
|
|
stack: callers(),
|
|
|
|
text: text,
|
|
|
|
}
|
2019-06-29 18:17:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapf returns an error annotating err with a stack trace
|
|
|
|
// at the point Wrapf is called, and the format specifier.
|
2019-06-30 22:21:08 +08:00
|
|
|
// It returns nil if given err is nil.
|
2019-06-29 18:17:33 +08:00
|
|
|
func Wrapf(err error, format string, args ...interface{}) error {
|
2019-06-30 22:21:08 +08:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &Error{
|
|
|
|
error: err,
|
|
|
|
stack: callers(),
|
|
|
|
text: fmt.Sprintf(format, args...),
|
2019-06-29 23:35:32 +08:00
|
|
|
}
|
2019-06-29 18:17:33 +08:00
|
|
|
}
|
|
|
|
|
2019-07-09 10:40:26 +08:00
|
|
|
// Cause returns the root cause error.
|
|
|
|
func Cause(err error) error {
|
|
|
|
if err != nil {
|
2019-07-13 14:24:44 +08:00
|
|
|
if e, ok := err.(ApiCause); ok {
|
2019-07-09 10:40:26 +08:00
|
|
|
return e.Cause()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-29 18:17:33 +08:00
|
|
|
// Stack returns the stack callers as string.
|
2019-06-30 22:21:08 +08:00
|
|
|
// It returns an empty string id the <err> does not support stacks.
|
2019-06-29 18:17:33 +08:00
|
|
|
func Stack(err error) string {
|
|
|
|
if err == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2019-07-13 14:24:44 +08:00
|
|
|
if e, ok := err.(ApiStack); ok {
|
2019-06-30 22:21:08 +08:00
|
|
|
return e.Stack()
|
2019-06-29 18:17:33 +08:00
|
|
|
}
|
2019-06-30 22:21:08 +08:00
|
|
|
return ""
|
2019-06-29 18:17:33 +08:00
|
|
|
}
|