1# Copyright 2017 Google Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# Module for errors thrown from AndroidDevice object.
16
17from mobly import signals
18
19HIERARCHY_TOKEN = '::'
20
21
22class Error(signals.ControllerError):
23  pass
24
25
26class DeviceError(Error):
27  """Raised for errors specific to an AndroidDevice object."""
28
29  def __init__(self, ad, msg):
30    template = '%s %s'
31    # If the message starts with the hierarchy token, don't add the extra
32    # space.
33    if isinstance(msg, str) and msg.startswith(HIERARCHY_TOKEN):
34      template = '%s%s'
35    new_msg = template % (repr(ad), msg)
36    super().__init__(new_msg)
37
38
39class ServiceError(DeviceError):
40  """Raised for errors specific to an AndroidDevice service.
41
42  A service is inherently associated with a device instance, so the service
43  error type is a subtype of `DeviceError`.
44  """
45
46  SERVICE_TYPE = None
47
48  def __init__(self, device, msg):
49    new_msg = '%sService<%s> %s' % (HIERARCHY_TOKEN, self.SERVICE_TYPE, msg)
50    super().__init__(device, new_msg)
51