debug(mobile): 下载失败诊断日志

添加详细 debugPrint,覆盖:
- URL 检查失败原因
- 实际下载 URL 和保存路径
- HTTP 响应状态码和 content-length/content-range
- DioException 类型、消息、HTTP 状态码
- SHA-256 校验结果(预期值 vs 实际值)
- 文件大小

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-03-07 08:53:05 -08:00
parent 62c114c09f
commit d174b74764
1 changed files with 14 additions and 4 deletions

View File

@ -34,7 +34,7 @@ class DownloadManager {
}) async {
try {
if (!url.startsWith('https://')) {
debugPrint('Download URL must use HTTPS');
debugPrint('[DownloadManager] ERROR: URL must use HTTPS, got: $url');
_status = DownloadStatus.failed;
return null;
}
@ -51,12 +51,16 @@ class DownloadManager {
int downloadedBytes = 0;
if (await tempFile.exists()) {
downloadedBytes = await tempFile.length();
debugPrint('[DownloadManager] Resume from $downloadedBytes bytes');
}
if (await file.exists()) {
await file.delete();
}
debugPrint('[DownloadManager] Starting download: $url');
debugPrint('[DownloadManager] Save path: $savePath');
final response = await _dio.get<ResponseBody>(
url,
cancelToken: _cancelToken,
@ -70,9 +74,11 @@ class DownloadManager {
),
);
debugPrint('[DownloadManager] Response status: ${response.statusCode}');
int totalBytes = 0;
final contentLength = response.headers.value('content-length');
final contentRange = response.headers.value('content-range');
debugPrint('[DownloadManager] content-length=$contentLength content-range=$contentRange');
if (contentRange != null) {
final match = RegExp(r'bytes \d+-\d+/(\d+)').firstMatch(contentRange);
@ -104,25 +110,29 @@ class DownloadManager {
_status = DownloadStatus.verifying;
debugPrint('[DownloadManager] Verifying SHA-256, file size=${await file.length()}');
final isValid = await _verifySha256(file, sha256Expected);
if (!isValid) {
debugPrint('SHA-256 verification failed');
debugPrint('[DownloadManager] SHA-256 FAILED, expected=$sha256Expected');
await file.delete();
_status = DownloadStatus.failed;
return null;
}
debugPrint('[DownloadManager] SHA-256 OK');
_status = DownloadStatus.completed;
return file;
} on DioException catch (e) {
if (e.type == DioExceptionType.cancel) {
debugPrint('[DownloadManager] Cancelled by user');
_status = DownloadStatus.cancelled;
} else {
debugPrint('[DownloadManager] DioException type=${e.type} msg=${e.message} status=${e.response?.statusCode}');
_status = DownloadStatus.failed;
}
return null;
} catch (e) {
debugPrint('Download failed: $e');
} catch (e, stack) {
debugPrint('[DownloadManager] Unexpected error: $e\n$stack');
_status = DownloadStatus.failed;
return null;
}