Home | History | Annotate | Download | only in android_scripting
      1 /*
      2  * Copyright (C) 2018 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.googlecode.android_scripting;
     18 
     19 import com.googlecode.android_scripting.jsonrpc.JsonRpcResult;
     20 
     21 import org.json.JSONException;
     22 import org.json.JSONObject;
     23 
     24 /**
     25  * A enum of Sl4aException objects. This enum holds all of the innate systematic
     26  * errors that can be sent to the client. Errors that come from Facades should
     27  * not use these Sl4aErrors, unless the Facades themselves are designed to
     28  * modify the state of SL4A's connection
     29  */
     30 public enum Sl4aErrors {
     31     // Process-Level Errors 0-99
     32 
     33     // JSON RPC Errors 100 - 199
     34     JSON_RPC_UNKNOWN_EXCEPTION(100,
     35             "Something went horribly wrong when parsing or returning your request."),
     36     JSON_RPC_REQUEST_NOT_JSON(101, "The request sent was not a valid JSONObject."),
     37     JSON_RPC_MISSING_ID(102, "The \"id\" field is missing."),
     38     JSON_RPC_MISSING_METHOD(103, "The \"method\" field is missing."),
     39     JSON_RPC_METHOD_NOT_STRING(104, "The \"method\" field must be a string."),
     40     JSON_RPC_PARAMS_NOT_AN_ARRAY(105, "The \"params\" field must be an array."),
     41     JSON_RPC_UNKNOWN_RPC_METHOD(106, "No known RPC for the given \"method\"."),
     42     JSON_RPC_RESULT_NOT_JSON_VALID(107,
     43             "The JsonBuilder was unable to convert the result to valid JSON."),
     44     JSON_RPC_FAILED_TO_BUILD_RESULT(108, "The JsonBuilder failed to build the result."),
     45     JSON_RPC_FACADE_EXCEPTION_OCCURRED(109, "An exception occurred while handling this RPC. "
     46             + "Check the \"data\" field for the error received."),
     47     JSON_RPC_INVALID_PARAMETERS(110, "The \"params\" given are not valid for this \"method\"."),
     48 
     49     // Session Errors 200 - 299
     50     SESSION_UNKNOWN_EXCEPTION(200, "Something went horribly wrong when handling your session."),
     51     SESSION_INVALID(201, "This session no longer exists or is invalid."),
     52     ;
     53 
     54     private final Sl4aException mSl4aException;
     55 
     56     Sl4aErrors(int errorCode, String errorMessage) {
     57         mSl4aException = new Sl4aException(errorCode, errorMessage);
     58     }
     59 
     60     /**
     61      * Returns the underlying {@see Sl4aException}.
     62      */
     63     public Sl4aException getError() {
     64         return mSl4aException;
     65     }
     66 
     67     /**
     68      * Converts this Sl4aError to a JSON-RPC 2.0 compliant JSONObject.
     69      * @param id the id given by the request
     70      * @return a JSON-RPC 2.0 error response as a JSONObject
     71      * @throws JSONException
     72      */
     73     public JSONObject toJson(Object id) throws JSONException {
     74         return JsonRpcResult.error(id, mSl4aException);
     75     }
     76 
     77     /**
     78      * Converts this Sl4aError to a JSON-RPC 2.0 compliant JSONObject.
     79      * @param id      the id given by the request
     80      * @param details additional details to pass along with the error
     81      * @return a JSON-RPC 2.0 error response as a JSONObject
     82      * @throws JSONException
     83      */
     84     public JSONObject toJson(Object id, Object details) throws JSONException {
     85         return JsonRpcResult.error(id, mSl4aException, details);
     86     }
     87 }
     88