github.com/wangyougui/gf/v2@v2.6.5/errors/gerror/gerror_api.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/wangyougui/gf.
     6  
     7  package gerror
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/wangyougui/gf/v2/errors/gcode"
    13  )
    14  
    15  // New creates and returns an error which is formatted from given text.
    16  func New(text string) error {
    17  	return &Error{
    18  		stack: callers(),
    19  		text:  text,
    20  		code:  gcode.CodeNil,
    21  	}
    22  }
    23  
    24  // Newf returns an error that formats as the given format and args.
    25  func Newf(format string, args ...interface{}) error {
    26  	return &Error{
    27  		stack: callers(),
    28  		text:  fmt.Sprintf(format, args...),
    29  		code:  gcode.CodeNil,
    30  	}
    31  }
    32  
    33  // NewSkip creates and returns an error which is formatted from given text.
    34  // The parameter `skip` specifies the stack callers skipped amount.
    35  func NewSkip(skip int, text string) error {
    36  	return &Error{
    37  		stack: callers(skip),
    38  		text:  text,
    39  		code:  gcode.CodeNil,
    40  	}
    41  }
    42  
    43  // NewSkipf returns an error that formats as the given format and args.
    44  // The parameter `skip` specifies the stack callers skipped amount.
    45  func NewSkipf(skip int, format string, args ...interface{}) error {
    46  	return &Error{
    47  		stack: callers(skip),
    48  		text:  fmt.Sprintf(format, args...),
    49  		code:  gcode.CodeNil,
    50  	}
    51  }
    52  
    53  // Wrap wraps error with text. It returns nil if given err is nil.
    54  // Note that it does not lose the error code of wrapped error, as it inherits the error code from it.
    55  func Wrap(err error, text string) error {
    56  	if err == nil {
    57  		return nil
    58  	}
    59  	return &Error{
    60  		error: err,
    61  		stack: callers(),
    62  		text:  text,
    63  		code:  Code(err),
    64  	}
    65  }
    66  
    67  // Wrapf returns an error annotating err with a stack trace at the point Wrapf is called, and the format specifier.
    68  // It returns nil if given `err` is nil.
    69  // Note that it does not lose the error code of wrapped error, as it inherits the error code from it.
    70  func Wrapf(err error, format string, args ...interface{}) error {
    71  	if err == nil {
    72  		return nil
    73  	}
    74  	return &Error{
    75  		error: err,
    76  		stack: callers(),
    77  		text:  fmt.Sprintf(format, args...),
    78  		code:  Code(err),
    79  	}
    80  }
    81  
    82  // WrapSkip wraps error with text. It returns nil if given err is nil.
    83  // The parameter `skip` specifies the stack callers skipped amount.
    84  // Note that it does not lose the error code of wrapped error, as it inherits the error code from it.
    85  func WrapSkip(skip int, err error, text string) error {
    86  	if err == nil {
    87  		return nil
    88  	}
    89  	return &Error{
    90  		error: err,
    91  		stack: callers(skip),
    92  		text:  text,
    93  		code:  Code(err),
    94  	}
    95  }
    96  
    97  // WrapSkipf wraps error with text that is formatted with given format and args. It returns nil if given err is nil.
    98  // The parameter `skip` specifies the stack callers skipped amount.
    99  // Note that it does not lose the error code of wrapped error, as it inherits the error code from it.
   100  func WrapSkipf(skip int, err error, format string, args ...interface{}) error {
   101  	if err == nil {
   102  		return nil
   103  	}
   104  	return &Error{
   105  		error: err,
   106  		stack: callers(skip),
   107  		text:  fmt.Sprintf(format, args...),
   108  		code:  Code(err),
   109  	}
   110  }