diff --git a/README.md b/README.md index df4e7ce..679d2a2 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,13 @@ System.out.println(bsLocal.isRunning()); bsLocal.stop(); ``` +### Disabling Error Logging +While creating the instance of Local, pass false to debugOutput. +``` +// creates an instance of Local without debug output +Local bsLocal = new Local(false); +``` + ## Arguments Apart from the key, all other BrowserStack Local modifiers are optional. For the full list of modifiers, refer [BrowserStack Local modifiers](https://www.browserstack.com/local-testing#modifiers). For examples, refer below - diff --git a/src/main/java/com/browserstack/local/Local.java b/src/main/java/com/browserstack/local/Local.java index b9cce6e..cc694a6 100644 --- a/src/main/java/com/browserstack/local/Local.java +++ b/src/main/java/com/browserstack/local/Local.java @@ -27,6 +27,8 @@ public class Local { private final Map parameters; private final Map avoidValueParameters; + private static boolean debugOutput = true; + public Local() { avoidValueParameters = new HashMap(); avoidValueParameters.put("v", "-vvv"); @@ -45,6 +47,11 @@ public Local() { parameters.put("proxyPass", "-proxyPass"); } + public Local(boolean debugOutput) { + this(); + this.debugOutput = debugOutput; + } + /** * Starts Local instance with options * @@ -56,7 +63,7 @@ public void start(Map options) throws Exception { if (options.get("binarypath") != null) { binaryPath = options.get("binarypath"); } else { - LocalBinary lb = new LocalBinary(); + LocalBinary lb = new LocalBinary(debugOutput); binaryPath = lb.getBinaryPath(); } @@ -77,12 +84,27 @@ public void start(Map options) throws Exception { } int r = proc.waitFor(); - JSONObject obj = new JSONObject(!stdout.equals("") ? stdout : stderr); - if(!obj.getString("state").equals("connected")){ - throw new LocalException(obj.getJSONObject("message").getString("message")); - } - else { - pid = obj.getInt("pid"); + String messageString = !stdout.equals("") ? stdout : stderr; + + try { + JSONObject obj = new JSONObject(messageString); + if(!obj.getString("state").equals("connected")){ + if (debugOutput) { + System.err.println("Message Body"); + System.err.println(messageString); + } + throw new LocalException(obj.getJSONObject("message").getString("message")); + } + else { + pid = obj.getInt("pid"); + } + } catch (Exception ex) { + if (debugOutput) { + System.err.println("Binary Response Parse Error:"); + ex.printStackTrace(); + System.err.println("Message Body"); + System.err.println(messageString); + } } } } @@ -109,7 +131,7 @@ public void stop(Map options) throws Exception { if (options.get("binarypath") != null) { binaryPath = options.get("binarypath"); } else { - LocalBinary lb = new LocalBinary(); + LocalBinary lb = new LocalBinary(debugOutput); binaryPath = lb.getBinaryPath(); } makeCommand(options, "stop"); diff --git a/src/main/java/com/browserstack/local/LocalBinary.java b/src/main/java/com/browserstack/local/LocalBinary.java index 36f8a4a..801960e 100644 --- a/src/main/java/com/browserstack/local/LocalBinary.java +++ b/src/main/java/com/browserstack/local/LocalBinary.java @@ -11,6 +11,7 @@ class LocalBinary { private static final String BIN_URL = "https://bstack-local-prod.s3.amazonaws.com/"; + private static boolean debugOutput = true; private String httpPath; @@ -25,11 +26,26 @@ class LocalBinary { }; LocalBinary() throws LocalException { - initialize(); - getBinary(); - checkBinary(); + try { + initialize(); + getBinary(); + checkBinary(); + } catch (Exception ex) { + LocalException err = new LocalException("Error trying to download BrowserStackLocal binary"); + if (debugOutput) { + System.err.println(err); + ex.printStackTrace(); + } + throw err; + } } + LocalBinary(boolean debugOutput) throws LocalException { + this(); + this.debugOutput = debugOutput; + } + + private void initialize() throws LocalException { String osname = System.getProperty("os.name").toLowerCase(); isOSWindows = osname.contains("windows"); @@ -51,7 +67,11 @@ private void initialize() throws LocalException { binFileName = "BrowserStackLocal-linux-ia32"; } } else { - throw new LocalException("Failed to detect OS type"); + LocalException err = new LocalException("Failed to detect OS type"); + if (debugOutput) { + err.printStackTrace(); + } + throw err; } httpPath = BIN_URL + binFileName; @@ -81,7 +101,11 @@ private void checkBinary() throws LocalException{ } getBinary(); if(!validateBinary()){ - throw new LocalException("BrowserStackLocal binary is corrupt"); + LocalException err = new LocalException("BrowserStackLocal binary is corrupt"); + if (debugOutput) { + err.printStackTrace(); + } + throw err; } } } @@ -104,10 +128,18 @@ private boolean validateBinary() throws LocalException{ return validBinary; }catch(IOException ex){ - throw new LocalException(ex.toString()); + LocalException err = new LocalException(ex.toString()); + if (debugOutput) { + err.printStackTrace(); + } + throw err; } catch(InterruptedException ex){ - throw new LocalException(ex.toString()); + LocalException err = new LocalException(ex.toString()); + if (debugOutput) { + err.printStackTrace(); + } + throw err; } } @@ -133,8 +165,11 @@ private String getAvailableDirectory() throws LocalException { else i++; } - - throw new LocalException("Error trying to download BrowserStackLocal binary"); + LocalException err = new LocalException("Error trying to download BrowserStackLocal binary"); + if (debugOutput) { + err.printStackTrace(); + } + throw err; } private boolean makePath(String path) { @@ -163,7 +198,12 @@ private void downloadBinary(String destParentDir) throws LocalException { changePermissions(binaryPath); } catch (Exception e) { - throw new LocalException("Error trying to download BrowserStackLocal binary"); + LocalException err = new LocalException("Error trying to download BrowserStackLocal binary"); + if (debugOutput) { + System.err.println(err.toString()); + e.printStackTrace(); + } + throw err; } } diff --git a/src/main/java/com/browserstack/local/LocalException.java b/src/main/java/com/browserstack/local/LocalException.java index 2085a5e..d2a7ef6 100644 --- a/src/main/java/com/browserstack/local/LocalException.java +++ b/src/main/java/com/browserstack/local/LocalException.java @@ -1,6 +1,6 @@ package com.browserstack.local; -class LocalException extends Exception { +public class LocalException extends Exception { LocalException(String message) { super(message);