github.com/ars0915/glossika-exercise@v0.0.0-20240904180321-a5617a00d892/util/cGin/error.go (about)

     1  package cGin
     2  
     3  // CustomError custom error
     4  type CustomError struct {
     5  	HTTPCode int
     6  	Code     int
     7  	Message  string
     8  }
     9  
    10  func (cErr CustomError) Error() string {
    11  	return cErr.Message
    12  }
    13  
    14  // type CheckCustomErrorFunc func(*CustomError) bool
    15  
    16  // // CheckCustomError compare input err's chain matches CustomError
    17  // // if err's chain is contains CustomError, check the CustomError with checkFunc
    18  // func CheckCustomError(err error, checkFuncs ...CheckCustomErrorFunc) bool {
    19  // 	valid, cErr := RecursivelyUnwrap(err)
    20  // 	if !valid {
    21  // 		return false
    22  // 	}
    23  
    24  // 	for _, checkFunc := range checkFuncs {
    25  // 		if checkFuncValid := checkFunc(cErr); !checkFuncValid {
    26  // 			return false
    27  // 		}
    28  // 	}
    29  // 	return true
    30  // }
    31  
    32  // // Is implements errors interface Is(error) bool
    33  // func (cErr CustomError) Is(target error) bool {
    34  // 	causeErr := errors.Cause(target)
    35  // 	if _cErr, ok := causeErr.(*CustomError); ok {
    36  // 		return cErr.Code == _cErr.Code
    37  // 	}
    38  
    39  // 	if _cErr, ok := causeErr.(CustomError); ok {
    40  // 		return cErr.Code == _cErr.Code
    41  // 	}
    42  
    43  // 	return false
    44  // }
    45  
    46  // // As implements errors interface As(interface) bool (go 1.15)
    47  // func (cErr CustomError) As(target interface{}) bool {
    48  // 	if _, ok := target.(CustomError); ok {
    49  // 		return ok
    50  // 	}
    51  
    52  // 	if _, ok := target.(*CustomError); ok {
    53  // 		return ok
    54  // 	}
    55  
    56  // 	return false
    57  // }
    58  
    59  // /* RecursivelyUnwrap
    60  //  * check the err's type in err's chain is contains CustomError
    61  //  * if true, unwrap error until the CustomError return
    62  //  * if false, return nil
    63  //  */
    64  // func RecursivelyUnwrap(err error) (bool, *CustomError) {
    65  // 	return recursivelyUnwrap(err, false, 0)
    66  
    67  // }
    68  // func recursivelyUnwrap(err error, skipCheckType bool, unwrapTimes uint) (bool, *CustomError) {
    69  // 	if !skipCheckType && !errors.As(err, &CustomError{}) {
    70  // 		return false, nil
    71  // 	}
    72  
    73  // 	var unwrapErr error
    74  // 	unwrapErr = err
    75  // 	if unwrapTimes != 0 {
    76  // 		unwrapErr = errors.Unwrap(err)
    77  // 	}
    78  
    79  // 	cusErr, ok := unwrapErr.(CustomError)
    80  // 	if ok {
    81  // 		return true, &cusErr
    82  // 	}
    83  
    84  // 	ptrCusErr, ok := unwrapErr.(*CustomError)
    85  // 	if ok {
    86  // 		return true, ptrCusErr
    87  // 	}
    88  // 	unwrapTimes++
    89  // 	return recursivelyUnwrap(unwrapErr, true, unwrapTimes)
    90  // }