github.com/gogf/gf/v2@v2.7.4/errors/gerror/gerror_api_option.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gerror
     8  
     9  import "github.com/gogf/gf/v2/errors/gcode"
    10  
    11  // Option is option for creating error.
    12  type Option struct {
    13  	Error error      // Wrapped error if any.
    14  	Stack bool       // Whether recording stack information into error.
    15  	Text  string     // Error text, which is created by New* functions.
    16  	Code  gcode.Code // Error code if necessary.
    17  }
    18  
    19  // NewWithOption creates and returns a custom error with Option.
    20  // It is the senior usage for creating error, which is often used internally in framework.
    21  func NewWithOption(option Option) error {
    22  	err := &Error{
    23  		error: option.Error,
    24  		text:  option.Text,
    25  		code:  option.Code,
    26  	}
    27  	if option.Stack {
    28  		err.stack = callers()
    29  	}
    30  	return err
    31  }
    32  
    33  // NewOption creates and returns a custom error with Option.
    34  // Deprecated: use NewWithOption instead.
    35  func NewOption(option Option) error {
    36  	return NewWithOption(option)
    37  }