discoverError static method

Map<String, List<String>> discoverError(
  1. List<Map<String, Object?>> response
)

Implementation

static Map<String, List<String>> discoverError(
  List<Map<String, Object?>> response,
) {
  Map<String, List<String>> res = {};

  for (final resp in response) {
    if (resp.containsKey('writeErrors') && resp['writeErrors'] is List) {
      final writeErrors = resp['writeErrors'] as List;
      for (final writeError in writeErrors) {
        String error = mongoDBErrorCodes[writeError['code']]?[#name] ??
            'mongo.error.unknown';
        final Map<String, Object?> keyPattern =
            (writeError['keyPattern'] != null &&
                    writeError['keyPattern'] is Map)
                ? writeError['keyPattern']
                : {};
        List<String> fields = keyPattern.keys.toList();
        fields.forEach((field) {
          if (!res.containsKey(field)) {
            res[field] = [];
          }
          res[field]!.add(error);
        });
      }
    }
  }

  return res;
}