github.com/songzhibin97/gkit@v1.2.13/errors/code.go (about)

     1  package errors
     2  
     3  // BadRequest new BadRequest error that is mapped to a 400 response.
     4  func BadRequest(reason, message string) *Error {
     5  	return Errorf(400, reason, message)
     6  }
     7  
     8  // IsBadRequest determines if err is an error which indicates a BadRequest error.
     9  // It supports wrapped errors.
    10  func IsBadRequest(err error) bool {
    11  	return Code(err) == 400
    12  }
    13  
    14  // Unauthorized new Unauthorized error that is mapped to a 401 response.
    15  func Unauthorized(reason, message string) *Error {
    16  	return Errorf(401, reason, message)
    17  }
    18  
    19  // IsUnauthorized determines if err is an error which indicates a Unauthorized error.
    20  // It supports wrapped errors.
    21  func IsUnauthorized(err error) bool {
    22  	return Code(err) == 401
    23  }
    24  
    25  // Forbidden new Forbidden error that is mapped to a 403 response.
    26  func Forbidden(reason, message string) *Error {
    27  	return Errorf(403, reason, message)
    28  }
    29  
    30  // IsForbidden determines if err is an error which indicates a Forbidden error.
    31  // It supports wrapped errors.
    32  func IsForbidden(err error) bool {
    33  	return Code(err) == 403
    34  }
    35  
    36  // NotFound new NotFound error that is mapped to a 404 response.
    37  func NotFound(reason, message string) *Error {
    38  	return Errorf(404, reason, message)
    39  }
    40  
    41  // IsNotFound determines if err is an error which indicates an NotFound error.
    42  // It supports wrapped errors.
    43  func IsNotFound(err error) bool {
    44  	return Code(err) == 404
    45  }
    46  
    47  // Conflict new Conflict error that is mapped to a 409 response.
    48  func Conflict(reason, message string) *Error {
    49  	return Errorf(409, reason, message)
    50  }
    51  
    52  // IsConflict determines if err is an error which indicates a Conflict error.
    53  // It supports wrapped errors.
    54  func IsConflict(err error) bool {
    55  	return Code(err) == 409
    56  }
    57  
    58  // InternalServer new InternalServer error that is mapped to a 500 response.
    59  func InternalServer(reason, message string) *Error {
    60  	return Errorf(500, reason, message)
    61  }
    62  
    63  // IsInternalServer determines if err is an error which indicates an Internal error.
    64  // It supports wrapped errors.
    65  func IsInternalServer(err error) bool {
    66  	return Code(err) == 500
    67  }
    68  
    69  // ServiceUnavailable new ServiceUnavailable error that is mapped to a HTTP 503 response.
    70  func ServiceUnavailable(reason, message string) *Error {
    71  	return Errorf(503, reason, message)
    72  }
    73  
    74  // IsServiceUnavailable determines if err is an error which indicates a Unavailable error.
    75  // It supports wrapped errors.
    76  func IsServiceUnavailable(err error) bool {
    77  	return Code(err) == 503
    78  }
    79  
    80  // GatewayTimeout new GatewayTimeout error that is mapped to a HTTP 504 response.
    81  func GatewayTimeout(reason, message string) *Error {
    82  	return Errorf(504, reason, message)
    83  }
    84  
    85  // IsGatewayTimeout determines if err is an error which indicates a GatewayTimeout error.
    86  // It supports wrapped errors.
    87  func IsGatewayTimeout(err error) bool {
    88  	return Code(err) == 504
    89  }
    90  
    91  // ClientClosed new ClientClosed error that is mapped to a HTTP 499 response.
    92  func ClientClosed(reason, message string) *Error {
    93  	return Errorf(499, reason, message)
    94  }
    95  
    96  // IsClientClosed determines if err is an error which indicates a IsClientClosed error.
    97  // It supports wrapped errors.
    98  func IsClientClosed(err error) bool {
    99  	return Code(err) == 499
   100  }