Skip to content

Commit 1d5bb8e

Browse files
DPDV-4642: Propagate error to the UI (#98)
* DPDV-4642: Propagate error to the UI * Improve error messages
1 parent 5b7d9d8 commit 1d5bb8e

File tree

2 files changed

+59
-29
lines changed

2 files changed

+59
-29
lines changed

TA_dataset/bin/dataset_common.py

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import json
33
import logging
44
import os.path as op
5-
import sys
65
import time
76
from logging import Logger
87
from typing import Optional # noqa: F401
@@ -54,22 +53,26 @@ def get_logger(session_key, suffix: str) -> Logger:
5453

5554
# one conf manager to rule them all
5655
def get_conf_manager(session_key, logger):
56+
realm = "__REST_CREDENTIAL__#{}#configs/conf-{}_settings".format(
57+
APP_NAME, CONF_NAME
58+
)
5759
try:
60+
logger.debug("Get conf manager - App: {}; Realm: {}".format(APP_NAME, realm))
5861
cfm = conf_manager.ConfManager(
5962
session_key,
6063
APP_NAME,
61-
realm="__REST_CREDENTIAL__#{}#configs/conf-{}_settings".format(
62-
APP_NAME, CONF_NAME
63-
),
64+
realm=realm,
6465
)
6566

6667
return cfm
6768

6869
except Exception as e:
69-
logger.error(
70-
"Failed to fetch configuration. Check permissions. error={}".format(e)
70+
msg = (
71+
"Failed to fetch configuration for realm {}. Check permissions. error={}"
72+
.format(realm, e)
7173
)
72-
sys.exit(1)
74+
logger.error(msg + " - %s", e, exc_info=True)
75+
raise Exception(msg) from e
7376

7477

7578
def get_log_level(session_key, logger):
@@ -88,10 +91,12 @@ def get_log_level(session_key, logger):
8891
)
8992
return log_level
9093

91-
except Exception:
94+
except Exception as e:
9295
logger.error(
9396
"Failed to fetch the log details from the configuration taking INFO as"
94-
" default level."
97+
" default level - %s",
98+
e,
99+
exc_info=True,
95100
)
96101
return "INFO"
97102

@@ -110,7 +115,7 @@ def get_proxy(session_key, logger):
110115
proxy_details = cfm.get_conf(CONF_NAME + "_settings").get("proxy")
111116
proxy_enabled = proxy_details.get("proxy_enabled", 0)
112117
except Exception as e:
113-
logger.debug("No proxy information defined: {}".format(e))
118+
logger.debug("No proxy information defined: {}".format(e), exc_info=True)
114119
return None
115120

116121
if int(proxy_enabled) == 0:
@@ -143,7 +148,7 @@ def get_proxy(session_key, logger):
143148
return proxies
144149

145150
except Exception as e:
146-
logger.info("Failed to fetch proxy information: {}".format(e))
151+
logger.info("Failed to fetch proxy information: {}".format(e), exc_info=True)
147152
return None
148153

149154

@@ -152,39 +157,43 @@ def get_acct_info(self, logger, account=None):
152157
"DataSetFunction={}, startTime={}".format("get_acct_info", time.time())
153158
)
154159
acct_dict = {}
160+
conf_name = "ta_dataset_account"
161+
155162
if account is not None:
156163
# wildcard to use all accounts
157164
if account == "*":
158165
try:
159-
confs = self.service.confs["ta_dataset_account"]
166+
confs = self.service.confs[conf_name]
160167
for conf in confs:
161168
acct_dict[conf.name] = {}
162169
acct_dict[conf.name]["base_url"] = conf.url
163170
acct_dict[conf.name]["ds_api_key"] = get_token(
164171
self, conf.name, "read", logger
165172
)
166173
except Exception as e:
167-
logger.error("Error retrieving add-on settings, error = {}".format(e))
168-
return None
174+
msg = "Error retrieving add-on settings, error = {}".format(e)
175+
logger.error(msg + " - %s", e, exc_info=True)
176+
raise Exception(msg) from e
169177
else:
170178
try:
171179
# remove spaces and split by commas
172180
account = account.replace(" ", "").split(",")
173181
for entry in account:
174-
conf = self.service.confs["ta_dataset_account"][entry]
182+
conf = self.service.confs[conf_name][entry]
175183
acct_dict[entry] = {}
176184
acct_dict[entry]["base_url"] = conf.url
177185
acct_dict[entry]["ds_api_key"] = get_token(
178186
self, entry, "read", logger
179187
)
180188
except Exception as e:
181-
logger.error("Error retrieving account settings, error = {}".format(e))
182-
return None
189+
msg = "Error retrieving account settings, error = {}".format(e)
190+
logger.error(msg + " - %s", e, exc_info=True)
191+
raise Exception(msg) from e
183192
# if account is not defined, try to get the first entry
184193
# (Splunk sorts alphabetically)
185194
else:
186195
try:
187-
confs = self.service.confs["ta_dataset_account"]
196+
confs = self.service.confs[conf_name]
188197
for conf in confs:
189198
acct_dict[conf.name] = {}
190199
acct_dict[conf.name]["base_url"] = conf.url
@@ -193,7 +202,12 @@ def get_acct_info(self, logger, account=None):
193202
)
194203
break
195204
except Exception as e:
196-
logger.error("Error retrieving settings, error = {}".format(e))
205+
msg = (
206+
"Error retrieving settings. Do you have at least one account in"
207+
" Configuration?, error = {}".format(e)
208+
)
209+
logger.error(msg + " - %s", e, exc_info=True)
210+
raise Exception(msg) from e
197211
logger.debug("DataSetFunction={}, endTime={}".format("get_acct_info", time.time()))
198212
return acct_dict
199213

