Commit 2d9db329 by tanghuan

优化错误场景的处理

1 parent 552c2145
......@@ -5,6 +5,7 @@ import 'package:appframe/bloc/web_cubit.dart';
import 'package:appframe/config/env_config.dart';
import 'package:appframe/config/constant.dart';
import 'package:appframe/config/locator.dart';
import 'package:appframe/exception/exceptions.dart';
import 'package:appframe/services/api_service.dart';
import 'package:appframe/services/dispatcher.dart';
import 'package:appframe/utils/file_type_util.dart';
......@@ -49,22 +50,22 @@ class UploadStartHandler extends MessageHandler {
Future<dynamic> handleMessage(params) async {
try {
if (params is! Map<String, dynamic>) {
throw Exception('参数错误');
throw ParamErrorException();
}
final String? tempFilePath = params['tempFilePath'] as String?;
if (tempFilePath == null || tempFilePath.isEmpty) {
throw Exception('参数错误');
throw ParamErrorException();
}
final String? busi = params['busi'] as String?;
if (busi == null || busi.isEmpty) {
throw Exception('参数错误');
throw ParamErrorException();
}
final String? subBusi = params['subBusi'] as String?;
if (subBusi == null || subBusi.isEmpty) {
throw Exception('参数错误');
throw ParamErrorException();
}
// 开始处理前,先生成唯一 cmdUploadId
......@@ -81,12 +82,17 @@ class UploadStartHandler extends MessageHandler {
// 返回 null,让 MessageDispatcher 不处理返回指令
return null;
} on ParamErrorException {
debugPrint('====================>上传失败:参数错误');
throw Exception('参数错误');
} on ChunkUploadFailedException {
debugPrint('====================>上传失败:网络错误');
// 确保发送了 uploadEnd 指令
_webCubit?.sendUploadEnd(_cmdUnique, _cmdUploadId, '', errMsg: '网络错误,上传失败');
} catch (e) {
debugPrint('====================>上传失败:$e');
// 确保发送了 uploadEnd 指令
if (e is Exception && e.toString() != Exception('参数错误').toString()) {
_webCubit?.sendUploadEnd(_cmdUnique, _cmdUploadId, '', errMsg: e.toString());
}
_webCubit?.sendUploadEnd(_cmdUnique, _cmdUploadId, '', errMsg: e.toString());
} finally {
_unfollowCubit();
}
......@@ -367,12 +373,14 @@ class UploadStartHandler extends MessageHandler {
return {'idx': chunkIndex + 1, 'etag': etags[0]};
} else {
throw Exception('Chunk $chunkIndex upload failed: ${resp.statusCode}');
// throw Exception('Chunk $chunkIndex upload failed: ${resp.statusCode}');
throw Exception('Chunk upload failed');
}
} catch (e) {
debugPrint('====================> 分片$chunkIndex${attempt + 1}次, 上传失败:${e.toString()}');
if (attempt == maxRetries) {
throw Exception('Chunk $chunkIndex upload failed after $maxRetries attempts: $e');
// throw Exception('Chunk $chunkIndex upload failed after $maxRetries attempts: $e');
throw Exception('Chunk upload failed');
}
// 等待后重试
await Future.delayed(Duration(seconds: 2 * (attempt + 1)));
......@@ -431,7 +439,8 @@ class UploadStartHandler extends MessageHandler {
return response;
} catch (e) {
throw Exception('Chunk upload failed: $e');
// throw Exception('Chunk upload failed: $e');
throw Exception('Chunk upload failed');
}
}
......
class ParamErrorException implements Exception {
final String message;
ParamErrorException([this.message = '参数错误']);
@override
String toString() => 'ParamErrorException: $message';
}
class ChunkUploadFailedException implements Exception {
final String message;
ChunkUploadFailedException([this.message = 'Chunk upload failed']);
@override
String toString() => 'ChunkUploadFailedException: $message';
}
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!