Skip to content

Loc 3644 add exception logging #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -
Expand Down
38 changes: 30 additions & 8 deletions src/main/java/com/browserstack/local/Local.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class Local {
private final Map<String, String> parameters;
private final Map<String, String> avoidValueParameters;

private static boolean debugOutput = true;

public Local() {
avoidValueParameters = new HashMap<String, String>();
avoidValueParameters.put("v", "-vvv");
Expand All @@ -45,6 +47,11 @@ public Local() {
parameters.put("proxyPass", "-proxyPass");
}

public Local(boolean debugOutput) {
this();
this.debugOutput = debugOutput;
}

/**
* Starts Local instance with options
*
Expand All @@ -56,7 +63,7 @@ public void start(Map<String, String> 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();
}

Expand All @@ -77,12 +84,27 @@ public void start(Map<String, String> 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);
}
}
}
}
Expand All @@ -109,7 +131,7 @@ public void stop(Map<String, String> 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");
Expand Down
60 changes: 50 additions & 10 deletions src/main/java/com/browserstack/local/LocalBinary.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -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;
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/browserstack/local/LocalException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.browserstack.local;

class LocalException extends Exception {
public class LocalException extends Exception {

LocalException(String message) {
super(message);
Expand Down