2023-08-18 15:51:11 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-08-18 15:51:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func ZitadelErrorToHTTPStatusCode(err error) (statusCode int, ok bool) {
|
|
|
|
if err == nil {
|
|
|
|
return http.StatusOK, true
|
|
|
|
}
|
|
|
|
//nolint:errorlint
|
|
|
|
switch err.(type) {
|
2023-12-08 16:30:55 +02:00
|
|
|
case *zerrors.AlreadyExistsError:
|
2023-08-18 15:51:11 +02:00
|
|
|
return http.StatusConflict, true
|
2023-12-08 16:30:55 +02:00
|
|
|
case *zerrors.DeadlineExceededError:
|
2023-08-18 15:51:11 +02:00
|
|
|
return http.StatusGatewayTimeout, true
|
2023-12-08 16:30:55 +02:00
|
|
|
case *zerrors.InternalError:
|
2023-08-18 15:51:11 +02:00
|
|
|
return http.StatusInternalServerError, true
|
2023-12-08 16:30:55 +02:00
|
|
|
case *zerrors.InvalidArgumentError:
|
2023-08-18 15:51:11 +02:00
|
|
|
return http.StatusBadRequest, true
|
2023-12-08 16:30:55 +02:00
|
|
|
case *zerrors.NotFoundError:
|
2023-08-18 15:51:11 +02:00
|
|
|
return http.StatusNotFound, true
|
2023-12-08 16:30:55 +02:00
|
|
|
case *zerrors.PermissionDeniedError:
|
2023-08-18 15:51:11 +02:00
|
|
|
return http.StatusForbidden, true
|
2023-12-08 16:30:55 +02:00
|
|
|
case *zerrors.PreconditionFailedError:
|
2023-08-18 15:51:11 +02:00
|
|
|
// use the same code as grpc-gateway:
|
|
|
|
// https://github.com/grpc-ecosystem/grpc-gateway/blob/9e33e38f15cb7d2f11096366e62ea391a3459ba9/runtime/errors.go#L59
|
|
|
|
return http.StatusBadRequest, true
|
2023-12-08 16:30:55 +02:00
|
|
|
case *zerrors.UnauthenticatedError:
|
2023-08-18 15:51:11 +02:00
|
|
|
return http.StatusUnauthorized, true
|
2023-12-08 16:30:55 +02:00
|
|
|
case *zerrors.UnavailableError:
|
2023-08-18 15:51:11 +02:00
|
|
|
return http.StatusServiceUnavailable, true
|
2023-12-08 16:30:55 +02:00
|
|
|
case *zerrors.UnimplementedError:
|
2023-08-18 15:51:11 +02:00
|
|
|
return http.StatusNotImplemented, true
|
2023-12-08 16:30:55 +02:00
|
|
|
case *zerrors.ResourceExhaustedError:
|
2023-08-18 15:51:11 +02:00
|
|
|
return http.StatusTooManyRequests, true
|
|
|
|
default:
|
2023-12-08 16:30:55 +02:00
|
|
|
c := new(zerrors.ZitadelError)
|
2023-08-18 15:51:11 +02:00
|
|
|
if errors.As(err, &c) {
|
|
|
|
return ZitadelErrorToHTTPStatusCode(errors.Unwrap(err))
|
|
|
|
}
|
|
|
|
return http.StatusInternalServerError, false
|
|
|
|
}
|
|
|
|
}
|