Skip to content

Commit

Permalink
refactor: create abstract exception and rename ExtractedSupabaseListR…
Browse files Browse the repository at this point in the history
…esult
  • Loading branch information
johannesvedder committed Feb 29, 2024
1 parent 69b7215 commit 5ef1bc4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/lib/screens/study/onboarding/study_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ class StudySelectionScreen extends StatelessWidget {
),
),
Expanded(
child: RetryFutureBuilder<ExtractedSupabaseListResult<Study>>(
child: RetryFutureBuilder<ExtractingFailedException<Study>>(
tryFunction: () async => Study.publishedPublicStudies(),
successBuilder: (BuildContext context, ExtractedSupabaseListResult<Study>? studies) {
successBuilder: (BuildContext context, ExtractingFailedException<Study>? studies) {
if (studies!.notExtracted.isNotEmpty) {
debugPrint('${studies.notExtracted.length} studies could not be extracted.');
ScaffoldMessenger.of(context).showSnackBar(
Expand Down
6 changes: 3 additions & 3 deletions core/lib/src/models/tables/study.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ class Study extends SupabaseObjectFunctions<Study> implements Comparable<Study>
);

// ['id', 'title', 'description', 'published', 'icon_name', 'results', 'schedule']
static Future<ExtractedSupabaseListResult<Study>> publishedPublicStudies() async {
static Future<ExtractingFailedException<Study>> publishedPublicStudies() async {
List<Study> extractedStudies;
List<JsonWithError> notExtracted = [];
try {
final response = await env.client.from(tableName).select().eq('participation', 'open');
extractedStudies = SupabaseQuery.extractSupabaseList<Study>(List<Map<String, dynamic>>.from(response));
} on ExtractedSupabaseListResult<Study> catch (error) {
} on ExtractingFailedException<Study> catch (error) {
// If some records could not be extracted, catch the exception and
// return the extracted records and the faulty records
extractedStudies = error.extracted;
Expand All @@ -182,7 +182,7 @@ class Study extends SupabaseObjectFunctions<Study> implements Comparable<Study>
SupabaseQuery.catchSupabaseException(error, stacktrace);
rethrow;
}
return ExtractedSupabaseListResult<Study>(extractedStudies, notExtracted);
return ExtractingFailedException<Study>(extractedStudies, notExtracted);
}

bool isOwner(User? user) => user != null && userId == user.id;
Expand Down
10 changes: 7 additions & 3 deletions core/lib/src/util/supabase_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class SupabaseQuery {
if (notExtracted.isNotEmpty) {
// If some records could not be extracted, we throw an exception
// with the extracted records and the faulty records
throw ExtractedSupabaseListResult(extracted, notExtracted);
throw ExtractingFailedException(extracted, notExtracted);
}
return extracted;
}
Expand Down Expand Up @@ -138,11 +138,15 @@ extension PrimaryKeyFilterBuilder on PostgrestFilterBuilder {
}
}

class ExtractedSupabaseListResult<T> {
abstract class IExtractingFailedException<T> implements Exception {
const IExtractingFailedException(this.extracted, this.notExtracted);

final List<T> extracted;
final List<JsonWithError> notExtracted;
}

ExtractedSupabaseListResult(this.extracted, this.notExtracted);
class ExtractingFailedException<T> extends IExtractingFailedException<T> {
ExtractingFailedException(super.extracted, super.notExtracted);
}

class JsonWithError {
Expand Down

0 comments on commit 5ef1bc4

Please sign in to comment.