mysql.dart
mysql.dart copied to clipboard
Catching SocketException
Hi. Thanks for your library.
Sometime I got SocketException and even if its wrapped in try catch the error will be thrown and can not be handled, this makes not able to return any response correctly. please could you fix it, any workaround?
SocketException: Operation timed out (OS Error: Operation timed out, errno = 60), address = ...
Good day/night. Can you provide some code?
Here the catch block never called
Future<Record<T>> first<T>(
String query, {
required T Function(Map<String, String?> dbResult) toModel,
Map<String, dynamic>? params,
}) async {
try {
final result = await pool.execute(query, params);
final value = result.rows.firstOrNull;
if (value == null) {
return Record.notExist();
}
return Record(toModel(value.assoc()));
} catch (e, s) {
logger.e('mysql: select first operation failed', e, s);
return Record.error(msg: e.toString(), error: e);
}
}
I tried to run runZonedGuarded
for the server, it will catch the error but no way to return response
await runZonedGuarded(
() => initServer(args),
(error, stack) {
logger.e('runZonedGuarded: ${error.toString()}', error, stack);
},
);
Its kind wired, in the log I see two SocketException thrown!
I think error is happening somewhere outside of your try catch block. Hard to tell right now
No, I tested a lot. the error coming from mysql and the address of mysql server will be printed in the exception
SocketException: Operation timed out (OS Error: Operation timed out, errno = 60), address = server address
just to reproduce same error:
- Sent a normal query, just to be sure there is one connection in the pool
- Turn off the WiFi
- Send another query and wait, it will throw
SocketException
- Send another query, this time it throw exception for host lockup, but it will catches without problem.
if try to add timeout to the request, when your WiFi is off, it will timeout then throw SocketException
I mean because of asynchronous nature of this library, exception is thrown outside execute method. I need more time to test this behaviour
I mean every request is multistep process, where request is send, and then asynchronously response is received.
Thank you for the time you put on checking this issue.
I found there is some bug in HttpClient on handling SocketException. According to this comment. You need to handle exception in onError callback. Maybe it help
Yes. I think this is the only way to handle such async errors. Library should be refactored a little to accept onError callback
Anyway this is something that can not be fixed in five minutes, so please if you can make some workaround temporary, provide your current solution here