Commit 033d4900 authored by jbudorick's avatar jbudorick Committed by Commit bot

[Android] Revise host_forwarder exception type & failure handling.

BUG=581256

Review URL: https://codereview.chromium.org/1636953002

Cr-Commit-Position: refs/heads/master@{#371454}
parent 094d962d
......@@ -9,6 +9,7 @@ import logging
import os
import psutil
from devil import base_error
from devil import devil_env
from devil.android.constants import file_system
from devil.android.valgrind_tools import base_tool
......@@ -40,6 +41,13 @@ class _FileLock(object):
os.close(self._fd)
class HostForwarderError(base_error.BaseError):
"""Exception for failures involving host_forwarder."""
def __init__(self, message):
super(HostForwarderError, self).__init__(message)
class Forwarder(object):
"""Thread-safe class to manage port forwards from the device to the host."""
......@@ -90,8 +98,9 @@ class Forwarder(object):
[instance._host_forwarder_path] + redirection_command)
except OSError as e:
if e.errno == 2:
raise Exception('Unable to start host forwarder. Make sure you have'
' built host_forwarder.')
raise HostForwarderError(
'Unable to start host forwarder. '
'Make sure you have built host_forwarder.')
else: raise
if exit_code != 0:
Forwarder._KillDeviceLocked(device, tool)
......@@ -101,12 +110,14 @@ class Forwarder(object):
for line in ps_out:
if 'device_forwarder' in line:
logging.info(' %s', line)
raise Exception('%s exited with %d:\n%s' % (
instance._host_forwarder_path, exit_code, '\n'.join(output)))
raise HostForwarderError(
'%s exited with %d:\n%s' % (instance._host_forwarder_path,
exit_code, '\n'.join(output)))
tokens = output.split(':')
if len(tokens) != 2:
raise Exception('Unexpected host forwarder output "%s", '
'expected "device_port:host_port"' % output)
raise HostForwarderError(
'Unexpected host forwarder output "%s", '
'expected "device_port:host_port"' % output)
device_port = int(tokens[0])
host_port = int(tokens[1])
serial_with_port = (device_serial, device_port)
......@@ -305,8 +316,9 @@ class Forwarder(object):
(exit_code, output) = cmd_helper.GetCmdStatusAndOutput(
['pkill', '-9', 'host_forwarder'])
if exit_code != 0:
raise Exception('%s exited with %d:\n%s' % (
self._host_forwarder_path, exit_code, '\n'.join(output)))
raise HostForwarderError(
'%s exited with %d:\n%s' % (self._host_forwarder_path, exit_code,
'\n'.join(output)))
@staticmethod
def _KillDeviceLocked(device, tool):
......
......@@ -6,6 +6,7 @@ import fnmatch
import functools
import logging
from devil import base_error
from devil.android import device_errors
from pylib import valgrind_tools
from pylib.base import base_test_result
......@@ -38,12 +39,13 @@ def handle_shard_failures_with(on_failure):
def wrapper(dev, *args, **kwargs):
try:
return f(dev, *args, **kwargs)
except device_errors.CommandFailedError:
logging.exception('Shard failed: %s(%s)', f.__name__, str(dev))
except device_errors.CommandTimeoutError:
logging.exception('Shard timed out: %s(%s)', f.__name__, str(dev))
except device_errors.DeviceUnreachableError:
logging.exception('Shard died: %s(%s)', f.__name__, str(dev))
except base_error.BaseError:
logging.exception('Shard failed: %s(%s)', f.__name__,
str(dev))
if on_failure:
on_failure(dev, f.__name__)
return None
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment