1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.devicelockcontroller.provision.grpc;
18 
19 import androidx.annotation.NonNull;
20 import androidx.annotation.Nullable;
21 
22 import io.grpc.Status;
23 import io.grpc.Status.Code;
24 
25 /**
26  * Base class for encapsulating a device check in server response. This class handles the Grpc
27  * status response, subclasses will handle request specific responses.
28  */
29 abstract class GrpcResponse {
30     @Nullable
31     Status mStatus;
32 
GrpcResponse()33     GrpcResponse() {
34         mStatus = null;
35     }
36 
GrpcResponse(@onNull Status status)37     GrpcResponse(@NonNull Status status) {
38         mStatus = status;
39     }
40 
hasRecoverableError()41     public boolean hasRecoverableError() {
42         return mStatus != null
43                 && (mStatus.getCode() == Code.UNAVAILABLE
44                 || mStatus.getCode() == Code.UNKNOWN
45                 || mStatus.getCode() == Code.INVALID_ARGUMENT
46                 || mStatus.getCode() == Code.PERMISSION_DENIED
47                 || mStatus.getCode() == Code.DEADLINE_EXCEEDED
48                 || mStatus.getCode() == Code.RESOURCE_EXHAUSTED
49                 || mStatus.getCode() == Code.ABORTED
50                 || mStatus.getCode() == Code.DATA_LOSS
51                 || mStatus.getCode() == Code.UNAUTHENTICATED);
52     }
53 
isSuccessful()54     public boolean isSuccessful() {
55         return mStatus == null || mStatus.isOk();
56     }
57 
hasFatalError()58     public boolean hasFatalError() {
59         return !isSuccessful() && !hasRecoverableError() && !isInterrupted();
60     }
61 
isInterrupted()62     public boolean isInterrupted() {
63         return mStatus != null && (mStatus.getCause() instanceof InterruptedException);
64     }
65 
66     @Override
toString()67     public String toString() {
68         return "mStatus: " + mStatus;
69     }
70 }
71