@@ -222,7 +236,11 @@ def get_token(self, account, rw, logger):
222236
except Exception as e:
223237
logger.error(
224238
self,
225-
"Unable to retrieve API token, check configuration. error={}".format(e),
239+
"Unable to retrieve API token, check configuration. error={} - %s".format(
240+
e
241+
),
242+
e,
243+
exc_info=True,
226244
)
227245

228246

TA_dataset/bin/dataset_search_command.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def get_search_arguments(self):
119119

120120

121121
def search_error_exit(self, r_json: Union[Dict[str, Any], str]) -> None:
122-
logger().error(r_json)
122+
logger().error("search_error_exit: %r", r_json)
123123
if "message" in r_json:
124124
if r_json["message"].startswith("Couldn't decode API token"):
125125
error_message = ( # make API error more user-friendly
@@ -284,26 +284,36 @@ def generate(self):
284284
logger = get_logger(self.service.token, "search_command")
285285

286286
proxy = get_proxy(self.service.token, logger)
287-
acct_dict = get_acct_info(self, logger, ds_account)
288-
if acct_dict is None:
289-
search_error_exit(
290-
self, "Account token error, review search log for details"
291-
)
287+
try:
288+
acct_dict = get_acct_info(self, logger, ds_account)
289+
if not acct_dict:
290+
search_error_exit(
291+
self, "Account token error, review search log for details"
292+
)
293+
except Exception as e:
294+
search_error_exit(self, str(e))
292295

293296
for ds_acct in acct_dict.keys():
294297
try:
295298
ds_base_url = acct_dict[ds_acct]["base_url"]
299+
if not ds_base_url:
300+
raise Exception("Configuration error: URL is not specified")
296301
ds_url = get_url(ds_base_url, ds_method)
297302
ds_api_key = acct_dict[ds_acct]["ds_api_key"]
303+
if not ds_api_key:
304+
raise Exception(
305+
"Configuration error: Read access key is not specified"
306+
)
298307
ds_headers = {
299-
"Authorization": "Bearer " + acct_dict[ds_acct]["ds_api_key"],
308+
"Authorization": "Bearer " + ds_api_key,
300309
"User-Agent": get_user_agent(),
301310
}
302311
except Exception as e:
312+
logger.error("Cannot extract configuration: %s", e, exc_info=True)
303313
search_error_exit(
304314
self,
305-
"Splunk configuration error, see search log for details.error={}"
306-
.format(e),
315+
"Splunk configuration error when extracting account configuration,"
316+
" see search log for details. error={}".format(e),
307317
)
308318

309319
try:
@@ -478,8 +488,10 @@ def generate(self):
478488
)
479489
GeneratingCommand.flush
480490
except APIException as e:
491+
logger.error("API exception", e, exc_info=True)
481492
search_error_exit(self, e.payload)
482493
except Exception as e:
494+
logger.error("Cannot perform dataset command", e, exc_info=True)
483495
search_error_exit(self, str(e))
484496

485497

0 commit comments

Comments
 (0)