1 /** 2 * Copyright (C) 2022 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.remoteprovisioner; 18 19 import android.annotation.IntDef; 20 import android.security.IGenerateRkpKeyService.Status; 21 22 import java.lang.annotation.Retention; 23 import java.lang.annotation.RetentionPolicy; 24 25 /** 26 * Represents an error that occurred while contacting the remote key provisioning server. 27 */ 28 public final class RemoteProvisioningException extends Exception { 29 /** @hide */ 30 @Retention(RetentionPolicy.SOURCE) 31 @IntDef(flag = true, value = { 32 Status.NO_NETWORK_CONNECTIVITY, 33 Status.NETWORK_COMMUNICATION_ERROR, 34 Status.DEVICE_NOT_REGISTERED, 35 Status.HTTP_CLIENT_ERROR, 36 Status.HTTP_SERVER_ERROR, 37 Status.HTTP_UNKNOWN_ERROR, 38 Status.INTERNAL_ERROR, 39 }) 40 public @interface ErrorCode { 41 } 42 43 private static final int HTTP_STATUS_DEVICE_NOT_REGISTERED = 444; 44 private static final int HTTP_CLIENT_ERROR_HUNDREDS_DIGIT = 4; 45 private static final int HTTP_SERVER_ERROR_HUNDREDS_DIGIT = 5; 46 47 @ErrorCode 48 private final int mErrorCode; 49 50 /** 51 * @param errorCode the underlying ServerInterface error 52 * @param message describes the exception 53 */ RemoteProvisioningException(@rrorCode int errorCode, String message)54 public RemoteProvisioningException(@ErrorCode int errorCode, String message) { 55 super(message); 56 mErrorCode = errorCode; 57 } 58 59 /** 60 * @param errorCode the underlying ServerInterface error 61 * @param message describes the exception 62 * @param cause the underlying error that led this exception 63 */ RemoteProvisioningException(@rrorCode int errorCode, String message, Throwable cause)64 public RemoteProvisioningException(@ErrorCode int errorCode, String message, Throwable cause) { 65 super(message, cause); 66 mErrorCode = errorCode; 67 } 68 69 /** 70 * @param httpStatus the HTTP status that lead to the error 71 * @return a newly created RemoteProvisioningException that indicates an HTTP error occurred 72 */ createFromHttpError(@rrorCode int httpStatus)73 public static RemoteProvisioningException createFromHttpError(@ErrorCode int httpStatus) { 74 String message = "HTTP error status encountered: " + httpStatus; 75 if (httpStatus == HTTP_STATUS_DEVICE_NOT_REGISTERED) { 76 return new RemoteProvisioningException(Status.DEVICE_NOT_REGISTERED, message); 77 } 78 if ((httpStatus / 100) == HTTP_CLIENT_ERROR_HUNDREDS_DIGIT) { 79 return new RemoteProvisioningException(Status.HTTP_CLIENT_ERROR, message); 80 } 81 if ((httpStatus / 100) == HTTP_SERVER_ERROR_HUNDREDS_DIGIT) { 82 return new RemoteProvisioningException(Status.HTTP_SERVER_ERROR, message); 83 } 84 return new RemoteProvisioningException(Status.HTTP_UNKNOWN_ERROR, message); 85 } 86 87 /** 88 * @return the underlying error that caused the failure 89 */ 90 @ErrorCode getErrorCode()91 public int getErrorCode() { 92 return mErrorCode; 93 } 94 } 95