From be57e1640141fe00e5936ae10eb3c51b2eae9fb0 Mon Sep 17 00:00:00 2001 From: Tim Shedor Date: Fri, 13 Sep 2024 13:04:50 -0700 Subject: [PATCH] fix(supabase): include request in generic error to handle offline upsert #439 --- .../CHANGELOG.md | 1 + .../rest_offline_queue_client.dart | 1 + ...ffline_first_with_supabase_repository.dart | 21 ++++++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/brick_offline_first_with_rest/CHANGELOG.md b/packages/brick_offline_first_with_rest/CHANGELOG.md index b05c5d8f..27cb420a 100644 --- a/packages/brick_offline_first_with_rest/CHANGELOG.md +++ b/packages/brick_offline_first_with_rest/CHANGELOG.md @@ -3,6 +3,7 @@ ## 3.1.0 - Expose offline queue functionality in `offline_queue.dart` +- Include `request` in `RestOfflineQueueClient`'s generic error response ## 3.0.2 diff --git a/packages/brick_offline_first_with_rest/lib/src/offline_queue/rest_offline_queue_client.dart b/packages/brick_offline_first_with_rest/lib/src/offline_queue/rest_offline_queue_client.dart index 6c186b26..a1f92da4 100644 --- a/packages/brick_offline_first_with_rest/lib/src/offline_queue/rest_offline_queue_client.dart +++ b/packages/brick_offline_first_with_rest/lib/src/offline_queue/rest_offline_queue_client.dart @@ -55,6 +55,7 @@ class RestOfflineQueueClient extends http.BaseClient { final genericErrorResponse = http.StreamedResponse( Stream.fromFuture(Future.value('unknown internal error'.codeUnits)), 501, + request: request, ); try { diff --git a/packages/brick_offline_first_with_supabase/lib/src/offline_first_with_supabase_repository.dart b/packages/brick_offline_first_with_supabase/lib/src/offline_first_with_supabase_repository.dart index 4dcbd4f0..70bd246c 100644 --- a/packages/brick_offline_first_with_supabase/lib/src/offline_first_with_supabase_repository.dart +++ b/packages/brick_offline_first_with_supabase/lib/src/offline_first_with_supabase_repository.dart @@ -5,6 +5,7 @@ import 'package:brick_supabase/brick_supabase.dart'; import 'package:http/http.dart' as http; import 'package:meta/meta.dart'; import 'package:sqflite_common/sqlite_api.dart' show DatabaseFactory; +import 'package:supabase/supabase.dart'; /// Ensures the [remoteProvider] is a [SupabaseProvider]. /// @@ -79,6 +80,25 @@ abstract class OfflineFirstWithSupabaseRepository await offlineRequestQueue.client.requestManager.migrate(); } + @override + Future upsert( + TModel instance, { + OfflineFirstUpsertPolicy policy = OfflineFirstUpsertPolicy.optimisticLocal, + Query? query, + }) async { + try { + return await super.upsert(instance, policy: policy, query: query); + } on PostgrestException catch (e) { + logger.warning('#upsert supabase failure: $e'); + + if (policy == OfflineFirstUpsertPolicy.requireRemote) { + throw OfflineFirstException(Exception(e)); + } + + return instance; + } + } + /// This is a convenience method to create the basic offline client and queue. /// The client is used to add offline capabilities to [SupabaseProvider]; /// the queue is used to add offline to the repository. @@ -96,7 +116,6 @@ abstract class OfflineFirstWithSupabaseRepository 409, 429, 500, - 501, 502, 503, 504,