|
| 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 | +} |
0 commit comments