1 /* 2 * Copyright 2020 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 android.app.appsearch.exceptions; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.app.appsearch.AppSearchResult; 22 23 /** 24 * An exception thrown by {@link android.app.appsearch.AppSearchSession} or a subcomponent. 25 * 26 * <p>These exceptions can be converted into a failed {@link AppSearchResult} for propagating to the 27 * client. 28 */ 29 public class AppSearchException extends Exception { 30 @AppSearchResult.ResultCode private final int mResultCode; 31 32 /** 33 * Initializes an {@link AppSearchException} with no message. 34 * 35 * @param resultCode One of the constants documented in {@link AppSearchResult#getResultCode}. 36 */ AppSearchException(@ppSearchResult.ResultCode int resultCode)37 public AppSearchException(@AppSearchResult.ResultCode int resultCode) { 38 this(resultCode, /* message= */ null); 39 } 40 41 /** 42 * Initializes an {@link AppSearchException} with a result code and message. 43 * 44 * @param resultCode One of the constants documented in {@link AppSearchResult#getResultCode}. 45 * @param message The detail message (which is saved for later retrieval by the {@link 46 * #getMessage()} method). 47 */ AppSearchException( @ppSearchResult.ResultCode int resultCode, @Nullable String message)48 public AppSearchException( 49 @AppSearchResult.ResultCode int resultCode, @Nullable String message) { 50 this(resultCode, message, /* cause= */ null); 51 } 52 53 /** 54 * Initializes an {@link AppSearchException} with a result code, message and cause. 55 * 56 * @param resultCode One of the constants documented in {@link AppSearchResult#getResultCode}. 57 * @param message The detail message (which is saved for later retrieval by the {@link 58 * #getMessage()} method). 59 * @param cause The cause (which is saved for later retrieval by the {@link #getCause()} 60 * method). (A null value is permitted, and indicates that the cause is nonexistent or 61 * unknown.) 62 */ AppSearchException( @ppSearchResult.ResultCode int resultCode, @Nullable String message, @Nullable Throwable cause)63 public AppSearchException( 64 @AppSearchResult.ResultCode int resultCode, 65 @Nullable String message, 66 @Nullable Throwable cause) { 67 super(message, cause); 68 mResultCode = resultCode; 69 } 70 71 /** 72 * Returns the result code this exception was constructed with. 73 * 74 * @return One of the constants documented in {@link AppSearchResult#getResultCode}. 75 */ 76 @AppSearchResult.ResultCode getResultCode()77 public int getResultCode() { 78 return mResultCode; 79 } 80 81 /** Converts this {@link java.lang.Exception} into a failed {@link AppSearchResult}. */ 82 @NonNull toAppSearchResult()83 public <T> AppSearchResult<T> toAppSearchResult() { 84 return AppSearchResult.newFailedResult(mResultCode, getMessage()); 85 } 86 } 87