Skip to content

Commit 46375ab

Browse files
committed
fix: normalize profile import timeouts
1 parent 55afa74 commit 46375ab

2 files changed

Lines changed: 44 additions & 23 deletions

File tree

lib/common/request.dart

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,35 @@ class Request {
6666
}
6767

6868
Future<Response<String>> getTextResponseForUrl(String url) async {
69-
final response = await _clashDio
70-
.get<String>(
71-
url,
72-
options: Options(
73-
responseType: ResponseType.plain,
74-
sendTimeout: profileRequestTimeoutDuration,
75-
receiveTimeout: profileRequestTimeoutDuration,
76-
),
77-
)
78-
.timeout(profileRequestTimeoutDuration);
79-
return response;
69+
try {
70+
return await _clashDio
71+
.get<String>(
72+
url,
73+
options: Options(
74+
responseType: ResponseType.plain,
75+
sendTimeout: profileRequestTimeoutDuration,
76+
receiveTimeout: profileRequestTimeoutDuration,
77+
),
78+
)
79+
.timeout(profileRequestTimeoutDuration);
80+
} catch (e) {
81+
commonPrint.log('getTextResponseForUrl error ${e.toString()}');
82+
if (e is TimeoutException) {
83+
throw appLocalizations.networkException;
84+
}
85+
if (e is DioException) {
86+
if (e.type == DioExceptionType.unknown) {
87+
throw appLocalizations.unknownNetworkError;
88+
} else if (e.type == DioExceptionType.badResponse ||
89+
e.type == DioExceptionType.connectionTimeout ||
90+
e.type == DioExceptionType.sendTimeout ||
91+
e.type == DioExceptionType.receiveTimeout) {
92+
throw appLocalizations.networkException;
93+
}
94+
rethrow;
95+
}
96+
throw appLocalizations.unknownNetworkError;
97+
}
8098
}
8199

82100
Future<MemoryImage?> getImage(String url) async {

lib/models/profile.dart

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,22 @@ extension ProfileExtension on Profile {
204204
final path = await appPath.tempFilePath;
205205
final tempFile = File(path);
206206
await tempFile.safeWriteAsBytes(bytes);
207-
final message = await coreController
208-
.validateConfig(path)
209-
.withTimeout(
210-
timeout: profileValidationTimeoutDuration,
211-
tag: 'validateConfig',
212-
);
213-
if (message.isNotEmpty) {
214-
throw message;
207+
try {
208+
final message = await coreController
209+
.validateConfig(path)
210+
.withTimeout(
211+
timeout: profileValidationTimeoutDuration,
212+
tag: 'validateConfig',
213+
);
214+
if (message.isNotEmpty) {
215+
throw message;
216+
}
217+
final mFile = await file;
218+
await tempFile.copy(mFile.path);
219+
return copyWith(lastUpdateDate: DateTime.now());
220+
} finally {
221+
await tempFile.safeDelete();
215222
}
216-
final mFile = await file;
217-
await tempFile.copy(mFile.path);
218-
await tempFile.safeDelete();
219-
return copyWith(lastUpdateDate: DateTime.now());
220223
}
221224

222225
Future<Profile> saveFileWithPath(String path) async {

0 commit comments

Comments
 (0)