forked from carbonblack/cbapi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.py
More file actions
104 lines (72 loc) · 3.07 KB
/
errors.py
File metadata and controls
104 lines (72 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
from cbapi.six import python_2_unicode_compatible
class ConnectionError(Exception):
pass
class ApiError(Exception):
def __init__(self, message=None, original_exception=None):
self.original_exception = original_exception
self.message = str(message)
def __str__(self):
return self.message
@python_2_unicode_compatible
class ServerError(ApiError):
"""A ServerError is raised when an HTTP error code is returned from the Carbon Black server."""
def __init__(self, error_code, message, result=None, original_exception=None):
super(ServerError, self).__init__(message=message, original_exception=original_exception)
self.error_code = error_code
self.result = result
def __str__(self):
msg = "Received error code {0:d} from API".format(self.error_code)
if self.message:
msg += ": {0:s}".format(self.message)
else:
msg += " (No further information provided)"
if self.result:
msg += ". {}".format(self.result)
return msg
@python_2_unicode_compatible
class ObjectNotFoundError(ApiError):
"""The requested object could not be found in the Carbon Black datastore."""
def __init__(self, uri, message=None, original_exception=None):
super(ObjectNotFoundError, self).__init__(message=message, original_exception=original_exception)
self.uri = uri
def __str__(self):
msg = "Received 404 (Object Not Found) for {0:s}".format(self.uri)
if self.message:
msg += ": {0:s}".format(self.message)
return msg
@python_2_unicode_compatible
class TimeoutError(ApiError):
def __init__(self, uri=None, error_code=None, message=None, original_exception=None):
super(TimeoutError, self).__init__(message=message, original_exception=original_exception)
self.uri = uri
self.error_code = error_code
def __str__(self):
if self.uri:
msg = "Timed out when requesting {0:s} from API".format(self.uri)
if self.error_code:
msg += " with HTTP status code {0:d}".format(self.error_code)
if self.message:
msg += ": {0:s}".format(self.message)
else:
msg = self.message
return msg
@python_2_unicode_compatible
class UnauthorizedError(ApiError):
def __init__(self, uri, message=None, action="read", original_exception=None):
super(UnauthorizedError, self).__init__(message=message, original_exception=original_exception)
self.uri = uri
self.action = action
def __str__(self):
if self.message:
return "Check your API Credentials: " + str(self.message)
return "Unauthorized (Check API creds): attempted to {0:s} {1:s}".format(self.action, self.uri)
class CredentialError(ApiError):
pass
class InvalidObjectError(ApiError):
pass
class InvalidHashError(Exception):
pass
class MoreThanOneResultError(ApiError):
"""Only one object was requested, but multiple matches were found in the Carbon Black datastore."""
pass