github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/errors/error.go (about)

     1  package errors
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  )
     7  
     8  type qError struct {
     9  	top  error
    10  	down error
    11  }
    12  
    13  func (e qError) Error() string {
    14  	if e.down == nil {
    15  		return e.top.Error()
    16  	}
    17  
    18  	return fmt.Sprintf("%s: %s", e.top, e.down)
    19  }
    20  
    21  func (e qError) Is(err error) bool {
    22  	return errors.Is(e.top, err)
    23  }
    24  
    25  func (e qError) Unwrap() error {
    26  	return e.down
    27  }
    28  
    29  func New(format string, v ...any) error {
    30  	return fmt.Errorf(format, v...)
    31  }
    32  
    33  func NewG(format string) func(v ...any) error {
    34  	return func(v ...any) error {
    35  		return New(format, v...)
    36  	}
    37  }
    38  
    39  func Wrap(top, down error) error {
    40  	if top == nil {
    41  		return down
    42  	}
    43  
    44  	return qError{
    45  		top:  top,
    46  		down: down,
    47  	}
    48  }
    49  
    50  func Is(err, target error) bool {
    51  	return errors.Is(err, target)
    52  }