Skip to content

Commit

Permalink
include more dynamic errors in DynamicErrorMapping mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
prakash100198 committed May 19, 2024
1 parent c844788 commit c968ade
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
15 changes: 8 additions & 7 deletions error/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ const (
InvalidValueErrorMsg = "Invalid value"
OperationInProgressErrorMsg = "another operation (install/upgrade/rollback) is in progress"
ForbiddenErrorMsg = "forbidden"
InvalidChartUrlErrorMsg = "invalid chart URL format"
)

// list of internal errors, these errors are easy for the users to understand
const (
InternalClusterUnreachableErrorMsg = "cluster unreachable"
InternalCrdPreconditionErrorMsg = "ensure CRDs are installed first"
InternalArrayStringMismatchErrorMsg = "got array expected string"
InternalInvalidValueErrorMsg = "invalid value in manifest"
InternalOperationInProgressErrorMsg = "another operation (install/upgrade/rollback) is in progress"
)

Expand All @@ -33,8 +29,13 @@ type errorGrpcCodeTuple struct {

var helmErrorInternalErrorMap = map[string]errorGrpcCodeTuple{
ClusterUnreachableErrorMsg: {errorMsg: InternalClusterUnreachableErrorMsg, grpcCode: codes.DeadlineExceeded},
CrdPreconditionErrorMsg: {errorMsg: InternalCrdPreconditionErrorMsg, grpcCode: codes.FailedPrecondition},
ArrayStringMismatchErrorMsg: {errorMsg: InternalArrayStringMismatchErrorMsg, grpcCode: codes.Unknown},
InvalidValueErrorMsg: {errorMsg: InternalInvalidValueErrorMsg, grpcCode: codes.Unknown},
OperationInProgressErrorMsg: {errorMsg: InternalOperationInProgressErrorMsg, grpcCode: codes.FailedPrecondition},
}

var DynamicErrorMapping = map[string]codes.Code{
NotFoundErrorMsg: codes.NotFound,
ForbiddenErrorMsg: codes.PermissionDenied,
InvalidValueErrorMsg: codes.InvalidArgument,
ArrayStringMismatchErrorMsg: codes.InvalidArgument,
CrdPreconditionErrorMsg: codes.FailedPrecondition,
}
14 changes: 7 additions & 7 deletions error/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package error

import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"strings"
)
Expand All @@ -28,12 +27,13 @@ func getInternalErrorForGenericErrorTypes(err error) error {
1. if namespace is not found err is:- namespace "ns1" not found,
2. in case ingress class not found error is of type ingress class: IngressClass.networking.k8s.io "ingress1" not found,
3. when some resource is forbidden then err can be of many formats one of which is:- Unable to continue with install: could not get information about the resource Ingress "prakash-1-prakash-env3-ingress" in namespace "prakash-ns3": ingresses.networking.k8s.io "prakash-1-prakash-env3-ingress" is forbidden...
etc..
*/
var internalError error
if strings.Compare(strings.ToLower(err.Error()), NotFoundErrorMsg) == 0 {
internalError = status.New(codes.NotFound, err.Error()).Err()
} else if strings.Contains(strings.ToLower(err.Error()), ForbiddenErrorMsg) {
internalError = status.New(codes.PermissionDenied, err.Error()).Err()
for errorMsg, code := range DynamicErrorMapping {
if strings.Contains(strings.ToLower(err.Error()), errorMsg) {
return status.New(code, err.Error()).Err()
}
}
return internalError

return nil
}

0 comments on commit c968ade

Please sign in to comment.