Reputation: 9859
I have follow code. I need to handle Exception in it, but I can't do it. And can't understand the reason.
try {
print("aaa");
List<List<dynamic>> result =
await connection.query(sql).timeout(Duration(seconds: 120));
print('bbb');
if (result.isEmpty) {
sql = """ ... """;
await connection.query(sql);
} else {
int dbXmlId = result[0][0];
DateTime dbXMLDocDate = result[0][1];
if (dbXMLDocDate.isBefore(DateTime.parse(docDate!))) {
sql = """ ... """;
// print(sql);
await connection.query(sql);
sql = """ .... """;
currentJob['filesInserted']++;
await connection.query(sql);
} else {
sql = """ .... """;
await connection.query(sql);
}
}
} on PostgreSQLException catch (e) {
print('Exception during Insert in xml_files: $e');
exit(0);
} on SocketException catch (e) {
// пытаемся багу поймать
print('SocketException during Insert in xml_files: $e');
exit(0);
} on Exception catch (e) {
print('BaseException during Insert in xml_files: $e');
exit(0);
} catch (e) {
print("We should not reach this");
}
What I am getting on console:
aaa
Unhandled exception:
SocketException: Write failed (OS Error: An existing connection was forcibly closed by the remote host, errno = 10054), address = localhost, port = 51230
#0 _NativeSocket.write (dart:io-patch/socket_patch.dart:1182:34)
#1 _RawSocket.write (dart:io-patch/socket_patch.dart:1897:15)
#2 _Socket._write (dart:io-patch/socket_patch.dart:2334:18)
#3 _SocketStreamConsumer.write (dart:io-patch/socket_patch.dart:2082:26)
#4 _SocketStreamConsumer.addStream.<anonymous closure> (dart:io-patch/socket_patch.dart:2056:11)
#5 _RootZone.runUnaryGuarded (dart:async/zone.dart:1620:10)
#6 _BufferingStreamSubscription._sendData(dart:async/stream_impl.dart:341:11)
#7 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#8 _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:733:19)
#9 _StreamController._add (dart:async/stream_controller.dart:607:7)
#10 _StreamController.add (dart:async/stream_controller.dart:554:5)
#11 _StreamSinkImpl.add (dart:io/io_sink.dart:136:17)
#12 _Socket.add (dart:io-patch/socket_patch.dart:2181:38)
#13 Query.sendExtended (package:postgres/src/query.dart:98:12)
#14 _PostgreSQLConnectionStateIdle.processQuery (package:postgres/src/connection_fsm.dart:214:9)
#15 _PostgreSQLConnectionStateIdle.awake (package:postgres/srcconnection_fsm.dart:200:14)
#16 _PostgreSQLExecutionContextMixin._enqueue (package:postgres/srcconnection.dart:516:67)
#17 _PostgreSQLExecutionContextMixin._query (package:postgres/srcconnection.dart:460:15)
#18 _PostgreSQLExecutionContextMixin.query (package:postgres/srcconnection.dart:427:7)
#19 insertOrUpdateXmlFiles (package:data_loader/Servicesftp_service.dart:499:53)
#20 scanUnpackedFolder (package:data_loader/Services/ftp_service.dart:360:11)
<asynchronous suspension>
#21 unpackArchive (package:data_loader/Services/ftp_service.dart:320:6)
<asynchronous suspension>
#22 downloadArchive (package:data_loader/Services/ftp_service.dart:281:7)
<asynchronous suspension>
#23 runFtpParsingOrOnlyUnpackArchives (package:data_loader/Services/ftp_service.dart:736:5)
<asynchronous suspension>
#24 choiceOfProcessingAction (package:data_loader/Services/ftp_service.dart:658:9)
<asynchronous suspension>
#25 startNewJob (package:data_loader/Services/ftp_service.dart:762:5)
<asynchronous suspension>
#26 main.<anonymous closure> (file:///D:/code/zakupki/data_loader/bin/data_loader.dart:61:9)
<asynchronous suspension>
#27 Alfred._incomingRequest (package:alfred/src/alfred.dart:345:15)
<asynchronous suspension>
#28 _QueuedFuture.execute (package:queue/src/dart_queue_base.dart:26:16)
<asynchronous suspension>
I am seeing on console: aaa
but do not see bbb
and try-catch do not handle an Exception between them.
Full code: https://gist.github.com/bubnenkoff/7377f735671aacd624371a14151856f4
Upvotes: 3
Views: 170
Reputation: 31219
Solved though chat so this is a resume:
There have been created an issue for this problem here: https://github.com/isoos/postgresql-dart/issues/23
Initial testing shows that it might be because the package are using a synchronously completer, which is really a great way to get a lot of potential issues if you read the official documentation: https://api.dart.dev/stable/2.15.1/dart-async/Completer/Completer.sync.html
If the Completer
is changed to a normal asynchronously completer, it is possible to catch the exception from the question.
The issue might be solved in another way but the conclusion is that the problem (with not being able to catch the exceptions, not the exceptions themself) is very likely in the postgres
package and should be fixed here.
Upvotes: 1