Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
Use rethrow for database exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
synw committed Nov 3, 2019
1 parent 92493cb commit cea8cc8
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions lib/src/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class KvStore {
try {
n = await _db.count(table: "kvstore", verbose: verbose);
} catch (e) {
throw ReadQueryException("Can not count keys in the store $e");
rethrow;
}
return n;
}
Expand All @@ -85,7 +85,7 @@ class KvStore {
final where = "key LIKE $expression%";
n = await _db.count(table: "kvstore", where: where, verbose: verbose);
} catch (e) {
throw ReadQueryException("Can not count keys in the store $e");
rethrow;
}
return n;
}
Expand All @@ -105,7 +105,7 @@ class KvStore {
final where = "key LIKE $expression%";
n = await _db.count(table: "kvstore", where: where, verbose: verbose);
} catch (e) {
throw ReadQueryException("Can not count keys in the store $e");
rethrow;
}
return n;
}
Expand All @@ -123,7 +123,7 @@ class KvStore {
try {
n = await _db.count(table: "kvstore", where: where, verbose: verbose);
} catch (e) {
throw ReadQueryException("Can not count keys in the store $e");
rethrow;
}
return n;
}
Expand All @@ -138,7 +138,7 @@ class KvStore {
table: "kvstore", where: 'key="$key"', verbose: verbose);
if (inMemory == true) _inMemoryStore.remove(key);
} catch (e) {
throw WriteQueryException("Can not delete data $e");
rethrow;
}
return deleted;
}
Expand All @@ -164,7 +164,7 @@ class KvStore {
}
}
} catch (e) {
throw ReadQueryException("Can not check hasKey in the store $e");
rethrow;
}
return has;
}
Expand Down Expand Up @@ -317,12 +317,13 @@ class KvStore {
columns: "key,value,type,list_type,map_key_type,map_value_type",
where: 'key="$key"',
verbose: verbose);
print("QRES $qres");
if (qres.isEmpty) {
return null;
}
res = qres[0];
} catch (e) {
throw ReadQueryException("Can not select data $e");
rethrow;
}
return res;
}
Expand All @@ -337,31 +338,28 @@ class KvStore {
throw ArgumentError(
"The value is of type ${value.runtimeType} and should be $T");
}

if (inMemory == true) {
_inMemoryStore[key] = value;
}
DatabaseEncodedRow encoded;
try {
if (inMemory == true) {
_inMemoryStore[key] = value;
}
DatabaseEncodedRow encoded;
try {
encoded = encode<T>(value);
} catch (e) {
throw EncodingException("Encoding $value failed: $e");
}
final Map<String, String> row = <String, String>{
"key": key,
"value": encoded.value,
"type": encoded.type,
"list_type": encoded.listType,
"map_key_type": encoded.mapKeyType,
"map_value_type": encoded.mapValueType
};
await _db
.upsert(table: "kvstore", row: row, verbose: verbose)
.catchError((dynamic e) {
throw WriteQueryException("Can not update store $e");
});
encoded = encode<T>(value);
} catch (e) {
throw EncodingException("Encoding $value failed: $e");
}
final Map<String, String> row = <String, String>{
"key": key,
"value": encoded.value,
"type": encoded.type,
"list_type": encoded.listType,
"map_key_type": encoded.mapKeyType,
"map_value_type": encoded.mapValueType
};
try {
await _db.upsert(table: "kvstore", row: row, verbose: verbose);
} catch (e) {
throw WriteQueryException("Can not upsert data $e");
rethrow;
}
}
}

0 comments on commit cea8cc8

Please sign in to comment.