forked from digitalshadows/splunk-soar-digitalshadows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exception_handling_functions.py
52 lines (47 loc) · 1.99 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
# 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 (ERR_CODE_MSG, ERR_MSG_UNAVAILABLE,
NON_NEGATIVE_INTEGER_MSG, PARSE_ERR_MSG,
VALID_INTEGER_MSG)
class ExceptionHandling:
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
"""
try:
if e.args:
if len(e.args) > 1:
error_code = e.args[0]
error_msg = e.args[1]
elif len(e.args) == 1:
error_code = ERR_CODE_MSG
error_msg = e.args[0]
else:
error_code = ERR_CODE_MSG
error_msg = ERR_MSG_UNAVAILABLE
except:
error_code = ERR_CODE_MSG
error_msg = ERR_MSG_UNAVAILABLE
try:
if error_code in ERR_CODE_MSG:
error_text = "Error Message: {0}".format(error_msg)
else:
error_text = "Error Code: {0}. Error Message: {1}".format(error_code, error_msg)
except:
error_text = PARSE_ERR_MSG
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_MSG.format(key=key)), None
parameter = int(parameter)
except:
return action_result.set_status(phantom.APP_ERROR, VALID_INTEGER_MSG.format(key=key)), None
if parameter < 0:
return action_result.set_status(phantom.APP_ERROR, NON_NEGATIVE_INTEGER_MSG.format(key=key)), None
return phantom.APP_SUCCESS, parameter