fix: 文件被占用或磁盘不足时改为中文提示
This commit is contained in:
@@ -9,6 +9,21 @@ import os
|
|||||||
import threading
|
import threading
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from app.platform.client import NqiError
|
||||||
|
|
||||||
|
|
||||||
|
def explain_os_error(exc: BaseException) -> str | None:
|
||||||
|
"""把常见的 Windows 文件错误转成中文。其他错误返回 None。"""
|
||||||
|
if not isinstance(exc, OSError):
|
||||||
|
return None
|
||||||
|
winerror = getattr(exc, "winerror", None)
|
||||||
|
errno = getattr(exc, "errno", None)
|
||||||
|
if winerror in {32, 33} or errno in {32, 33}:
|
||||||
|
return "文件被占用,请先关闭 Excel 等程序"
|
||||||
|
if winerror in {112, 28} or errno in {112, 28}:
|
||||||
|
return "磁盘空间不足"
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _id_key(value) -> int | str:
|
def _id_key(value) -> int | str:
|
||||||
if isinstance(value, bool):
|
if isinstance(value, bool):
|
||||||
@@ -31,6 +46,13 @@ class _IdSet(set):
|
|||||||
return super().__contains__(_id_key(value))
|
return super().__contains__(_id_key(value))
|
||||||
|
|
||||||
|
|
||||||
|
def _raise_os(exc: OSError) -> None:
|
||||||
|
message = explain_os_error(exc)
|
||||||
|
if message:
|
||||||
|
raise NqiError(message) from exc
|
||||||
|
raise exc
|
||||||
|
|
||||||
|
|
||||||
class CsvJournal:
|
class CsvJournal:
|
||||||
def __init__(self, csv_path: Path, progress_path: Path, headers: list[str]):
|
def __init__(self, csv_path: Path, progress_path: Path, headers: list[str]):
|
||||||
self.csv_path = csv_path
|
self.csv_path = csv_path
|
||||||
@@ -47,6 +69,9 @@ class CsvJournal:
|
|||||||
self._close_handles()
|
self._close_handles()
|
||||||
try:
|
try:
|
||||||
self._open_locked()
|
self._open_locked()
|
||||||
|
except OSError as exc:
|
||||||
|
self._close_handles()
|
||||||
|
_raise_os(exc)
|
||||||
except Exception:
|
except Exception:
|
||||||
self._close_handles()
|
self._close_handles()
|
||||||
raise
|
raise
|
||||||
@@ -115,9 +140,9 @@ class CsvJournal:
|
|||||||
self._progress.write(b"".join(lines))
|
self._progress.write(b"".join(lines))
|
||||||
self._progress.flush()
|
self._progress.flush()
|
||||||
os.fsync(self._progress.fileno())
|
os.fsync(self._progress.fileno())
|
||||||
except OSError:
|
except OSError as exc:
|
||||||
self._rollback(start, progress_at)
|
self._rollback(start, progress_at)
|
||||||
raise
|
_raise_os(exc)
|
||||||
for key, row_count, _blob in prepared:
|
for key, row_count, _blob in prepared:
|
||||||
self.completed.add(key)
|
self.completed.add(key)
|
||||||
self.written_rows += row_count
|
self.written_rows += row_count
|
||||||
@@ -148,9 +173,9 @@ class CsvJournal:
|
|||||||
self._progress.flush()
|
self._progress.flush()
|
||||||
if sync:
|
if sync:
|
||||||
os.fsync(self._progress.fileno())
|
os.fsync(self._progress.fileno())
|
||||||
except OSError:
|
except OSError as exc:
|
||||||
self._rollback(start, progress_at)
|
self._rollback(start, progress_at)
|
||||||
raise
|
_raise_os(exc)
|
||||||
self.written_rows += len(rows)
|
self.written_rows += len(rows)
|
||||||
self.completed.add(key)
|
self.completed.add(key)
|
||||||
return len(rows)
|
return len(rows)
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import os
|
|||||||
import threading
|
import threading
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from app.features.download.journal import CsvJournal
|
from app.features.download.journal import CsvJournal, explain_os_error
|
||||||
|
from app.platform.client import NqiError
|
||||||
|
|
||||||
|
|
||||||
def test_progress_recovers_by_streaming_and_truncates_only_a_bad_tail(tmp_path: Path, monkeypatch):
|
def test_progress_recovers_by_streaming_and_truncates_only_a_bad_tail(tmp_path: Path, monkeypatch):
|
||||||
@@ -103,6 +104,38 @@ def test_commit_many_is_thread_safe(tmp_path: Path):
|
|||||||
assert {row[0] for row in rows[1:]} == {"甲", "乙"}
|
assert {row[0] for row in rows[1:]} == {"甲", "乙"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_file_errors_use_chinese(tmp_path: Path, monkeypatch):
|
||||||
|
busy = OSError("busy")
|
||||||
|
busy.winerror = 32
|
||||||
|
locked = OSError("locked")
|
||||||
|
locked.winerror = 33
|
||||||
|
full = OSError("full")
|
||||||
|
full.winerror = 112
|
||||||
|
full_errno = OSError("full")
|
||||||
|
full_errno.errno = 28
|
||||||
|
assert explain_os_error(busy) == "文件被占用,请先关闭 Excel 等程序"
|
||||||
|
assert explain_os_error(locked) == "文件被占用,请先关闭 Excel 等程序"
|
||||||
|
assert explain_os_error(full) == "磁盘空间不足"
|
||||||
|
assert explain_os_error(full_errno) == "磁盘空间不足"
|
||||||
|
|
||||||
|
journal = CsvJournal(tmp_path / "out.csv", tmp_path / "progress.jsonl", ["栅格"])
|
||||||
|
journal.open()
|
||||||
|
|
||||||
|
def deny(_fd):
|
||||||
|
error = PermissionError(13, "busy")
|
||||||
|
error.winerror = 32
|
||||||
|
raise error
|
||||||
|
|
||||||
|
monkeypatch.setattr("app.features.download.journal.os.fsync", deny)
|
||||||
|
try:
|
||||||
|
journal.write_rows("1", [["1"]])
|
||||||
|
except NqiError as exc:
|
||||||
|
assert str(exc) == "文件被占用,请先关闭 Excel 等程序"
|
||||||
|
else:
|
||||||
|
raise AssertionError("占用文件时应给出中文")
|
||||||
|
journal.close()
|
||||||
|
|
||||||
|
|
||||||
def _patch_fsync(monkeypatch):
|
def _patch_fsync(monkeypatch):
|
||||||
calls = []
|
calls = []
|
||||||
original = os.fsync
|
original = os.fsync
|
||||||
|
|||||||
Reference in New Issue
Block a user