-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathcustom_exceptions.py
More file actions
109 lines (75 loc) · 3.43 KB
/
custom_exceptions.py
File metadata and controls
109 lines (75 loc) · 3.43 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
105
106
107
108
109
from requests import Response
from cycode.cli.models import CliError, CliErrors
class CycodeError(Exception):
"""Base class for all custom exceptions."""
def __str__(self) -> str:
class_name = self.__class__.__name__
return f'{class_name} error occurred.'
class RequestError(CycodeError): ...
class RequestTimeoutError(RequestError): ...
class RequestConnectionError(RequestError): ...
class RequestSslError(RequestConnectionError): ...
class RequestHttpError(RequestError):
def __init__(self, status_code: int, error_message: str, response: Response) -> None:
self.status_code = status_code
self.error_message = error_message
self.response = response
super().__init__(self.error_message)
def __str__(self) -> str:
return f'HTTP error occurred during the request (code {self.status_code}). Message: {self.error_message}'
class ScanAsyncError(CycodeError):
def __init__(self, error_message: str) -> None:
self.error_message = error_message
super().__init__(self.error_message)
def __str__(self) -> str:
return f'Async scan error occurred during the scan. Message: {self.error_message}'
class ReportAsyncError(CycodeError):
pass
class HttpUnauthorizedError(RequestHttpError):
def __init__(self, error_message: str, response: Response) -> None:
super().__init__(401, error_message, response)
def __str__(self) -> str:
return f'HTTP unauthorized error occurred during the request. Message: {self.error_message}'
class ZipTooLargeError(CycodeError):
def __init__(self, size_limit: int) -> None:
self.size_limit = size_limit
super().__init__()
def __str__(self) -> str:
return f'The size of zip to scan is too large, size limit: {self.size_limit}'
class AuthProcessError(CycodeError):
def __init__(self, error_message: str) -> None:
self.error_message = error_message
super().__init__()
def __str__(self) -> str:
return f'Something went wrong during the authentication process. Message: {self.error_message}'
class TfplanKeyError(CycodeError):
def __init__(self, file_path: str) -> None:
self.file_path = file_path
super().__init__()
def __str__(self) -> str:
return f'Error occurred while parsing terraform plan file. Path: {self.file_path}'
KNOWN_USER_FRIENDLY_REQUEST_ERRORS: CliErrors = {
RequestHttpError: CliError(
soft_fail=True,
code='cycode_error',
message='Cycode was unable to complete this scan. Please try again by executing the `cycode scan` command',
),
RequestTimeoutError: CliError(
soft_fail=True,
code='timeout_error',
message='The request timed out. Please try again by executing the `cycode scan` command',
),
HttpUnauthorizedError: CliError(
soft_fail=True,
code='auth_error',
message='Unable to authenticate to Cycode, your token is either invalid or has expired. '
'Please re-generate your token and reconfigure it by running the `cycode configure` command',
),
RequestSslError: CliError(
soft_fail=True,
code='ssl_error',
message='An SSL error occurred when trying to connect to the Cycode API. '
'If you use an on-premises installation or a proxy that intercepts SSL traffic '
'you should use the CURL_CA_BUNDLE environment variable to specify path to a valid .pem or similar',
),
}