1#!/usr/bin/env python
2#
3# Copyright 2016 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Define errors that are raised by the driver."""
18
19import json
20
21HTTP_NOT_FOUND_CODE = 404
22
23
24class DriverError(Exception):
25    """Base Android Gce driver exception."""
26
27
28class ConfigError(DriverError):
29    """Error related to config."""
30
31
32class CommandArgError(DriverError):
33    """Error related to command line args."""
34
35
36class GceOperationTimeoutError(DriverError):
37    """Error raised when a GCE operation timedout."""
38
39
40class HttpError(DriverError):
41    """Error related to http requests."""
42
43    def __init__(self, code, message):
44        self.code = code
45        super(HttpError, self).__init__(message)
46
47    @staticmethod
48    def CreateFromHttpError(http_error):
49        """Create from an apiclient.errors.HttpError.
50
51        Parse the error code from apiclient.errors.HttpError
52        and create an instance of HttpError from this module
53        that has the error code.
54
55        Args:
56            http_error: An apiclient.errors.HttpError instance.
57
58        Returns:
59            An HttpError instance from this module.
60        """
61        return HttpError(http_error.resp.status, str(http_error))
62
63
64class ResourceNotFoundError(HttpError):
65    """Error raised when a resource is not found."""
66
67
68class InvalidVirtualDeviceIpError(DriverError):
69    """Invalid virtual device's IP is set.
70
71    Raise this when the virtual device's IP of an AVD instance is invalid.
72    """
73
74
75class DeviceBootTimeoutError(DriverError):
76    """Raised when an AVD defice failed to boot within timeout."""
77
78
79class HasRetriableRequestsError(DriverError):
80    """Raised when some retriable requests fail in a batch execution."""
81
82
83class AuthentcationError(DriverError):
84    """Raised when authentication fails."""
85