Skip to content

Commit 26ff48e

Browse files
committed
improve onboarding error handling
1 parent 407fbe5 commit 26ff48e

1 file changed

Lines changed: 27 additions & 30 deletions

File tree

src/nrfcloud_utils/nrf_cloud_onboard.py

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -178,38 +178,34 @@ def get_onboarding_results(api_key, bulk_ops_req_id):
178178

179179
def parse_err_msg(err_str):
180180

181-
# Until the error msg is fixed by the cloud, we have to do some extra parsing...
182-
# See IRIS-3758
183-
184-
# Search for the end of the first item, which is just a general error msg
185-
err_begin_idx = err_str.find(ERR_FIND_FIRST_STR)
186-
187-
# Find the end of the detailed error list, which has escaped quotes
188-
err_end_idx = err_str.rfind(ERR_FIND_END_STR)
189-
190-
if err_begin_idx == -1 or err_end_idx == -1:
181+
# Parse JSON format: {"errors":["[0]: error message"]}
182+
try:
183+
err_json = json.loads(err_str)
184+
if "errors" in err_json and isinstance(err_json["errors"], list):
185+
# New format: simple list of error strings with indices
186+
err_dict = {}
187+
err_cnt = 0
188+
for err_item in err_json["errors"]:
189+
# Extract index from "[0]: message" format
190+
if err_item.startswith("[") and "]:" in err_item:
191+
idx_end = err_item.find("]:")
192+
try:
193+
idx = int(err_item[1:idx_end])
194+
msg = err_item[idx_end + 2:].strip()
195+
if msg not in err_dict:
196+
err_dict[msg] = []
197+
err_dict[msg].append(idx)
198+
err_cnt += 1
199+
except ValueError:
200+
pass
201+
if err_cnt > 0:
202+
return err_cnt, err_dict
203+
except (json.JSONDecodeError, AttributeError):
191204
logger.error("Unhandled error response format")
192205
return
193206

194-
# Inspect the first item for total number of reported errors
195-
err_begin_str = err_str[:err_begin_idx]
196-
err_cnt = 0
197-
for s in err_begin_str.split():
198-
if s.isdigit():
199-
err_cnt = int(s)
200-
break
201-
202-
if err_cnt == 0:
203-
logger.error("Warning: no errors reported")
204-
205-
# Get the start of the detailed error list, which is after the first item
206-
err_json_str = err_str[ (err_begin_idx + len(ERR_FIND_FIRST_STR)) : err_end_idx]
207-
208-
# Fix the escaped quotes
209-
err_json_str = literal_eval("'%s'" % err_json_str)
210-
211-
# Return the error count and the json formatted error dict
212-
return err_cnt, json.loads(err_json_str)
207+
logger.error("Unhandled error response format")
208+
return
213209

214210
def update_device_list_err(dev_list, err_dict):
215211

@@ -224,7 +220,7 @@ def update_device_list_err(dev_list, err_dict):
224220

225221
# Use the indicies to access the device list
226222
for dev_idx in idx_list:
227-
i = dev_idx -1
223+
i = dev_idx
228224
if i >= list_sz:
229225
logger.error("Reported device index out of range: {}".format(dev_idx))
230226
continue
@@ -434,6 +430,7 @@ def do_onboarding(api_key, csv_in, res_out, do_check):
434430

435431
if error_results.status_code == 200:
436432
# Parse the error message and update the device list
433+
logger.error("Onboarding errors:\n" + error_results.text)
437434
err_count, err_json = parse_err_msg(error_results.text)
438435
device_list = update_device_list_err(device_list, err_json)
439436
else:

0 commit comments

Comments
 (0)