2021-07-20 23:02:02 +08:00
|
|
|
// 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
|
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
import "github.com/gogf/gf/v2/errors/gcode"
|
2021-08-24 21:18:59 +08:00
|
|
|
|
2021-07-20 23:02:02 +08:00
|
|
|
// Option is option for creating error.
|
|
|
|
|
type Option struct {
|
2021-08-24 21:18:59 +08:00
|
|
|
Error error // Wrapped error if any.
|
|
|
|
|
Stack bool // Whether recording stack information into error.
|
|
|
|
|
Text string // Error text, which is created by New* functions.
|
|
|
|
|
Code gcode.Code // Error code if necessary.
|
2021-07-20 23:02:02 +08:00
|
|
|
}
|
|
|
|
|
|
2023-12-04 19:34:48 +08:00
|
|
|
// NewWithOption creates and returns a custom error with Option.
|
2021-07-30 15:15:44 +08:00
|
|
|
// It is the senior usage for creating error, which is often used internally in framework.
|
2023-12-04 19:34:48 +08:00
|
|
|
func NewWithOption(option Option) error {
|
2021-07-20 23:02:02 +08:00
|
|
|
err := &Error{
|
|
|
|
|
error: option.Error,
|
|
|
|
|
text: option.Text,
|
|
|
|
|
code: option.Code,
|
|
|
|
|
}
|
|
|
|
|
if option.Stack {
|
|
|
|
|
err.stack = callers()
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
2023-12-04 19:34:48 +08:00
|
|
|
|
|
|
|
|
// NewOption creates and returns a custom error with Option.
|
|
|
|
|
// Deprecated: use NewWithOption instead.
|
|
|
|
|
func NewOption(option Option) error {
|
|
|
|
|
return NewWithOption(option)
|
|
|
|
|
}
|