-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sks-menu): improve error handling
- Loading branch information
1 parent
3736a3d
commit 2e33cb2
Showing
6 changed files
with
99 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import "package:dio/dio.dart"; | ||
import "package:flutter/widgets.dart"; | ||
import "package:flutter_riverpod/flutter_riverpod.dart"; | ||
|
||
import "dio_client.dart"; | ||
|
||
class RestFrameworkOfflineException implements Exception { | ||
final String message; | ||
final String Function(BuildContext context) localizedMessage; | ||
final VoidCallback? onRetry; | ||
|
||
RestFrameworkOfflineException({ | ||
required this.localizedMessage, | ||
this.onRetry, | ||
this.message = "No Internet connection", | ||
}); | ||
|
||
@override | ||
String toString() => "RestFrameworkOfflineException: $message"; | ||
} | ||
|
||
extension DioSafeRequestsX on Ref { | ||
Future<Response<T>> safeRequest<T>( | ||
Future<Response<T>> Function() request, { | ||
required String Function(BuildContext context) localizedMessage, | ||
VoidCallback? onRetry, | ||
}) async { | ||
try { | ||
return await request(); | ||
} on DioException catch (_) { | ||
throw RestFrameworkOfflineException( | ||
localizedMessage: localizedMessage, | ||
onRetry: onRetry, | ||
); | ||
} | ||
} | ||
|
||
Future<Response<T>> safeGetWatch<T>( | ||
String url, { | ||
required String Function(BuildContext context) localizedMessage, | ||
VoidCallback? onRetry, | ||
}) async { | ||
final dio = watch(restClientProvider); | ||
return safeRequest( | ||
() => dio.get(url), | ||
localizedMessage: localizedMessage, | ||
onRetry: onRetry, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters