github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/errors/errors.go (about)

     1  package errors
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/ronaksoft/rony"
     7  )
     8  
     9  /*
    10     Creation Time: 2021 - May - 21
    11     Created by:  (ehsan)
    12     Maintainers:
    13        1.  Ehsan N. Moosa (E2)
    14     Auditor: Ehsan N. Moosa (E2)
    15     Copyright Ronak Software Group 2020
    16  */
    17  
    18  func New(code Code, item string) *rony.Error {
    19  	return &rony.Error{
    20  		Code:        string(code),
    21  		Items:       item,
    22  		Description: "",
    23  	}
    24  }
    25  
    26  func NewF(code Code, item string, format string, args ...interface{}) *rony.Error {
    27  	return &rony.Error{
    28  		Code:        string(code),
    29  		Items:       item,
    30  		Description: fmt.Sprintf(format, args...),
    31  	}
    32  }
    33  
    34  func Message(reqID uint64, errCode Code, errItem string) *rony.MessageEnvelope {
    35  	msg := &rony.MessageEnvelope{}
    36  	ToMessage(msg, reqID, errCode, errItem)
    37  
    38  	return msg
    39  }
    40  
    41  func ToMessage(out *rony.MessageEnvelope, reqID uint64, errCode Code, errItem string) {
    42  	errMessage := rony.PoolError.Get()
    43  	errMessage.Code = string(errCode)
    44  	errMessage.Items = errItem
    45  	out.Fill(reqID, rony.C_Error, errMessage)
    46  	rony.PoolError.Put(errMessage)
    47  }
    48  
    49  var (
    50  	GenAccessErr           = genWithErrorAndItem(Access)
    51  	GenAlreadyExistsErr    = genWithErrorAndItem(AlreadyExists)
    52  	GenBusyErr             = genWithErrorAndItem(Busy)
    53  	GenExpiredErr          = genWithErrorAndItem(Expired)
    54  	GenIncompleteErr       = genWithErrorAndItem(Incomplete)
    55  	GenInternalErr         = genWithErrorAndItem(Internal)
    56  	GenInvalidErr          = genWithErrorAndItem(Invalid)
    57  	GenNotImplementedErr   = genWithErrorAndItem(NotImplemented)
    58  	GenOutOfRangeErr       = genWithErrorAndItem(OutOfRange)
    59  	GenPartiallyAppliedErr = genWithErrorAndItem(PartiallyApplied)
    60  	GenTimeoutErr          = genWithErrorAndItem(Timeout)
    61  	GenTooFewErr           = genWithErrorAndItem(TooFew)
    62  	GenTooManyErr          = genWithErrorAndItem(TooMany)
    63  	GenUnavailableErr      = genWithErrorAndItem(Unavailable)
    64  )
    65  
    66  func genWithErrorAndItem(code Code) func(item string, err error) *rony.Error {
    67  	codeString := string(code)
    68  
    69  	return func(item string, err error) *rony.Error {
    70  		if err != nil {
    71  			return &rony.Error{
    72  				Code:        codeString,
    73  				Items:       item,
    74  				Description: err.Error(),
    75  			}
    76  		} else {
    77  			return &rony.Error{
    78  				Code:        codeString,
    79  				Items:       item,
    80  				Description: "",
    81  			}
    82  		}
    83  	}
    84  }