Skip to content

Commit adcc039

Browse files
authored
add serviceclient exceptions (#989)
What changed? added all exceptions to match thrift exceptions added ErrorMapper which maps StatusRuntimeException to ServiceClientException Why? thrift exceptions are used across Java SDK. It's easier to abstract the protocol level error handling by introducing internal exception layer
1 parent 8d0137a commit adcc039

17 files changed

+640
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.internal.compatibility.proto;
19+
20+
import com.google.protobuf.Any;
21+
import com.google.protobuf.InvalidProtocolBufferException;
22+
import com.google.rpc.Status;
23+
import com.uber.cadence.api.v1.*;
24+
import com.uber.cadence.serviceclient.exceptions.*;
25+
import io.grpc.StatusRuntimeException;
26+
import io.grpc.protobuf.StatusProto;
27+
28+
public class ErrorMapper {
29+
public static ServiceClientException Error(StatusRuntimeException e) {
30+
31+
Status status = StatusProto.fromThrowable(e);
32+
if (status == null) {
33+
return new ServiceClientException("empty status", e);
34+
}
35+
36+
Any detail = Any.getDefaultInstance();
37+
if (status.getDetailsCount() > 0) {
38+
detail = status.getDetails(0);
39+
}
40+
41+
try {
42+
switch (e.getStatus().getCode()) {
43+
case PERMISSION_DENIED:
44+
return new AccessDeniedException(e);
45+
case INTERNAL:
46+
return new InternalServiceException(e);
47+
case NOT_FOUND:
48+
if (detail.is(WorkflowExecutionAlreadyCompletedError.class)) {
49+
return new WorkflowExecutionAlreadyCompletedException(e);
50+
} else {
51+
return new EntityNotExistsException(e);
52+
}
53+
case ALREADY_EXISTS:
54+
if (detail.is(CancellationAlreadyRequestedError.class)) {
55+
return new CancellationAlreadyRequestedException(e);
56+
} else if (detail.is(DomainAlreadyExistsError.class)) {
57+
return new DomainAlreadyExistsException(e);
58+
} else if (detail.is(WorkflowExecutionAlreadyStartedError.class)) {
59+
WorkflowExecutionAlreadyStartedError error =
60+
detail.unpack(WorkflowExecutionAlreadyStartedError.class);
61+
return new WorkflowExecutionAlreadyStartedException(
62+
error.getStartRequestId(), error.getRunId());
63+
}
64+
case DATA_LOSS:
65+
return new InternalDataInconsistencyException(e);
66+
case FAILED_PRECONDITION:
67+
if (detail.is(ClientVersionNotSupportedError.class)) {
68+
ClientVersionNotSupportedError error =
69+
detail.unpack(ClientVersionNotSupportedError.class);
70+
return new ClientVersionNotSupportedException(
71+
error.getFeatureVersion(), error.getClientImpl(), error.getSupportedVersions());
72+
} else if (detail.is(FeatureNotEnabledError.class)) {
73+
FeatureNotEnabledError error = detail.unpack(FeatureNotEnabledError.class);
74+
return new FeatureNotEnabledException(error.getFeatureFlag());
75+
} else if (detail.is(DomainNotActiveError.class)) {
76+
DomainNotActiveError error = detail.unpack(DomainNotActiveError.class);
77+
return new DomainNotActiveException(
78+
error.getDomain(), error.getCurrentCluster(), error.getActiveCluster());
79+
}
80+
case RESOURCE_EXHAUSTED:
81+
if (detail.is(LimitExceededError.class)) {
82+
return new LimitExceededException(e);
83+
} else {
84+
return new ServiceBusyException(e);
85+
}
86+
case UNKNOWN:
87+
default:
88+
return new ServiceClientException(e);
89+
}
90+
} catch (InvalidProtocolBufferException ex) {
91+
return new ServiceClientException(ex);
92+
}
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.serviceclient.exceptions;
19+
20+
public class AccessDeniedException extends ServiceClientException {
21+
22+
public AccessDeniedException(Throwable cause) {
23+
super(cause);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.serviceclient.exceptions;
19+
20+
public class CancellationAlreadyRequestedException extends ServiceClientException {
21+
public CancellationAlreadyRequestedException(Throwable cause) {
22+
super(cause);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.serviceclient.exceptions;
19+
20+
public class ClientVersionNotSupportedException extends ServiceClientException {
21+
private final String featureVersion;
22+
private final String clientImpl;
23+
private final String supportedVersions;
24+
25+
public ClientVersionNotSupportedException(
26+
String featureVersion, String clientImpl, String supportedVersions) {
27+
this.featureVersion = featureVersion;
28+
this.clientImpl = clientImpl;
29+
this.supportedVersions = supportedVersions;
30+
}
31+
32+
public String getFeatureVersion() {
33+
return featureVersion;
34+
}
35+
36+
public String getClientImpl() {
37+
return clientImpl;
38+
}
39+
40+
public String getSupportedVersions() {
41+
return supportedVersions;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.serviceclient.exceptions;
19+
20+
public class DomainAlreadyExistsException extends ServiceClientException {
21+
public DomainAlreadyExistsException(Throwable cause) {
22+
super(cause);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.serviceclient.exceptions;
19+
20+
public class DomainNotActiveException extends ServiceClientException {
21+
private final String domain;
22+
private final String currentCluster;
23+
private final String activeCluster;
24+
25+
public DomainNotActiveException(String domain, String currentCluster, String activeCluster) {
26+
this.domain = domain;
27+
this.currentCluster = currentCluster;
28+
this.activeCluster = activeCluster;
29+
}
30+
31+
public String getDomain() {
32+
return domain;
33+
}
34+
35+
public String getCurrentCluster() {
36+
return currentCluster;
37+
}
38+
39+
public String getActiveCluster() {
40+
return activeCluster;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.serviceclient.exceptions;
19+
20+
public class EntityNotExistsException extends ServiceClientException {
21+
22+
public EntityNotExistsException(Throwable cause) {
23+
super(cause);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.serviceclient.exceptions;
19+
20+
public class FeatureNotEnabledException extends ServiceClientException {
21+
private final String featureFlag;
22+
23+
public FeatureNotEnabledException(String featureFlag) {
24+
this.featureFlag = featureFlag;
25+
}
26+
27+
public String getFeatureFlag() {
28+
return featureFlag;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.serviceclient.exceptions;
19+
20+
public class InternalDataInconsistencyException extends ServiceClientException {
21+
public InternalDataInconsistencyException(Throwable cause) {
22+
super(cause);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.serviceclient.exceptions;
19+
20+
public class InternalServiceException extends ServiceClientException {
21+
public InternalServiceException(Throwable cause) {
22+
super(cause);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.serviceclient.exceptions;
19+
20+
public class LimitExceededException extends ServiceClientException {
21+
public LimitExceededException(Throwable cause) {
22+
super(cause);
23+
}
24+
}

0 commit comments

Comments
 (0)