github.com/jurado-dev/errors@v1.2.10/error_types.go (about)

     1  package errors
     2  
     3  func parseFields(fields []interface{}) Err {
     4  
     5  	var err Err
     6  	for _, field := range fields {
     7  
     8  		if e, ok := field.(error); ok {
     9  			err.Cause = e.Error()
    10  			err.Wrapped = e
    11  		}
    12  
    13  		if _, ok := field.(string); ok {
    14  			err.Message = field.(string)
    15  			continue
    16  		}
    17  
    18  		if _, ok := field.(ErrTrace); ok {
    19  			err.Trace = field.(ErrTrace)
    20  			err.Stack = append(err.Stack, field.(ErrTrace))
    21  			continue
    22  		}
    23  
    24  		if _, ok := field.(int); ok {
    25  			err.Code = field.(int)
    26  			continue
    27  		}
    28  	}
    29  	return err
    30  }
    31  
    32  type BadRequest struct {
    33  	Err
    34  }
    35  func NewBadRequest(fields ...interface{}) *BadRequest {
    36  	e := &BadRequest{Err: parseFields(fields)}
    37  	if e.Err.Code <= 0 {
    38  		e.Err.Code = 400
    39  	}
    40  	return e
    41  }
    42  func IsBadRequest(err error) bool {
    43  	_, ok := err.(*BadRequest)
    44  	return ok
    45  }
    46  
    47  type Internal struct {
    48  	Err
    49  }
    50  func NewInternal(fields ...interface{}) *Internal {
    51  	e := &Internal{Err: parseFields(fields)}
    52  	if e.Err.Code <= 0 {
    53  		e.Err.Code = 500
    54  	}
    55  	return e
    56  }
    57  func IsInternal(err error) bool {
    58  	_, ok := err.(*Internal)
    59  	return ok
    60  }
    61  
    62  type NotFound struct {
    63  	Err
    64  }
    65  func NewNotFound(fields ...interface{}) *NotFound {
    66  	e := &NotFound{Err: parseFields(fields)}
    67  	if e.Err.Code <= 0 {
    68  		e.Err.Code = 404
    69  	}
    70  	return e
    71  }
    72  func IsNotFound(err error) bool {
    73  	_, ok := err.(*NotFound)
    74  	return ok
    75  }
    76  
    77  type Conflict struct {
    78  	Err
    79  }
    80  func NewConflict(fields ...interface{}) *Conflict {
    81  	e := &Conflict{Err: parseFields(fields)}
    82  	if e.Err.Code <= 0 {
    83  		e.Err.Code = 409
    84  	}
    85  	return e
    86  }
    87  func IsConflict(err error) bool {
    88  	_, ok := err.(*Conflict)
    89  	return ok
    90  }
    91  
    92  type Unauthorized struct {
    93  	Err
    94  }
    95  func NewUnauthorized(fields ...interface{}) *Unauthorized {
    96  	e := &Unauthorized{Err: parseFields(fields)}
    97  	if e.Err.Code <= 0 {
    98  		e.Err.Code = 403
    99  	}
   100  	return e
   101  }
   102  func IsUnauthorized(err error) bool {
   103  	_, ok := err.(*Unauthorized)
   104  	return ok
   105  }
   106  
   107  type Fatal struct {
   108  	Err
   109  }
   110  func NewFatal(fields ...interface{}) *Fatal {
   111  	e := &Fatal{Err: parseFields(fields)}
   112  	if e.Err.Code <= 0 {
   113  		e.Err.Code = 500
   114  	}
   115  	return e
   116  }
   117  func IsFatal(err error) bool {
   118  	_, ok := err.(*Fatal)
   119  	return ok
   120  }
   121  
   122  type NoContent struct {
   123  	Err
   124  }
   125  func NewNoContent(fields ...interface{}) *NoContent {
   126  	e := &NoContent{Err: parseFields(fields)}
   127  	if e.Err.Code <= 0 {
   128  		e.Err.Code = 204
   129  	}
   130  	return e
   131  }
   132  func IsNoContent(err error) bool {
   133  	_, ok := err.(*NoContent)
   134  	return ok
   135  }
   136  
   137  type Timeout struct {
   138  	Err
   139  }
   140  func NewTimeout(fields ...interface{}) *Timeout {
   141  	e := &Timeout{Err: parseFields(fields)}
   142  	if e.Err.Code <= 0 {
   143  		e.Err.Code = 408
   144  	}
   145  	return e
   146  }
   147  func IsTimeout(err error) bool {
   148  	_, ok := err.(*Timeout)
   149  	return ok
   150  }