mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-15 04:18:01 +00:00
f680dd934d
* chore: rename package errors to zerrors * rename package errors to gerrors * fix error related linting issues * fix zitadel error assertion * fix gosimple linting issues * fix deprecated linting issues * resolve gci linting issues * fix import structure --------- Co-authored-by: Elio Bischof <elio@zitadel.com>
48 lines
1.4 KiB
Go
48 lines
1.4 KiB
Go
package http
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
func ZitadelErrorToHTTPStatusCode(err error) (statusCode int, ok bool) {
|
|
if err == nil {
|
|
return http.StatusOK, true
|
|
}
|
|
//nolint:errorlint
|
|
switch err.(type) {
|
|
case *zerrors.AlreadyExistsError:
|
|
return http.StatusConflict, true
|
|
case *zerrors.DeadlineExceededError:
|
|
return http.StatusGatewayTimeout, true
|
|
case *zerrors.InternalError:
|
|
return http.StatusInternalServerError, true
|
|
case *zerrors.InvalidArgumentError:
|
|
return http.StatusBadRequest, true
|
|
case *zerrors.NotFoundError:
|
|
return http.StatusNotFound, true
|
|
case *zerrors.PermissionDeniedError:
|
|
return http.StatusForbidden, true
|
|
case *zerrors.PreconditionFailedError:
|
|
// use the same code as grpc-gateway:
|
|
// https://github.com/grpc-ecosystem/grpc-gateway/blob/9e33e38f15cb7d2f11096366e62ea391a3459ba9/runtime/errors.go#L59
|
|
return http.StatusBadRequest, true
|
|
case *zerrors.UnauthenticatedError:
|
|
return http.StatusUnauthorized, true
|
|
case *zerrors.UnavailableError:
|
|
return http.StatusServiceUnavailable, true
|
|
case *zerrors.UnimplementedError:
|
|
return http.StatusNotImplemented, true
|
|
case *zerrors.ResourceExhaustedError:
|
|
return http.StatusTooManyRequests, true
|
|
default:
|
|
c := new(zerrors.ZitadelError)
|
|
if errors.As(err, &c) {
|
|
return ZitadelErrorToHTTPStatusCode(errors.Unwrap(err))
|
|
}
|
|
return http.StatusInternalServerError, false
|
|
}
|
|
}
|