Repository: TencentBlueKing/bk-user

Issue:
[优化] HTTP API 数据源同步报错
对应API 没有按照要求, 返回`count/results`, 此时取空

实际上这里的逻辑会`results`的第一条, 处理; 但是没有很好地校验

会报错

```
Task bkuser_core.categories.tasks.adapter_sync[78744136-5e83-4605-8503-d4d8ed90f9e3] raised unexpected: IndexError('list index out of range',)
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/celery/app/trace.py", line 412, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/celery/app/trace.py", line 704, in __protected_call__
    return self.run(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/celery/app/autoretry.py", line 50, in run
    raise task.retry(exc=exc, **retry_kwargs)
  File "/usr/local/lib/python3.6/site-packages/celery/app/task.py", line 706, in retry
    raise_with_context(exc)
  File "/usr/local/lib/python3.6/site-packages/celery/app/autoretry.py", line 35, in run
    return task._orig_run(*args, **kwargs)
  File "/app/bkuser_core/categories/tasks.py", line 111, in adapter_sync
    plugin.sync(instance_id=instance_id, task_id=task_id, *args, **kwargs)
  File "/app/bkuser_core/categories/plugins/plugin.py", line 124, in sync
    syncer.sync(*args, **kwargs)
  File "/app/bkuser_core/categories/plugins/custom/sycner.py", line 45, in sync
    self._sync_department()
  File "/app/bkuser_core/categories/plugins/custom/sycner.py", line 50, in _sync_department
    self._load2sync_manager(self.client.fetch_departments())
  File "/app/bkuser_core/categories/plugins/custom/sycner.py", line 74, in _load2sync_manager
    category=self.category, db_sync_manager=self.db_sync_manager, items=items, context=self.context
  File "/app/bkuser_core/categories/plugins/custom/helpers.py", line 277, in init_helper
    return _map[items.custom_type](
  File "/app/bkuser_core/categories/plugins/custom/models.py", line 81, in custom_type
    return type(list(self.items_map.values())[0])
IndexError: list index out of range
```


Discussion:
- [ ] 做准入校验, 而不是扩散到 N 层抽象封装执行时报错
- [ ] 有没有版本系统性解决这种问题? 排查问题过于困难, 完全靠经验(看代码时间成本/调试成本巨大, 如果外部用户遇到, 拿到报错堆栈帮助也不大)
https://github.com/TencentBlueKing/bk-user/pull/400
`results`为空的时候, 还是会报错

需要有机制 => 这是正常的返回? 还是应该认为异常不处理? (即, 同步接口空, 会导致所有用户被软删除, 系统不可用)

Resolving patch:
diff --git a/src/api/bkuser_core/categories/plugins/custom/client.py b/src/api/bkuser_core/categories/plugins/custom/client.py
--- a/src/api/bkuser_core/categories/plugins/custom/client.py
+++ b/src/api/bkuser_core/categories/plugins/custom/client.py
@@ -72,6 +72,12 @@ def _fetch_items(self, path: str):
             logger.error("`results` in response is not a list, cUrl format: %s", curl_format)
             raise CustomAPIRequestFailed("the `results` in response is not a list")
 
+        # currently, if the results is empty, CustomTypeList.custom_type will raise IndexError(task fail)
+        # so, here, we should check here: results size should not be empty
+        if not results:
+            logger.error("`results` in response is empty, cUrl format: %s", curl_format)
+            raise CustomAPIRequestFailed("the `results` in response is empty")
+
         return results
 
     def fetch_profiles(self, page_info: Optional[PageInfo] = None) -> CustomTypeList:


Test patch: