forked from splunk-soar-connectors/digitalshadows
-
Notifications
You must be signed in to change notification settings - Fork 2
/
exception_handling_functions.py
59 lines (51 loc) · 2.28 KB
/
exception_handling_functions.py
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
# File: exception_handling_functions.py
#
# Licensed under Apache 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
#
import phantom.app as phantom
from digital_shadows_consts import (ERROR_CODE_MESSAGE, ERROR_MESSAGE_UNAVAILABLE, NON_NEGATIVE_INTEGER_MESSAGE, PARSE_ERROR_MESSAGE,
VALID_INTEGER_MESSAGE)
class ExceptionHandling:
def __init__(self, connector):
self.obj = connector
def _dump_error_log(self, error, message="Exception occurred."):
self.obj.error_print(message, dump_object=error)
def get_error_message_from_exception(self, e):
""" This method is used to get appropriate error message from the exception.
:param e: Exception object
:return: error message
"""
self._dump_error_log(e)
try:
if e.args:
if len(e.args) > 1:
error_code = e.args[0]
error_message = e.args[1]
elif len(e.args) == 1:
error_code = ERROR_CODE_MESSAGE
error_message = e.args[0]
else:
error_code = ERROR_CODE_MESSAGE
error_message = ERROR_MESSAGE_UNAVAILABLE
except:
error_code = ERROR_CODE_MESSAGE
error_message = ERROR_MESSAGE_UNAVAILABLE
try:
if error_code in ERROR_CODE_MESSAGE:
error_text = "Error Message: {0}".format(error_message)
else:
error_text = "Error Code: {0}. Error Message: {1}".format(error_code, error_message)
except:
error_text = PARSE_ERROR_MESSAGE
return error_text
def validate_integer(self, action_result, parameter, key):
if parameter:
try:
if not float(parameter).is_integer():
return action_result.set_status(phantom.APP_ERROR, VALID_INTEGER_MESSAGE.format(key=key)), None
parameter = int(parameter)
except:
return action_result.set_status(phantom.APP_ERROR, VALID_INTEGER_MESSAGE.format(key=key)), None
if parameter < 0:
return action_result.set_status(phantom.APP_ERROR, NON_NEGATIVE_INTEGER_MESSAGE.format(key=key)), None
return phantom.APP_SUCCESS, parameter