gitee.com/hongliu9527/go-tools@v0.0.8/errors/gerror/gerror_api_option.go (about)

     1  /*
     2   * @Author: hongliu
     3   * @Date: 2022-12-29 10:51:12
     4   * @LastEditors: hongliu
     5   * @LastEditTime: 2022-12-29 11:27:09
     6   * @FilePath: \go-tools\errors\gerror\gerror_api_option.go
     7   * @Description: api错误创建选项
     8   *
     9   * Copyright (c) 2022 by 洪流, All Rights Reserved.
    10   */
    11  
    12  package gerror
    13  
    14  import "gitee.com/hongliu9527/go-tools/errors/gcode"
    15  
    16  // Option is option for creating error.
    17  type Option struct {
    18  	Error error      // Wrapped error if any.
    19  	Stack bool       // Whether recording stack information into error.
    20  	Text  string     // Error text, which is created by New* functions.
    21  	Code  gcode.Code // Error code if necessary.
    22  }
    23  
    24  // NewOption creates and returns a custom error with Option.
    25  // It is the senior usage for creating error, which is often used internally in framework.
    26  func NewOption(option Option) error {
    27  	err := &Error{
    28  		error: option.Error,
    29  		text:  option.Text,
    30  		code:  option.Code,
    31  	}
    32  	if option.Stack {
    33  		err.stack = callers()
    34  	}
    35  	return err
    36  }