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

     1  package errors
     2  
     3  import (
     4  	"github.com/ronaksoft/rony/tools"
     5  	"github.com/valyala/fasthttp"
     6  )
     7  
     8  /*
     9     Creation Time: 2021 - May - 21
    10     Created by:  (ehsan)
    11     Maintainers:
    12        1.  Ehsan N. Moosa (E2)
    13     Auditor: Ehsan N. Moosa (E2)
    14     Copyright Ronak Software Group 2020
    15  */
    16  
    17  type Code string
    18  
    19  // Error Codes
    20  const (
    21  	Internal         Code = "E00" // When Error is Unknown or it is internal and should not be exposed to the client
    22  	Invalid          Code = "E01"
    23  	Unavailable      Code = "E02"
    24  	TooMany          Code = "E03"
    25  	TooFew           Code = "E04"
    26  	Incomplete       Code = "E05"
    27  	Timeout          Code = "E06"
    28  	Access           Code = "E07"
    29  	AlreadyExists    Code = "E08"
    30  	Busy             Code = "E09"
    31  	OutOfRange       Code = "E10"
    32  	PartiallyApplied Code = "E11"
    33  	Expired          Code = "E12"
    34  	NotImplemented   Code = "E13"
    35  )
    36  
    37  func (c Code) Name() string {
    38  	return _codeName[c]
    39  }
    40  
    41  func (c Code) HttpStatus() int {
    42  	code := _httpStatus[c]
    43  	if code != 0 {
    44  		return code
    45  	}
    46  	code = tools.StrToInt(string(c))
    47  	if code != 0 {
    48  		return code
    49  	}
    50  
    51  	return fasthttp.StatusInternalServerError
    52  }
    53  
    54  var _codeName = map[Code]string{
    55  	Internal:         "Internal",
    56  	Invalid:          "Invalid",
    57  	Unavailable:      "Unavailable",
    58  	TooMany:          "TooMany",
    59  	TooFew:           "TooFew",
    60  	Incomplete:       "Incomplete",
    61  	Timeout:          "Timeout",
    62  	Access:           "Access",
    63  	AlreadyExists:    "AlreadyExists",
    64  	Busy:             "Busy",
    65  	OutOfRange:       "OutOfRange",
    66  	PartiallyApplied: "PartiallyApplied",
    67  	Expired:          "Expired",
    68  	NotImplemented:   "NotImplemented",
    69  }
    70  
    71  var _httpStatus = map[Code]int{
    72  	Internal:         fasthttp.StatusInternalServerError,
    73  	Invalid:          fasthttp.StatusBadRequest,
    74  	Unavailable:      fasthttp.StatusNotFound,
    75  	TooMany:          fasthttp.StatusExpectationFailed,
    76  	TooFew:           fasthttp.StatusExpectationFailed,
    77  	Incomplete:       fasthttp.StatusUnprocessableEntity,
    78  	Timeout:          fasthttp.StatusRequestTimeout,
    79  	Access:           fasthttp.StatusForbidden,
    80  	AlreadyExists:    fasthttp.StatusAlreadyReported,
    81  	Busy:             fasthttp.StatusServiceUnavailable,
    82  	OutOfRange:       fasthttp.StatusRequestedRangeNotSatisfiable,
    83  	PartiallyApplied: fasthttp.StatusPartialContent,
    84  	Expired:          fasthttp.StatusExpectationFailed,
    85  	NotImplemented:   fasthttp.StatusNotImplemented,
    86  }