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.rkpdapp; 18 19 /** 20 * Represents an error that occurred while contacting the remote key provisioning server. 21 */ 22 public final class RkpdException extends Exception { 23 private static final int HTTP_STATUS_DEVICE_NOT_REGISTERED = 444; 24 private static final int HTTP_CLIENT_ERROR_HUNDREDS_DIGIT = 4; 25 private static final int HTTP_SERVER_ERROR_HUNDREDS_DIGIT = 5; 26 public enum ErrorCode { 27 NO_NETWORK_CONNECTIVITY, 28 NETWORK_COMMUNICATION_ERROR, 29 DEVICE_NOT_REGISTERED, 30 HTTP_CLIENT_ERROR, 31 HTTP_SERVER_ERROR, 32 HTTP_UNKNOWN_ERROR, 33 INTERNAL_ERROR, 34 } 35 36 private final ErrorCode mErrorCode; 37 38 /** 39 * @param errorCode the underlying ServerInterface error 40 * @param message describes the exception 41 */ RkpdException(ErrorCode errorCode, String message)42 public RkpdException(ErrorCode errorCode, String message) { 43 super(message); 44 mErrorCode = errorCode; 45 } 46 47 /** 48 * @param errorCode the underlying ServerInterface error 49 * @param message describes the exception 50 * @param cause the underlying error that led this exception 51 */ RkpdException(ErrorCode errorCode, String message, Throwable cause)52 public RkpdException(ErrorCode errorCode, String message, Throwable cause) { 53 super(message, cause); 54 mErrorCode = errorCode; 55 } 56 57 /** 58 * @param httpStatus the HTTP status that lead to the error 59 * @return a newly created RemoteProvisioningException that indicates an HTTP error occurred 60 */ createFromHttpError(int httpStatus)61 public static RkpdException createFromHttpError(int httpStatus) { 62 String message = "HTTP error status encountered: " + httpStatus; 63 if (httpStatus == HTTP_STATUS_DEVICE_NOT_REGISTERED) { 64 return new RkpdException(ErrorCode.DEVICE_NOT_REGISTERED, message); 65 } 66 if ((httpStatus / 100) == HTTP_CLIENT_ERROR_HUNDREDS_DIGIT) { 67 return new RkpdException(ErrorCode.HTTP_CLIENT_ERROR, message); 68 } 69 if ((httpStatus / 100) == HTTP_SERVER_ERROR_HUNDREDS_DIGIT) { 70 return new RkpdException(ErrorCode.HTTP_SERVER_ERROR, message); 71 } 72 return new RkpdException(ErrorCode.HTTP_UNKNOWN_ERROR, message); 73 } 74 75 /** 76 * @return the underlying error that caused the failure 77 */ getErrorCode()78 public ErrorCode getErrorCode() { 79 return mErrorCode; 80 } 81 } 82