func FromError()

in common/types/mapper/proto/errors.go [34:112]


func FromError(err error) error {
	if err == nil {
		return protobuf.NewError(yarpcerrors.CodeOK, "")
	}

	switch e := err.(type) {
	case *types.AccessDeniedError:
		return protobuf.NewError(yarpcerrors.CodePermissionDenied, e.Message)
	case *types.InternalServiceError:
		return protobuf.NewError(yarpcerrors.CodeInternal, e.Message)
	case *types.EntityNotExistsError:
		return protobuf.NewError(yarpcerrors.CodeNotFound, e.Message, protobuf.WithErrorDetails(&apiv1.EntityNotExistsError{
			CurrentCluster: e.CurrentCluster,
			ActiveCluster:  e.ActiveCluster,
		}))
	case *types.WorkflowExecutionAlreadyCompletedError:
		return protobuf.NewError(yarpcerrors.CodeNotFound, e.Message, protobuf.WithErrorDetails(&apiv1.WorkflowExecutionAlreadyCompletedError{}))
	case *types.BadRequestError:
		return protobuf.NewError(yarpcerrors.CodeInvalidArgument, e.Message)
	case *types.QueryFailedError:
		return protobuf.NewError(yarpcerrors.CodeInvalidArgument, e.Message, protobuf.WithErrorDetails(&apiv1.QueryFailedError{}))
	case *types.ShardOwnershipLostError:
		return protobuf.NewError(yarpcerrors.CodeAborted, e.Message, protobuf.WithErrorDetails(&sharedv1.ShardOwnershipLostError{
			Owner: e.Owner,
		}))
	case *types.CurrentBranchChangedError:
		return protobuf.NewError(yarpcerrors.CodeAborted, e.Message, protobuf.WithErrorDetails(&sharedv1.CurrentBranchChangedError{
			CurrentBranchToken: e.GetCurrentBranchToken(),
		}))
	case *types.RetryTaskV2Error:
		return protobuf.NewError(yarpcerrors.CodeAborted, e.Message, protobuf.WithErrorDetails(&sharedv1.RetryTaskV2Error{
			DomainId:          e.DomainID,
			WorkflowExecution: FromWorkflowRunPair(e.WorkflowID, e.RunID),
			StartEvent:        FromEventIDVersionPair(e.StartEventID, e.StartEventVersion),
			EndEvent:          FromEventIDVersionPair(e.EndEventID, e.EndEventVersion),
		}))
	case *types.CancellationAlreadyRequestedError:
		return protobuf.NewError(yarpcerrors.CodeAlreadyExists, e.Message, protobuf.WithErrorDetails(&apiv1.CancellationAlreadyRequestedError{}))
	case *types.DomainAlreadyExistsError:
		return protobuf.NewError(yarpcerrors.CodeAlreadyExists, e.Message, protobuf.WithErrorDetails(&apiv1.DomainAlreadyExistsError{}))
	case *types.EventAlreadyStartedError:
		return protobuf.NewError(yarpcerrors.CodeAlreadyExists, e.Message, protobuf.WithErrorDetails(&sharedv1.EventAlreadyStartedError{}))
	case *types.WorkflowExecutionAlreadyStartedError:
		return protobuf.NewError(yarpcerrors.CodeAlreadyExists, e.Message, protobuf.WithErrorDetails(&apiv1.WorkflowExecutionAlreadyStartedError{
			StartRequestId: e.StartRequestID,
			RunId:          e.RunID,
		}))
	case *types.ClientVersionNotSupportedError:
		return protobuf.NewError(yarpcerrors.CodeFailedPrecondition, "Client version not supported", protobuf.WithErrorDetails(&apiv1.ClientVersionNotSupportedError{
			FeatureVersion:    e.FeatureVersion,
			ClientImpl:        e.ClientImpl,
			SupportedVersions: e.SupportedVersions,
		}))
	case *types.FeatureNotEnabledError:
		return protobuf.NewError(yarpcerrors.CodeFailedPrecondition, "Feature flag not enabled", protobuf.WithErrorDetails(&apiv1.FeatureNotEnabledError{
			FeatureFlag: e.FeatureFlag,
		}))
	case *types.DomainNotActiveError:
		return protobuf.NewError(yarpcerrors.CodeFailedPrecondition, e.Message, protobuf.WithErrorDetails(&apiv1.DomainNotActiveError{
			Domain:         e.DomainName,
			CurrentCluster: e.CurrentCluster,
			ActiveCluster:  e.ActiveCluster,
		}))
	case *types.InternalDataInconsistencyError:
		return protobuf.NewError(yarpcerrors.CodeDataLoss, e.Message, protobuf.WithErrorDetails(&sharedv1.InternalDataInconsistencyError{}))
	case *types.LimitExceededError:
		return protobuf.NewError(yarpcerrors.CodeResourceExhausted, e.Message, protobuf.WithErrorDetails(&apiv1.LimitExceededError{}))
	case *types.ServiceBusyError:
		return protobuf.NewError(yarpcerrors.CodeResourceExhausted, e.Message, protobuf.WithErrorDetails(&apiv1.ServiceBusyError{
			Reason: e.Reason,
		}))
	case *types.RemoteSyncMatchedError:
		return protobuf.NewError(yarpcerrors.CodeUnavailable, e.Message, protobuf.WithErrorDetails(&sharedv1.RemoteSyncMatchedError{}))
	case *types.StickyWorkerUnavailableError:
		return protobuf.NewError(yarpcerrors.CodeUnavailable, e.Message, protobuf.WithErrorDetails(&apiv1.StickyWorkerUnavailableError{}))
	}

	return protobuf.NewError(yarpcerrors.CodeUnknown, err.Error())
}