go.uber.org/cadence@v1.2.9/internal/compatibility/proto/error.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package proto
    22  
    23  import (
    24  	apiv1 "github.com/uber/cadence-idl/go/proto/api/v1"
    25  	"go.uber.org/yarpc/encoding/protobuf"
    26  	"go.uber.org/yarpc/yarpcerrors"
    27  
    28  	"go.uber.org/cadence/.gen/go/shared"
    29  )
    30  
    31  func Error(err error) error {
    32  	if err == nil {
    33  		return protobuf.NewError(yarpcerrors.CodeOK, "")
    34  	}
    35  
    36  	switch e := err.(type) {
    37  	case *shared.AccessDeniedError:
    38  		return protobuf.NewError(yarpcerrors.CodePermissionDenied, e.Message)
    39  	case *shared.InternalServiceError:
    40  		return protobuf.NewError(yarpcerrors.CodeInternal, e.Message)
    41  	case *shared.EntityNotExistsError:
    42  		return protobuf.NewError(yarpcerrors.CodeNotFound, e.Message, protobuf.WithErrorDetails(&apiv1.EntityNotExistsError{
    43  			CurrentCluster: e.GetCurrentCluster(),
    44  			ActiveCluster:  e.GetActiveCluster(),
    45  		}))
    46  	case *shared.WorkflowExecutionAlreadyCompletedError:
    47  		return protobuf.NewError(yarpcerrors.CodeNotFound, e.Message, protobuf.WithErrorDetails(&apiv1.WorkflowExecutionAlreadyCompletedError{}))
    48  	case *shared.BadRequestError:
    49  		return protobuf.NewError(yarpcerrors.CodeInvalidArgument, e.Message)
    50  	case *shared.QueryFailedError:
    51  		return protobuf.NewError(yarpcerrors.CodeInvalidArgument, e.Message, protobuf.WithErrorDetails(&apiv1.QueryFailedError{}))
    52  
    53  	case *shared.CancellationAlreadyRequestedError:
    54  		return protobuf.NewError(yarpcerrors.CodeAlreadyExists, e.Message, protobuf.WithErrorDetails(&apiv1.CancellationAlreadyRequestedError{}))
    55  	case *shared.DomainAlreadyExistsError:
    56  		return protobuf.NewError(yarpcerrors.CodeAlreadyExists, e.Message, protobuf.WithErrorDetails(&apiv1.DomainAlreadyExistsError{}))
    57  	case *shared.WorkflowExecutionAlreadyStartedError:
    58  		return protobuf.NewError(yarpcerrors.CodeAlreadyExists, e.GetMessage(), protobuf.WithErrorDetails(&apiv1.WorkflowExecutionAlreadyStartedError{
    59  			StartRequestId: e.GetStartRequestId(),
    60  			RunId:          e.GetRunId(),
    61  		}))
    62  	case *shared.ClientVersionNotSupportedError:
    63  		return protobuf.NewError(yarpcerrors.CodeFailedPrecondition, "Client version not supported", protobuf.WithErrorDetails(&apiv1.ClientVersionNotSupportedError{
    64  			FeatureVersion:    e.FeatureVersion,
    65  			ClientImpl:        e.ClientImpl,
    66  			SupportedVersions: e.SupportedVersions,
    67  		}))
    68  	case *shared.FeatureNotEnabledError:
    69  		return protobuf.NewError(yarpcerrors.CodeFailedPrecondition, "Feature flag not enabled", protobuf.WithErrorDetails(&apiv1.FeatureNotEnabledError{
    70  			FeatureFlag: e.FeatureFlag,
    71  		}))
    72  	case *shared.DomainNotActiveError:
    73  		return protobuf.NewError(yarpcerrors.CodeFailedPrecondition, e.Message, protobuf.WithErrorDetails(&apiv1.DomainNotActiveError{
    74  			Domain:         e.DomainName,
    75  			CurrentCluster: e.CurrentCluster,
    76  			ActiveCluster:  e.ActiveCluster,
    77  		}))
    78  	case *shared.LimitExceededError:
    79  		return protobuf.NewError(yarpcerrors.CodeResourceExhausted, e.Message, protobuf.WithErrorDetails(&apiv1.LimitExceededError{}))
    80  	case *shared.ServiceBusyError:
    81  		return protobuf.NewError(yarpcerrors.CodeResourceExhausted, e.Message, protobuf.WithErrorDetails(&apiv1.ServiceBusyError{}))
    82  	}
    83  
    84  	return protobuf.NewError(yarpcerrors.CodeUnknown, err.Error())
    85  }