extractSupabaseList<T extends SupabaseObject> static method

List<T> extractSupabaseList<T extends SupabaseObject>(
  1. List<Map<String, dynamic>> response, {
  2. bool throwForNonExtracted = false,
})

Extracts a list of SupabaseObjects from a response. If some records could not be extracted, ExtractionFailedException is thrown containing the extracted records and the faulty records.

Implementation

static List<T> extractSupabaseList<T extends SupabaseObject>(
  List<Map<String, dynamic>> response, {
  bool throwForNonExtracted = false,
}) {
  final extracted = <T>[];
  final notExtracted = <JsonWithError>[];
  for (final json in response) {
    try {
      extracted.add(SupabaseObjectFunctions.fromJson<T>(json));
      // ignore: avoid_catching_errors
    } on ArgumentError catch (error) {
      // We are catching ArgumentError because unknown enums throw an ArgumentError
      // and UnknownJsonTypeError is a subclass of ArgumentError
      notExtracted.add(JsonWithError(json, error));
    }
  }
  if (notExtracted.isNotEmpty) {
    StudyULogger.warning(
      'Some records could not be extracted: ${notExtracted.length} errors',
    );
    StudyULogger.debug(
      'Not extracted records: ${notExtracted.map((e) => e.json).join(', ')}',
    );
    StudyUDiagnostics.captureException(
      ExtractionFailedException(extracted, notExtracted),
    );
    // Only throw if we are supposed to throw for non-extracted records.
    // Otherwise, we just log the error and return the extracted records.
    if (throwForNonExtracted) {
      // If some records could not be extracted, we throw an exception
      // with the extracted records and the faulty records
      throw ExtractionFailedException(extracted, notExtracted);
    }
  }
  return extracted;
}