diff options
author | 2024-10-28 11:40:12 +0000 | |
---|---|---|
committer | 2024-10-31 15:01:11 +0000 | |
commit | 3ead620641042b201e94d2b1af91af9e8445d406 (patch) | |
tree | 1b811a863dc1c8dff099c1fe1ad0107082c9c4a5 /libs/appfunctions/java | |
parent | a1d05e9ee66e4c4c541fc5f8111d9b854bd54509 (diff) |
Add Error categories for AppFunction exe response
Flag: android.app.appfunctions.flags.enable_app_function_manager
Test: existing CTS
Bug: 357551503
Change-Id: I9209234f28150ebde02886a1dddce0a218d43db0
Diffstat (limited to 'libs/appfunctions/java')
-rw-r--r-- | libs/appfunctions/java/com/google/android/appfunctions/sidecar/ExecuteAppFunctionResponse.java | 133 |
1 files changed, 119 insertions, 14 deletions
diff --git a/libs/appfunctions/java/com/google/android/appfunctions/sidecar/ExecuteAppFunctionResponse.java b/libs/appfunctions/java/com/google/android/appfunctions/sidecar/ExecuteAppFunctionResponse.java index d5dfaeb77140..bf4ab45ed939 100644 --- a/libs/appfunctions/java/com/google/android/appfunctions/sidecar/ExecuteAppFunctionResponse.java +++ b/libs/appfunctions/java/com/google/android/appfunctions/sidecar/ExecuteAppFunctionResponse.java @@ -50,38 +50,97 @@ public final class ExecuteAppFunctionResponse { */ public static final String PROPERTY_RETURN_VALUE = "returnValue"; - /** The call was successful. */ + /** + * The call was successful. + * + * <p>This result code does not belong in an error category. + */ public static final int RESULT_OK = 0; - /** The caller does not have the permission to execute an app function. */ - public static final int RESULT_DENIED = 1; + /** + * The caller does not have the permission to execute an app function. + * + * <p>This error is in the {@link #ERROR_CATEGORY_REQUEST_ERROR} category. + */ + public static final int RESULT_DENIED = 1000; + + /** + * The caller supplied invalid arguments to the execution request. + * + * <p>This error may be considered similar to {@link IllegalArgumentException}. + * + * <p>This error is in the {@link #ERROR_CATEGORY_REQUEST_ERROR} category. + */ + public static final int RESULT_INVALID_ARGUMENT = 1001; + + /** + * The caller tried to execute a disabled app function. + * + * <p>This error is in the request error category. + * + * <p>This error is in the {@link #ERROR_CATEGORY_REQUEST_ERROR} category. + */ + public static final int RESULT_DISABLED = 1002; + + /** + * An internal unexpected error coming from the system. + * + * <p>This error is in the {@link #ERROR_CATEGORY_SYSTEM} category. + */ + public static final int RESULT_INTERNAL_ERROR = 2000; + + /** + * The operation was cancelled. Use this error code to report that a cancellation is done after + * receiving a cancellation signal. + * + * <p>This error is in the {@link #ERROR_CATEGORY_SYSTEM} category. + */ + public static final int RESULT_CANCELLED = 2001; /** * An unknown error occurred while processing the call in the AppFunctionService. * * <p>This error is thrown when the service is connected in the remote application but an * unexpected error is thrown from the bound application. + * + * <p>This error is in the {@link #ERROR_CATEGORY_APP} category. */ - public static final int RESULT_APP_UNKNOWN_ERROR = 2; + public static final int RESULT_APP_UNKNOWN_ERROR = 3000; - /** An internal unexpected error coming from the system. */ - public static final int RESULT_INTERNAL_ERROR = 3; + /** + * The error category is unknown. + * + * <p>This is the default value for {@link #getErrorCategory}. + */ + public static final int ERROR_CATEGORY_UNKNOWN = 0; /** - * The caller supplied invalid arguments to the call. + * The error is caused by the app requesting a function execution. * - * <p>This error may be considered similar to {@link IllegalArgumentException}. + * <p>For example, the caller provided invalid parameters in the execution request e.g. an + * invalid function ID. + * + * <p>Errors in the category fall in the range 1000-1999 inclusive. */ - public static final int RESULT_INVALID_ARGUMENT = 4; + public static final int ERROR_CATEGORY_REQUEST_ERROR = 1; - /** The caller tried to execute a disabled app function. */ - public static final int RESULT_DISABLED = 5; + /** + * The error is caused by an issue in the system. + * + * <p>For example, the AppFunctionService implementation is not found by the system. + * + * <p>Errors in the category fall in the range 2000-2999 inclusive. + */ + public static final int ERROR_CATEGORY_SYSTEM = 2; /** - * The operation was cancelled. Use this error code to report that a cancellation is done after - * receiving a cancellation signal. + * The error is caused by the app providing the function. + * + * <p>For example, the app crashed when the system is executing the request. + * + * <p>Errors in the category fall in the range 3000-3999 inclusive. */ - public static final int RESULT_CANCELLED = 6; + public static final int ERROR_CATEGORY_APP = 3; /** The result code of the app function execution. */ @ResultCode private final int mResultCode; @@ -171,6 +230,36 @@ public final class ExecuteAppFunctionResponse { } /** + * Returns the error category of the {@link ExecuteAppFunctionResponse}. + * + * <p>This method categorizes errors based on their underlying cause, allowing developers to + * implement targeted error handling and provide more informative error messages to users. It + * maps ranges of result codes to specific error categories. + * + * <p>When constructing a {@link #newFailure} response, use the appropriate result code value to + * ensure correct categorization of the failed response. + * + * <p>This method returns {@code ERROR_CATEGORY_UNKNOWN} if the result code does not belong to + * any error category, for example, in the case of a successful result with {@link #RESULT_OK}. + * + * <p>See {@link ErrorCategory} for a complete list of error categories and their corresponding + * result code ranges. + */ + @ErrorCategory + public int getErrorCategory() { + if (mResultCode >= 1000 && mResultCode < 2000) { + return ERROR_CATEGORY_REQUEST_ERROR; + } + if (mResultCode >= 2000 && mResultCode < 3000) { + return ERROR_CATEGORY_SYSTEM; + } + if (mResultCode >= 3000 && mResultCode < 4000) { + return ERROR_CATEGORY_APP; + } + return ERROR_CATEGORY_UNKNOWN; + } + + /** * Returns a generic document containing the return value of the executed function. * * <p>The {@link #PROPERTY_RETURN_VALUE} key can be used to obtain the return value. @@ -245,4 +334,20 @@ public final class ExecuteAppFunctionResponse { }) @Retention(RetentionPolicy.SOURCE) public @interface ResultCode {} + + /** + * Error categories. + * + * @hide + */ + @IntDef( + prefix = {"ERROR_CATEGORY_"}, + value = { + ERROR_CATEGORY_UNKNOWN, + ERROR_CATEGORY_REQUEST_ERROR, + ERROR_CATEGORY_APP, + ERROR_CATEGORY_SYSTEM + }) + @Retention(RetentionPolicy.SOURCE) + public @interface ErrorCategory {} } |