From 0ea12890f9aa8c903c2f3877563e16d6ed2cd434 Mon Sep 17 00:00:00 2001 From: "Edwin Clement (Windows - Adam)" Date: Mon, 11 Jul 2022 10:46:44 +0530 Subject: [PATCH 1/3] Added exception logging --- README.md | 7 ++ .../java/com/browserstack/local/Local.java | 11 ++- .../com/browserstack/local/LocalBinary.java | 67 ++++++++++++++++--- 3 files changed, 73 insertions(+), 12 deletions(-) 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..65219e2 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(); } @@ -109,7 +116,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..0bfa2ba 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,27 @@ 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.toString()); + System.err.println(ex.toString()); + err.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 +68,12 @@ 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) { + System.err.println(err.toString()); + err.printStackTrace(); + } + throw err; } httpPath = BIN_URL + binFileName; @@ -81,7 +103,12 @@ 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) { + System.err.println(err.toString()); + err.printStackTrace(); + } + throw err; } } } @@ -104,10 +131,20 @@ private boolean validateBinary() throws LocalException{ return validBinary; }catch(IOException ex){ - throw new LocalException(ex.toString()); + LocalException err = new LocalException(ex.toString()); + if (debugOutput) { + System.err.println(err.toString()); + err.printStackTrace(); + } + throw err; } catch(InterruptedException ex){ - throw new LocalException(ex.toString()); + LocalException err = new LocalException(ex.toString()); + if (debugOutput) { + System.err.println(err.toString()); + err.printStackTrace(); + } + throw err; } } @@ -133,8 +170,12 @@ 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) { + System.err.println(err.toString()); + err.printStackTrace(); + } + throw err; } private boolean makePath(String path) { @@ -163,7 +204,13 @@ 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()); + System.err.println(e.toString()); + err.printStackTrace(); + } + throw err; } } From aa9371d3aad11fa7c83f9b04259864ac8da38270 Mon Sep 17 00:00:00 2001 From: "Edwin Clement (Windows - Adam)" Date: Mon, 11 Jul 2022 10:48:34 +0530 Subject: [PATCH 2/3] Mark LocalException as public --- src/main/java/com/browserstack/local/LocalException.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); From 692d19a90991f001d95567a1f4fcbed896e3f3b3 Mon Sep 17 00:00:00 2001 From: "Edwin Clement (Windows - Adam)" Date: Tue, 12 Jul 2022 10:15:14 +0530 Subject: [PATCH 3/3] Added more logging, cleaned up elsewhere --- .../java/com/browserstack/local/Local.java | 27 ++++++++++++++----- .../com/browserstack/local/LocalBinary.java | 13 +++------ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/browserstack/local/Local.java b/src/main/java/com/browserstack/local/Local.java index 65219e2..cc694a6 100644 --- a/src/main/java/com/browserstack/local/Local.java +++ b/src/main/java/com/browserstack/local/Local.java @@ -84,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); + } } } } diff --git a/src/main/java/com/browserstack/local/LocalBinary.java b/src/main/java/com/browserstack/local/LocalBinary.java index 0bfa2ba..801960e 100644 --- a/src/main/java/com/browserstack/local/LocalBinary.java +++ b/src/main/java/com/browserstack/local/LocalBinary.java @@ -33,9 +33,8 @@ class LocalBinary { } catch (Exception ex) { LocalException err = new LocalException("Error trying to download BrowserStackLocal binary"); if (debugOutput) { - System.err.println(err.toString()); - System.err.println(ex.toString()); - err.printStackTrace(); + System.err.println(err); + ex.printStackTrace(); } throw err; } @@ -70,7 +69,6 @@ private void initialize() throws LocalException { } else { LocalException err = new LocalException("Failed to detect OS type"); if (debugOutput) { - System.err.println(err.toString()); err.printStackTrace(); } throw err; @@ -105,7 +103,6 @@ private void checkBinary() throws LocalException{ if(!validateBinary()){ LocalException err = new LocalException("BrowserStackLocal binary is corrupt"); if (debugOutput) { - System.err.println(err.toString()); err.printStackTrace(); } throw err; @@ -133,7 +130,6 @@ private boolean validateBinary() throws LocalException{ }catch(IOException ex){ LocalException err = new LocalException(ex.toString()); if (debugOutput) { - System.err.println(err.toString()); err.printStackTrace(); } throw err; @@ -141,7 +137,6 @@ private boolean validateBinary() throws LocalException{ catch(InterruptedException ex){ LocalException err = new LocalException(ex.toString()); if (debugOutput) { - System.err.println(err.toString()); err.printStackTrace(); } throw err; @@ -172,7 +167,6 @@ private String getAvailableDirectory() throws LocalException { } LocalException err = new LocalException("Error trying to download BrowserStackLocal binary"); if (debugOutput) { - System.err.println(err.toString()); err.printStackTrace(); } throw err; @@ -207,8 +201,7 @@ private void downloadBinary(String destParentDir) throws LocalException { LocalException err = new LocalException("Error trying to download BrowserStackLocal binary"); if (debugOutput) { System.err.println(err.toString()); - System.err.println(e.toString()); - err.printStackTrace(); + e.printStackTrace(); } throw err; }