|
17 | 17 | package io.appium.java_client.remote; |
18 | 18 |
|
19 | 19 | import static com.google.common.base.Throwables.throwIfUnchecked; |
| 20 | +import static java.util.Optional.ofNullable; |
20 | 21 |
|
| 22 | +import com.google.common.base.Function; |
21 | 23 | import com.google.common.base.Throwables; |
22 | 24 |
|
23 | 25 | import org.openqa.selenium.WebDriverException; |
@@ -61,25 +63,30 @@ public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, |
61 | 63 | this(additionalCommands, service, new ApacheHttpClient.Factory()); |
62 | 64 | } |
63 | 65 |
|
64 | | - @Override public Response execute(Command command) throws IOException, WebDriverException { |
65 | | - if (DriverCommand.NEW_SESSION.equals(command.getName()) && service != null) { |
66 | | - service.start(); |
| 66 | + @Override public Response execute(Command command) throws WebDriverException { |
| 67 | + if (DriverCommand.NEW_SESSION.equals(command.getName())) { |
| 68 | + ofNullable(service).ifPresent(driverService -> { |
| 69 | + try { |
| 70 | + driverService.start(); |
| 71 | + } catch (IOException e) { |
| 72 | + throw new WebDriverException(e.getMessage(), e); |
| 73 | + } |
| 74 | + }); |
67 | 75 | } |
68 | 76 |
|
69 | 77 | try { |
70 | 78 | return super.execute(command); |
71 | 79 | } catch (Throwable t) { |
72 | 80 | Throwable rootCause = Throwables.getRootCause(t); |
73 | 81 | if (rootCause instanceof ConnectException |
74 | | - && rootCause.getMessage().contains("Connection refused") |
75 | | - && service != null) { |
76 | | - if (service.isRunning()) { |
77 | | - throw new WebDriverException("The session is closed!", t); |
78 | | - } |
| 82 | + && rootCause.getMessage().contains("Connection refused")) { |
| 83 | + throw ofNullable(service).map((Function<DriverService, WebDriverException>) service -> { |
| 84 | + if (service.isRunning()) { |
| 85 | + return new WebDriverException("The session is closed!", rootCause); |
| 86 | + } |
79 | 87 |
|
80 | | - if (!service.isRunning()) { |
81 | | - throw new WebDriverException("The appium server has accidentally died!", t); |
82 | | - } |
| 88 | + return new WebDriverException("The appium server has accidentally died!", rootCause); |
| 89 | + }).orElse(new WebDriverException(rootCause.getMessage(), rootCause)); |
83 | 90 | } |
84 | 91 | throwIfUnchecked(t); |
85 | 92 | throw new WebDriverException(t); |
|
0 commit comments