diff --git a/src/main/java/com/uber/cadence/internal/compatibility/proto/ErrorMapper.java b/src/main/java/com/uber/cadence/internal/compatibility/proto/ErrorMapper.java new file mode 100644 index 000000000..6e3ed1581 --- /dev/null +++ b/src/main/java/com/uber/cadence/internal/compatibility/proto/ErrorMapper.java @@ -0,0 +1,94 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.uber.cadence.internal.compatibility.proto; + +import com.google.protobuf.Any; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.rpc.Status; +import com.uber.cadence.api.v1.*; +import com.uber.cadence.serviceclient.exceptions.*; +import io.grpc.StatusRuntimeException; +import io.grpc.protobuf.StatusProto; + +public class ErrorMapper { + public static ServiceClientException Error(StatusRuntimeException e) { + + Status status = StatusProto.fromThrowable(e); + if (status == null) { + return new ServiceClientException("empty status", e); + } + + Any detail = Any.getDefaultInstance(); + if (status.getDetailsCount() > 0) { + detail = status.getDetails(0); + } + + try { + switch (e.getStatus().getCode()) { + case PERMISSION_DENIED: + return new AccessDeniedException(e); + case INTERNAL: + return new InternalServiceException(e); + case NOT_FOUND: + if (detail.is(WorkflowExecutionAlreadyCompletedError.class)) { + return new WorkflowExecutionAlreadyCompletedException(e); + } else { + return new EntityNotExistsException(e); + } + case ALREADY_EXISTS: + if (detail.is(CancellationAlreadyRequestedError.class)) { + return new CancellationAlreadyRequestedException(e); + } else if (detail.is(DomainAlreadyExistsError.class)) { + return new DomainAlreadyExistsException(e); + } else if (detail.is(WorkflowExecutionAlreadyStartedError.class)) { + WorkflowExecutionAlreadyStartedError error = + detail.unpack(WorkflowExecutionAlreadyStartedError.class); + return new WorkflowExecutionAlreadyStartedException( + error.getStartRequestId(), error.getRunId()); + } + case DATA_LOSS: + return new InternalDataInconsistencyException(e); + case FAILED_PRECONDITION: + if (detail.is(ClientVersionNotSupportedError.class)) { + ClientVersionNotSupportedError error = + detail.unpack(ClientVersionNotSupportedError.class); + return new ClientVersionNotSupportedException( + error.getFeatureVersion(), error.getClientImpl(), error.getSupportedVersions()); + } else if (detail.is(FeatureNotEnabledError.class)) { + FeatureNotEnabledError error = detail.unpack(FeatureNotEnabledError.class); + return new FeatureNotEnabledException(error.getFeatureFlag()); + } else if (detail.is(DomainNotActiveError.class)) { + DomainNotActiveError error = detail.unpack(DomainNotActiveError.class); + return new DomainNotActiveException( + error.getDomain(), error.getCurrentCluster(), error.getActiveCluster()); + } + case RESOURCE_EXHAUSTED: + if (detail.is(LimitExceededError.class)) { + return new LimitExceededException(e); + } else { + return new ServiceBusyException(e); + } + case UNKNOWN: + default: + return new ServiceClientException(e); + } + } catch (InvalidProtocolBufferException ex) { + return new ServiceClientException(ex); + } + } +} diff --git a/src/main/java/com/uber/cadence/serviceclient/exceptions/AccessDeniedException.java b/src/main/java/com/uber/cadence/serviceclient/exceptions/AccessDeniedException.java new file mode 100644 index 000000000..a1ed1eee0 --- /dev/null +++ b/src/main/java/com/uber/cadence/serviceclient/exceptions/AccessDeniedException.java @@ -0,0 +1,25 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.uber.cadence.serviceclient.exceptions; + +public class AccessDeniedException extends ServiceClientException { + + public AccessDeniedException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/com/uber/cadence/serviceclient/exceptions/CancellationAlreadyRequestedException.java b/src/main/java/com/uber/cadence/serviceclient/exceptions/CancellationAlreadyRequestedException.java new file mode 100644 index 000000000..20be80a90 --- /dev/null +++ b/src/main/java/com/uber/cadence/serviceclient/exceptions/CancellationAlreadyRequestedException.java @@ -0,0 +1,24 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.uber.cadence.serviceclient.exceptions; + +public class CancellationAlreadyRequestedException extends ServiceClientException { + public CancellationAlreadyRequestedException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/com/uber/cadence/serviceclient/exceptions/ClientVersionNotSupportedException.java b/src/main/java/com/uber/cadence/serviceclient/exceptions/ClientVersionNotSupportedException.java new file mode 100644 index 000000000..ab61e11d7 --- /dev/null +++ b/src/main/java/com/uber/cadence/serviceclient/exceptions/ClientVersionNotSupportedException.java @@ -0,0 +1,43 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.uber.cadence.serviceclient.exceptions; + +public class ClientVersionNotSupportedException extends ServiceClientException { + private final String featureVersion; + private final String clientImpl; + private final String supportedVersions; + + public ClientVersionNotSupportedException( + String featureVersion, String clientImpl, String supportedVersions) { + this.featureVersion = featureVersion; + this.clientImpl = clientImpl; + this.supportedVersions = supportedVersions; + } + + public String getFeatureVersion() { + return featureVersion; + } + + public String getClientImpl() { + return clientImpl; + } + + public String getSupportedVersions() { + return supportedVersions; + } +} diff --git a/src/main/java/com/uber/cadence/serviceclient/exceptions/DomainAlreadyExistsException.java b/src/main/java/com/uber/cadence/serviceclient/exceptions/DomainAlreadyExistsException.java new file mode 100644 index 000000000..cddcc40db --- /dev/null +++ b/src/main/java/com/uber/cadence/serviceclient/exceptions/DomainAlreadyExistsException.java @@ -0,0 +1,24 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.uber.cadence.serviceclient.exceptions; + +public class DomainAlreadyExistsException extends ServiceClientException { + public DomainAlreadyExistsException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/com/uber/cadence/serviceclient/exceptions/DomainNotActiveException.java b/src/main/java/com/uber/cadence/serviceclient/exceptions/DomainNotActiveException.java new file mode 100644 index 000000000..002bb471f --- /dev/null +++ b/src/main/java/com/uber/cadence/serviceclient/exceptions/DomainNotActiveException.java @@ -0,0 +1,42 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.uber.cadence.serviceclient.exceptions; + +public class DomainNotActiveException extends ServiceClientException { + private final String domain; + private final String currentCluster; + private final String activeCluster; + + public DomainNotActiveException(String domain, String currentCluster, String activeCluster) { + this.domain = domain; + this.currentCluster = currentCluster; + this.activeCluster = activeCluster; + } + + public String getDomain() { + return domain; + } + + public String getCurrentCluster() { + return currentCluster; + } + + public String getActiveCluster() { + return activeCluster; + } +} diff --git a/src/main/java/com/uber/cadence/serviceclient/exceptions/EntityNotExistsException.java b/src/main/java/com/uber/cadence/serviceclient/exceptions/EntityNotExistsException.java new file mode 100644 index 000000000..9553a0d8e --- /dev/null +++ b/src/main/java/com/uber/cadence/serviceclient/exceptions/EntityNotExistsException.java @@ -0,0 +1,25 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.uber.cadence.serviceclient.exceptions; + +public class EntityNotExistsException extends ServiceClientException { + + public EntityNotExistsException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/com/uber/cadence/serviceclient/exceptions/FeatureNotEnabledException.java b/src/main/java/com/uber/cadence/serviceclient/exceptions/FeatureNotEnabledException.java new file mode 100644 index 000000000..f8172b626 --- /dev/null +++ b/src/main/java/com/uber/cadence/serviceclient/exceptions/FeatureNotEnabledException.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.uber.cadence.serviceclient.exceptions; + +public class FeatureNotEnabledException extends ServiceClientException { + private final String featureFlag; + + public FeatureNotEnabledException(String featureFlag) { + this.featureFlag = featureFlag; + } + + public String getFeatureFlag() { + return featureFlag; + } +} diff --git a/src/main/java/com/uber/cadence/serviceclient/exceptions/InternalDataInconsistencyException.java b/src/main/java/com/uber/cadence/serviceclient/exceptions/InternalDataInconsistencyException.java new file mode 100644 index 000000000..79b679639 --- /dev/null +++ b/src/main/java/com/uber/cadence/serviceclient/exceptions/InternalDataInconsistencyException.java @@ -0,0 +1,24 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.uber.cadence.serviceclient.exceptions; + +public class InternalDataInconsistencyException extends ServiceClientException { + public InternalDataInconsistencyException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/com/uber/cadence/serviceclient/exceptions/InternalServiceException.java b/src/main/java/com/uber/cadence/serviceclient/exceptions/InternalServiceException.java new file mode 100644 index 000000000..17915be6b --- /dev/null +++ b/src/main/java/com/uber/cadence/serviceclient/exceptions/InternalServiceException.java @@ -0,0 +1,24 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.uber.cadence.serviceclient.exceptions; + +public class InternalServiceException extends ServiceClientException { + public InternalServiceException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/com/uber/cadence/serviceclient/exceptions/LimitExceededException.java b/src/main/java/com/uber/cadence/serviceclient/exceptions/LimitExceededException.java new file mode 100644 index 000000000..112150646 --- /dev/null +++ b/src/main/java/com/uber/cadence/serviceclient/exceptions/LimitExceededException.java @@ -0,0 +1,24 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.uber.cadence.serviceclient.exceptions; + +public class LimitExceededException extends ServiceClientException { + public LimitExceededException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/com/uber/cadence/serviceclient/exceptions/ServiceBusyException.java b/src/main/java/com/uber/cadence/serviceclient/exceptions/ServiceBusyException.java new file mode 100644 index 000000000..1cf7cf65b --- /dev/null +++ b/src/main/java/com/uber/cadence/serviceclient/exceptions/ServiceBusyException.java @@ -0,0 +1,24 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.uber.cadence.serviceclient.exceptions; + +public class ServiceBusyException extends ServiceClientException { + public ServiceBusyException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/com/uber/cadence/serviceclient/exceptions/ServiceClientException.java b/src/main/java/com/uber/cadence/serviceclient/exceptions/ServiceClientException.java new file mode 100644 index 000000000..f28de9469 --- /dev/null +++ b/src/main/java/com/uber/cadence/serviceclient/exceptions/ServiceClientException.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.uber.cadence.serviceclient.exceptions; + +/** + * Base class for all exceptions thrown by the service client. + * + *

This is a catchall for all other errors. + */ +public class ServiceClientException extends RuntimeException { + ServiceClientException() { + super(); + } + + public ServiceClientException(String message, Throwable cause) { + super(message, cause); + } + + public ServiceClientException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/com/uber/cadence/serviceclient/exceptions/WorkflowExecutionAlreadyCompletedException.java b/src/main/java/com/uber/cadence/serviceclient/exceptions/WorkflowExecutionAlreadyCompletedException.java new file mode 100644 index 000000000..72f70515a --- /dev/null +++ b/src/main/java/com/uber/cadence/serviceclient/exceptions/WorkflowExecutionAlreadyCompletedException.java @@ -0,0 +1,23 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package com.uber.cadence.serviceclient.exceptions; + +public class WorkflowExecutionAlreadyCompletedException extends ServiceClientException { + public WorkflowExecutionAlreadyCompletedException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/com/uber/cadence/serviceclient/exceptions/WorkflowExecutionAlreadyStartedException.java b/src/main/java/com/uber/cadence/serviceclient/exceptions/WorkflowExecutionAlreadyStartedException.java new file mode 100644 index 000000000..2dbbfecb5 --- /dev/null +++ b/src/main/java/com/uber/cadence/serviceclient/exceptions/WorkflowExecutionAlreadyStartedException.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package com.uber.cadence.serviceclient.exceptions; + +public class WorkflowExecutionAlreadyStartedException extends ServiceClientException { + private final String startRequestId; + private final String runId; + + public WorkflowExecutionAlreadyStartedException(String startRequestId, String runId) { + this.startRequestId = startRequestId; + this.runId = runId; + } + + public String getStartRequestId() { + return startRequestId; + } + + public String getRunId() { + return runId; + } +} diff --git a/src/test/java/com/uber/cadence/converter/JsonDataConverterTest.java b/src/test/java/com/uber/cadence/converter/JsonDataConverterTest.java index 20743414a..155c1b860 100644 --- a/src/test/java/com/uber/cadence/converter/JsonDataConverterTest.java +++ b/src/test/java/com/uber/cadence/converter/JsonDataConverterTest.java @@ -245,8 +245,6 @@ public NonSerializableException(Throwable cause) { } } - // TODO flaky test in local env: expected: but - // was: @Test public void testException() { RuntimeException rootException = new RuntimeException("root exception"); @@ -263,7 +261,9 @@ public void testException() { assertNotNull(causeFromConverted); assertEquals(DataConverterException.class, causeFromConverted.getClass()); assertNotNull(causeFromConverted.getCause()); - assertEquals(JsonIOException.class, causeFromConverted.getCause().getClass()); + Class causeClass = causeFromConverted.getCause().getClass(); + // it depends on the gson implementation on different runtimes + assertTrue(causeClass == JsonIOException.class || causeClass == StackOverflowError.class); assertNotNull(causeFromConverted.getSuppressed()); assertEquals(1, causeFromConverted.getSuppressed().length); diff --git a/src/test/java/com/uber/cadence/internal/compatibility/proto/ErrorMapperTest.java b/src/test/java/com/uber/cadence/internal/compatibility/proto/ErrorMapperTest.java new file mode 100644 index 000000000..8720efb7d --- /dev/null +++ b/src/test/java/com/uber/cadence/internal/compatibility/proto/ErrorMapperTest.java @@ -0,0 +1,139 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.uber.cadence.internal.compatibility.proto; + +import static org.junit.Assert.assertEquals; + +import com.google.protobuf.Any; +import com.google.protobuf.Message; +import com.uber.cadence.api.v1.*; +import com.uber.cadence.serviceclient.exceptions.*; +import io.grpc.Status; +import io.grpc.StatusRuntimeException; +import io.grpc.protobuf.StatusProto; +import java.util.Arrays; +import java.util.Collection; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class ErrorMapperTest { + + @Parameterized.Parameter(0) + public Status status; + + @Parameterized.Parameter(1) + public Message detail; + + @Parameterized.Parameter(2) + public Class expectedException; + + @Parameterized.Parameters + public static Collection data() { + Object[][] data = + new Object[][] { + {Status.PERMISSION_DENIED, null, AccessDeniedException.class}, + {Status.INTERNAL, null, InternalServiceException.class}, + {Status.NOT_FOUND, null, EntityNotExistsException.class}, + { + Status.ALREADY_EXISTS, + DomainAlreadyExistsError.getDefaultInstance(), + DomainAlreadyExistsException.class + }, + { + Status.FAILED_PRECONDITION, + FeatureNotEnabledError.getDefaultInstance(), + FeatureNotEnabledException.class + }, + { + Status.RESOURCE_EXHAUSTED, + LimitExceededError.getDefaultInstance(), + LimitExceededException.class + }, + {Status.UNKNOWN, null, ServiceClientException.class}, + { + Status.NOT_FOUND, + WorkflowExecutionAlreadyCompletedError.getDefaultInstance(), + WorkflowExecutionAlreadyCompletedException.class + }, + { + Status.ALREADY_EXISTS, + WorkflowExecutionAlreadyStartedError.getDefaultInstance(), + WorkflowExecutionAlreadyStartedException.class + }, + { + Status.FAILED_PRECONDITION, + DomainNotActiveError.getDefaultInstance(), + DomainNotActiveException.class + }, + { + Status.FAILED_PRECONDITION, + ClientVersionNotSupportedError.getDefaultInstance(), + ClientVersionNotSupportedException.class + }, + { + Status.FAILED_PRECONDITION, + FeatureNotEnabledError.getDefaultInstance(), + FeatureNotEnabledException.class + }, + { + Status.FAILED_PRECONDITION, + DomainNotActiveError.getDefaultInstance(), + DomainNotActiveException.class + }, + { + Status.FAILED_PRECONDITION, + ClientVersionNotSupportedError.getDefaultInstance(), + ClientVersionNotSupportedException.class + }, + { + Status.FAILED_PRECONDITION, + FeatureNotEnabledError.getDefaultInstance(), + FeatureNotEnabledException.class + }, + { + Status.RESOURCE_EXHAUSTED, + LimitExceededError.getDefaultInstance(), + LimitExceededException.class + }, + {Status.DATA_LOSS, null, InternalDataInconsistencyException.class}, + { + Status.RESOURCE_EXHAUSTED, + ServiceBusyError.getDefaultInstance(), + ServiceBusyException.class + }, + {Status.INTERNAL, null, InternalServiceException.class} + }; + return Arrays.asList(data); + } + + @Test + public void testErrorMapper() { + com.google.rpc.Status.Builder builder = + com.google.rpc.Status.newBuilder().setCode(status.getCode().value()); + + if (detail != null) { + builder.addDetails(Any.pack(detail)); + } + + StatusRuntimeException ex = StatusProto.toStatusRuntimeException(builder.build()); + ServiceClientException result = ErrorMapper.Error(ex); + assertEquals(expectedException, result.getClass()); + } +}