Skip to content

Commit

Permalink
feature: refined error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mirageoasis committed Dec 26, 2023
1 parent 16d2d07 commit bbce4fa
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/ai/backend/storage/api/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

import asyncio
import errno
import json
import logging
import os
Expand Down Expand Up @@ -783,26 +784,48 @@ class Params(TypedDict):
exist_ok = params["exist_ok"]
relpath = params["relpath"]
relpaths = relpath if isinstance(relpath, list) else [relpath]
error_file_list: List[str] = []
success_file_list: List[str] = []

async with ctx.get_volume(params["volume"]) as volume:
with handle_fs_errors(volume, vfid):
mkdir_tasks = [
volume.mkdir(vfid, rpath, parents=parents, exist_ok=exist_ok)
for rpath in relpaths
]
result_group = await asyncio.gather(*mkdir_tasks, return_exceptions=True)
mkdir_tasks = [
volume.mkdir(vfid, rpath, parents=parents, exist_ok=exist_ok) for rpath in relpaths
]
result_group = await asyncio.gather(*mkdir_tasks, return_exceptions=True)

# if result group is all false throw error.
if all([isinstance(res, FileExistsError) for res in result_group]):
raise FileExistsError("None of directories created because they already exist")
# if result group is all false throw error.
if all([isinstance(res, FileExistsError) for res in result_group]):
raise web.HTTPBadRequest(
body=json.dumps(
{
"msg": "FileExistsError None of directories created because they already exist",
"errno": errno.EEXIST,
},
),
content_type="application/json",
)

for result_or_exception in result_group:
if any([isinstance(res, BaseException) for res in result_group]):
for relpath, result_or_exception in zip(relpaths, result_group):
if isinstance(result_or_exception, BaseException):
log.error(
"%s",
"{}",
repr(result_or_exception),
exc_info=result_or_exception,
)
error_file_list.append(f"[Errno {errno.EEXIST}] File exists: {relpath}")
else:
success_file_list.append(str(relpath))

raise web.HTTPBadRequest(
body=json.dumps(
{
"error_file_list": error_file_list,
"success_file_list": success_file_list,
},
),
content_type="application/json",
)

return web.Response(status=204)

Expand Down

0 comments on commit bbce4fa

Please sign in to comment.