Skip to content

Commit c3cd5d1

Browse files
committed
Update ErrorHandler to use Java 8-isms
Also updates some deprecated guava usages and removes the author tag, since we don't use those in this project.
1 parent 25d99bd commit c3cd5d1

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

java/client/src/org/openqa/selenium/remote/ErrorHandler.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@
2020

2121
import static org.openqa.selenium.remote.ErrorCodes.SUCCESS;
2222

23-
import com.google.common.base.Function;
24-
import com.google.common.base.Optional;
25-
import com.google.common.base.Predicates;
2623
import com.google.common.base.Throwables;
27-
import com.google.common.collect.Iterables;
2824
import com.google.common.primitives.Ints;
2925

3026
import org.openqa.selenium.UnhandledAlertException;
@@ -35,11 +31,12 @@
3531
import java.math.RoundingMode;
3632
import java.util.List;
3733
import java.util.Map;
34+
import java.util.Objects;
35+
import java.util.Optional;
36+
import java.util.function.Function;
3837

3938
/**
4039
* Maps exceptions to status codes for sending over the wire.
41-
*
42-
* @author [email protected] (Jason Leyba)
4340
*/
4441
public class ErrorHandler {
4542

@@ -97,7 +94,9 @@ public Response throwIfResponseFailed(Response response, long duration) throws R
9794
}
9895

9996
if (response.getValue() instanceof Throwable) {
100-
throw Throwables.propagate((Throwable) response.getValue());
97+
Throwable throwable = (Throwable) response.getValue();
98+
Throwables.throwIfUnchecked(throwable);
99+
throw new RuntimeException(throwable);
101100
}
102101

103102
Class<? extends WebDriverException> outerErrorType =
@@ -214,10 +213,8 @@ private <T extends Throwable> T createThrowable(
214213
try {
215214
Constructor<T> constructor = clazz.getConstructor(parameterTypes);
216215
return constructor.newInstance(parameters);
217-
} catch (ReflectiveOperationException e) {
216+
} catch (OutOfMemoryError | ReflectiveOperationException e) {
218217
// Do nothing - fall through.
219-
} catch (OutOfMemoryError error) {
220-
// It can happen...
221218
}
222219
return null;
223220
}
@@ -270,10 +267,11 @@ private Throwable rebuildServerError(Map<String, Object> rawErrorData, int respo
270267
@SuppressWarnings({"unchecked"})
271268
List<Map<String, Object>> stackTraceInfo =
272269
(List<Map<String, Object>>) rawErrorData.get(STACK_TRACE);
273-
Iterable<StackTraceElement> stackFrames =
274-
Iterables.transform(stackTraceInfo, new FrameInfoToStackFrame());
275-
stackFrames = Iterables.filter(stackFrames, Predicates.notNull());
276-
stackTrace = Iterables.toArray(stackFrames, StackTraceElement.class);
270+
271+
stackTrace = stackTraceInfo.stream()
272+
.map(entry -> new FrameInfoToStackFrame().apply(entry))
273+
.filter(Objects::nonNull)
274+
.toArray(StackTraceElement[]::new);
277275
}
278276

279277
toReturn.setStackTrace(stackTrace);
@@ -300,30 +298,33 @@ public StackTraceElement apply(Map<String, Object> frameInfo) {
300298
return null;
301299
}
302300

303-
Optional<Number> maybeLineNumberInteger = Optional.absent();
301+
Optional<Number> maybeLineNumberInteger = Optional.empty();
304302

305303
final Object lineNumberObject = frameInfo.get(LINE_NUMBER);
306304
if (lineNumberObject instanceof Number) {
307-
maybeLineNumberInteger = Optional.of((Number) lineNumberObject);
305+
maybeLineNumberInteger = Optional.of((Number) lineNumberObject);
308306
} else if (lineNumberObject != null) {
309-
// might be a Number as a String
310-
maybeLineNumberInteger = Optional.fromNullable((Number) Ints.tryParse(lineNumberObject.toString()));
307+
// might be a Number as a String
308+
maybeLineNumberInteger = Optional.ofNullable(Ints.tryParse(lineNumberObject.toString()));
311309
}
312310

313311
// default -1 for unknown, see StackTraceElement constructor javadoc
314-
final int lineNumber = maybeLineNumberInteger.or(-1).intValue();
312+
final int lineNumber = maybeLineNumberInteger.orElse(-1).intValue();
315313

316314
// Gracefully handle remote servers that don't (or can't) send back
317315
// complete stack trace info. At least some of this information should
318316
// be included...
319-
String className = frameInfo.containsKey(CLASS_NAME)
320-
? toStringOrNull(frameInfo.get(CLASS_NAME)) : UNKNOWN_CLASS;
321-
String methodName = frameInfo.containsKey(METHOD_NAME)
322-
? toStringOrNull(frameInfo.get(METHOD_NAME)) : UNKNOWN_METHOD;
323-
String fileName = frameInfo.containsKey(FILE_NAME)
324-
? toStringOrNull(frameInfo.get(FILE_NAME)) : UNKNOWN_FILE;
325-
326-
return new StackTraceElement(className, methodName, fileName,
317+
String className = frameInfo.containsKey(CLASS_NAME) ?
318+
toStringOrNull(frameInfo.get(CLASS_NAME)) : UNKNOWN_CLASS;
319+
String methodName = frameInfo.containsKey(METHOD_NAME) ?
320+
toStringOrNull(frameInfo.get(METHOD_NAME)) : UNKNOWN_METHOD;
321+
String fileName = frameInfo.containsKey(FILE_NAME) ?
322+
toStringOrNull(frameInfo.get(FILE_NAME)) : UNKNOWN_FILE;
323+
324+
return new StackTraceElement(
325+
className,
326+
methodName,
327+
fileName,
327328
lineNumber);
328329
}
329330

0 commit comments

Comments
 (0)