@@ -249,6 +249,78 @@ void main() {
249249 await db.execute ('BEGIN' );
250250 }, throwsA ((e) => e is sqlite.SqliteException ));
251251 });
252+
253+ test ('should handle normal errors' , () async {
254+ final db = await setupDatabase (path: path);
255+ await createTables (db);
256+ Error ? caughtError;
257+ final syntheticError = ArgumentError ('foobar' );
258+ await db.computeWithDatabase <void >((db) async {
259+ throw syntheticError;
260+ }).catchError ((error) {
261+ caughtError = error;
262+ });
263+ expect (caughtError.toString (), equals (syntheticError.toString ()));
264+
265+ // Check that we can still continue afterwards
266+ final computed = await db.computeWithDatabase ((db) async {
267+ return 5 ;
268+ });
269+ expect (computed, equals (5 ));
270+ });
271+
272+ test ('should handle uncaught errors' , () async {
273+ final db = await setupDatabase (path: path);
274+ await createTables (db);
275+ Object ? caughtError;
276+ await db.computeWithDatabase <void >((db) async {
277+ Future <void > asyncCompute () async {
278+ throw ArgumentError ('uncaught async error' );
279+ }
280+
281+ asyncCompute ();
282+ }).catchError ((error) {
283+ caughtError = error;
284+ });
285+ // This may change into a better error in the future
286+ expect (caughtError.toString (), equals ("Instance of 'ClosedException'" ));
287+
288+ // Check that we can still continue afterwards
289+ final computed = await db.computeWithDatabase ((db) async {
290+ return 5 ;
291+ });
292+ expect (computed, equals (5 ));
293+ });
294+
295+ test ('should handle uncaught errors in read connections' , () async {
296+ final db = await setupDatabase (path: path);
297+ await createTables (db);
298+ for (var i = 0 ; i < 10 ; i++ ) {
299+ Object ? caughtError;
300+
301+ await db.readTransaction ((ctx) async {
302+ await ctx.computeWithDatabase ((db) async {
303+ Future <void > asyncCompute () async {
304+ throw ArgumentError ('uncaught async error' );
305+ }
306+
307+ asyncCompute ();
308+ });
309+ }).catchError ((error) {
310+ caughtError = error;
311+ });
312+ // This may change into a better error in the future
313+ expect (caughtError.toString (), equals ("Instance of 'ClosedException'" ));
314+ }
315+
316+ // Check that we can still continue afterwards
317+ final computed = await db.readTransaction ((ctx) async {
318+ return await ctx.computeWithDatabase ((db) async {
319+ return 5 ;
320+ });
321+ });
322+ expect (computed, equals (5 ));
323+ });
252324 });
253325}
254326
0 commit comments