mirror of
https://github.com/kataras/iris.git
synced 2026-01-25 21:05:56 +00:00
add x/errors.OK, Create, NoContent and NoContentOrNotModified package-level generic functions as custom service helpers
This commit is contained in:
@@ -48,9 +48,12 @@ type (
|
||||
// A read-only map of valid http error codes.
|
||||
var errorCodeMap = make(map[ErrorCodeName]ErrorCode)
|
||||
|
||||
// E registers a custom HTTP Error and returns its canonical name for future use.
|
||||
// Deprecated: Use Register instead.
|
||||
var E = Register
|
||||
|
||||
// Register registers a custom HTTP Error and returns its canonical name for future use.
|
||||
// The method "New" is reserved and was kept as it is for compatibility
|
||||
// with the standard errors package, therefore the "E" name was chosen instead.
|
||||
// with the standard errors package, therefore the "Register" name was chosen instead.
|
||||
// The key stroke "e" is near and accessible while typing the "errors" word
|
||||
// so developers may find it easy to use.
|
||||
//
|
||||
@@ -59,14 +62,14 @@ var errorCodeMap = make(map[ErrorCodeName]ErrorCode)
|
||||
// Example:
|
||||
//
|
||||
// var (
|
||||
// NotFound = errors.E("NOT_FOUND", http.StatusNotFound)
|
||||
// NotFound = errors.Register("NOT_FOUND", http.StatusNotFound)
|
||||
// )
|
||||
// ...
|
||||
// NotFound.Details(ctx, "resource not found", "user with id: %q was not found", userID)
|
||||
//
|
||||
// This method MUST be called on initialization, before HTTP server starts as
|
||||
// the internal map is not protected by mutex.
|
||||
func E(httpErrorCanonicalName string, httpStatusCode int) ErrorCodeName {
|
||||
func Register(httpErrorCanonicalName string, httpStatusCode int) ErrorCodeName {
|
||||
canonicalName := ErrorCodeName(httpErrorCanonicalName)
|
||||
RegisterErrorCode(canonicalName, httpStatusCode)
|
||||
return canonicalName
|
||||
@@ -99,22 +102,22 @@ func RegisterErrorCodeMap(errorMap map[ErrorCodeName]int) {
|
||||
|
||||
// List of default error codes a server should follow and send back to the client.
|
||||
var (
|
||||
Cancelled ErrorCodeName = E("CANCELLED", context.StatusTokenRequired)
|
||||
Unknown ErrorCodeName = E("UNKNOWN", http.StatusInternalServerError)
|
||||
InvalidArgument ErrorCodeName = E("INVALID_ARGUMENT", http.StatusBadRequest)
|
||||
DeadlineExceeded ErrorCodeName = E("DEADLINE_EXCEEDED", http.StatusGatewayTimeout)
|
||||
NotFound ErrorCodeName = E("NOT_FOUND", http.StatusNotFound)
|
||||
AlreadyExists ErrorCodeName = E("ALREADY_EXISTS", http.StatusConflict)
|
||||
PermissionDenied ErrorCodeName = E("PERMISSION_DENIED", http.StatusForbidden)
|
||||
Unauthenticated ErrorCodeName = E("UNAUTHENTICATED", http.StatusUnauthorized)
|
||||
ResourceExhausted ErrorCodeName = E("RESOURCE_EXHAUSTED", http.StatusTooManyRequests)
|
||||
FailedPrecondition ErrorCodeName = E("FAILED_PRECONDITION", http.StatusBadRequest)
|
||||
Aborted ErrorCodeName = E("ABORTED", http.StatusConflict)
|
||||
OutOfRange ErrorCodeName = E("OUT_OF_RANGE", http.StatusBadRequest)
|
||||
Unimplemented ErrorCodeName = E("UNIMPLEMENTED", http.StatusNotImplemented)
|
||||
Internal ErrorCodeName = E("INTERNAL", http.StatusInternalServerError)
|
||||
Unavailable ErrorCodeName = E("UNAVAILABLE", http.StatusServiceUnavailable)
|
||||
DataLoss ErrorCodeName = E("DATA_LOSS", http.StatusInternalServerError)
|
||||
Cancelled ErrorCodeName = Register("CANCELLED", context.StatusTokenRequired)
|
||||
Unknown ErrorCodeName = Register("UNKNOWN", http.StatusInternalServerError)
|
||||
InvalidArgument ErrorCodeName = Register("INVALID_ARGUMENT", http.StatusBadRequest)
|
||||
DeadlineExceeded ErrorCodeName = Register("DEADLINE_EXCEEDED", http.StatusGatewayTimeout)
|
||||
NotFound ErrorCodeName = Register("NOT_FOUND", http.StatusNotFound)
|
||||
AlreadyExists ErrorCodeName = Register("ALREADY_EXISTS", http.StatusConflict)
|
||||
PermissionDenied ErrorCodeName = Register("PERMISSION_DENIED", http.StatusForbidden)
|
||||
Unauthenticated ErrorCodeName = Register("UNAUTHENTICATED", http.StatusUnauthorized)
|
||||
ResourceExhausted ErrorCodeName = Register("RESOURCE_EXHAUSTED", http.StatusTooManyRequests)
|
||||
FailedPrecondition ErrorCodeName = Register("FAILED_PRECONDITION", http.StatusBadRequest)
|
||||
Aborted ErrorCodeName = Register("ABORTED", http.StatusConflict)
|
||||
OutOfRange ErrorCodeName = Register("OUT_OF_RANGE", http.StatusBadRequest)
|
||||
Unimplemented ErrorCodeName = Register("UNIMPLEMENTED", http.StatusNotImplemented)
|
||||
Internal ErrorCodeName = Register("INTERNAL", http.StatusInternalServerError)
|
||||
Unavailable ErrorCodeName = Register("UNAVAILABLE", http.StatusServiceUnavailable)
|
||||
DataLoss ErrorCodeName = Register("DATA_LOSS", http.StatusInternalServerError)
|
||||
)
|
||||
|
||||
// errorFuncCodeMap is a read-only map of error code names and their error functions.
|
||||
@@ -352,7 +355,7 @@ var (
|
||||
// The server fails to send an error on two cases:
|
||||
// 1. when the provided error code name is not registered (the error value is the ErrUnexpectedErrorCode)
|
||||
// 2. when the error contains data but cannot be encoded to json (the value of the error is the result error of json.Marshal).
|
||||
ErrUnexpected = E("UNEXPECTED_ERROR", http.StatusInternalServerError)
|
||||
ErrUnexpected = Register("UNEXPECTED_ERROR", http.StatusInternalServerError)
|
||||
// ErrUnexpectedErrorCode is the error which logged
|
||||
// when the given error code name is not registered.
|
||||
ErrUnexpectedErrorCode = New("unexpected error code name")
|
||||
|
||||
Reference in New Issue
Block a user