diff --git a/packages/brick_offline_first_with_graphql/lib/src/graphql_offline_queue_link.dart b/packages/brick_offline_first_with_graphql/lib/src/graphql_offline_queue_link.dart index da995143..7247e7a7 100644 --- a/packages/brick_offline_first_with_graphql/lib/src/graphql_offline_queue_link.dart +++ b/packages/brick_offline_first_with_graphql/lib/src/graphql_offline_queue_link.dart @@ -15,8 +15,17 @@ class GraphqlOfflineQueueLink extends Link { final GraphqlRequestSqliteCacheManager requestManager; - GraphqlOfflineQueueLink(this.requestManager) - : _logger = Logger('GraphqlOfflineQueueLink#${requestManager.databaseName}'); + /// A callback triggered when a request failed, but will be reattempted. + final void Function(Request request, int statusCode)? onReattemptableResponse; + + /// A callback triggered when a request throws an exception during execution. + final void Function(Request request, Object error)? onRequestError; + + GraphqlOfflineQueueLink( + this.requestManager, { + this.onReattemptableResponse, + this.onRequestError, + }) : _logger = Logger('GraphqlOfflineQueueLink#${requestManager.databaseName}'); @override Stream request(Request request, [NextLink? forward]) async* { @@ -33,6 +42,7 @@ class GraphqlOfflineQueueLink extends Link { yield* forward!(request).handleError( (e) async { _logger.warning('GraphqlOfflineQueueLink#request: error $e'); + onRequestError?.call(request, e); final db = await requestManager.getDb(); await cacheItem.unlock(db); }, @@ -46,6 +56,8 @@ class GraphqlOfflineQueueLink extends Link { // request was successfully sent and can be removed _logger.finest('removing from queue: ${cacheItem.toSqlite()}'); await cacheItem.delete(db); + } else { + onReattemptableResponse?.call(request, response.response['statusCode'] as int); } final db = await requestManager.getDb(); await cacheItem.unlock(db);