Repository: appium/appium
Issue: Getting "unknown server-side error..." when real error is "Could not find an element…"

Android only:
appium 11.2
python
android native app

If I'm on a page without a button and I try
driver.find_element_by_xpath('//button')

I see in the log:
2013-11-27T21:17:07.542Z - info: [BOOTSTRAP] [debug] Finding //button using XPATH with the contextId: 
2013-11-27 15:17:07,552 [DEBUG] exec_logger.AppiumServer:info: [BOOTSTRAP] [info] Returning result: {"value":"Could not find an element using supplied strategy. ","status":13} [service.py:47]
2013-11-27 15:17:07,552 [DEBUG] exec_logger.AppiumServer:info: Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command.","origValue":"Could not find an element using supplied strategy. "},"sessionId":"fe3c866f-81d6-4fff-a28f-5e9c40a17562"} [service.py:47]

and the exception that is returned to my call is
WebDriverException: Message: u'An unknown server-side error occurred while processing the command.' 

I did some digging in the bootstrap java and every time I convinced myself I knew where it was I would find something that changed my mind, but I did see that in handlers/Find.java it was doing stuff like
catch (final ElementNotFoundException e) {
    return getErrorResult(e.getMessage());
}

and getErrorResult is defined in Find's superclass CommandHandler as
protected AndroidCommandResult getErrorResult(final String msg) {
    return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR, msg);
}

I believe instead of the 13: UNKNOWN_ERROR, bootstrap should throw  7: NO_SUCH_ELEMENT


Changed files:
lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/Find.java

diff --git a/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/Find.java b/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/Find.java
index fe95c4585..6317a869d 100644
--- a/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/Find.java
+++ b/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/Find.java
@@ -33,10 +33,10 @@ import com.android.uiautomator.core.UiSelector;
 
 /**
  * This handler is used to find elements in the Android UI.
- * 
+ *
  * Based on which {@link Strategy}, {@link UiSelector}, and optionally the
  * contextId, the element Id or Ids are returned to the user.
- * 
+ *
  */
 public class Find extends CommandHandler {
   AndroidElementsHash      elements   = AndroidElementsHash.getInstance();
@@ -60,11 +60,11 @@ public class Find extends CommandHandler {
 
   /*
    * @param command The {@link AndroidCommand} used for this handler.
-   * 
+   *
    * @return {@link AndroidCommandResult}
-   * 
+   *
    * @throws JSONException
-   * 
+   *
    * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.
    * bootstrap.AndroidCommand)
    */
@@ -211,15 +211,15 @@ public class Find extends CommandHandler {
           return getSuccessResult(fetchElement(sel, contextId));
         }
       } catch (final AndroidCommandException e) {
-        return getErrorResult(e.getMessage());
+        return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR, e.getMessage());
       } catch (final ElementNotFoundException e) {
-        return getErrorResult(e.getMessage());
+        return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
       } catch (final UnallowedTagNameException e) {
-        return getErrorResult(e.getMessage());
+        return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR, e.getMessage());
       } catch (final ElementNotInHashException e) {
-        return getErrorResult(e.getMessage());
+        return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
       } catch (final UiObjectNotFoundException e) {
-        return getErrorResult(e.getMessage());
+        return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
       }
     } else {
       try {
@@ -260,13 +260,13 @@ public class Find extends CommandHandler {
       } catch (final InvalidStrategyException e) {
         return getErrorResult(e.getMessage());
       } catch (final UnallowedTagNameException e) {
-        return getErrorResult(e.getMessage());
+        return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR, e.getMessage());
       } catch (final AndroidCommandException e) {
-        return getErrorResult(e.getMessage());
+        return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR, e.getMessage());
       } catch (final UiObjectNotFoundException e) {
-        return getErrorResult(e.getMessage());
+        return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
       } catch (final ElementNotFoundException e) {
-        return getErrorResult(e.getMessage());
+        return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage());
       }
     }
   }
@@ -274,12 +274,12 @@ public class Find extends CommandHandler {
   /**
    * Get the element from the {@link AndroidElementsHash} and return the element
    * id using JSON.
-   * 
+   *
    * @param sel
    *          A UiSelector that targets the element to fetch.
    * @param contextId
    *          The Id of the element used for the context.
-   * 
+   *
    * @return JSONObject
    * @throws JSONException
    * @throws ElementNotFoundException
@@ -295,12 +295,12 @@ public class Find extends CommandHandler {
   /**
    * Get an array of elements from the {@link AndroidElementsHash} and return
    * the element's ids using JSON.
-   * 
+   *
    * @param sel
    *          A UiSelector that targets the element to fetch.
    * @param contextId
    *          The Id of the element used for the context.
-   * 
+   *
    * @return JSONObject
    * @throws JSONException
    * @throws UiObjectNotFoundException
@@ -320,7 +320,7 @@ public class Find extends CommandHandler {
   /**
    * Create and return a UiSelector based on the strategy, text, and how many
    * you want returned.
-   * 
+   *
    * @param strategy
    *          The {@link Strategy} used to search for the element.
    * @param text
@@ -400,7 +400,7 @@ public class Find extends CommandHandler {
 
   /**
    * Create and return a UiSelector based on Xpath attributes.
-   * 
+   *
    * @param path
    *          The Xpath path.
    * @param attr
@@ -409,7 +409,7 @@ public class Find extends CommandHandler {
    *          Any constraint.
    * @param substr
    *          Any substr.
-   * 
+   *
    * @return UiSelector
    * @throws AndroidCommandException
    */