mongo-dart / mongo_dart

Mongo_dart: MongoDB driver for Dart programming language
https://pub.dev/packages/mongo_dart
MIT License
442 stars 97 forks source link

Catching MongoDart Errors #301

Closed chasseuragace closed 1 year ago

chasseuragace commented 1 year ago

how do i catch Mongo Dart Errors?

Error On console Unhandled exception: MongoDart Error: Invalid document in UpdateOneStatement. The document is either null or contains invalid update operators

0 new UpdateOneStatement (package:mongo_dart/src/database/commands/query_and_write_operation_commands/wrapper/update_one/update_one_statement.dart:20:7)

1 DbCollection.updateOne (package:mongo_dart/src/database/dbcollection.dart:597:9)

2 AppDatabase.pinger (file:///Users/ajaydahal/AndroidStudioProjects/chasseur_api_server/app/database/db.dart:59:31)

3 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)

4 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)

5 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

where the triggering code is : _db?.collection("PING").updateOne({ "ping": "ping_timeStamp" }, { "ping": "ping_timeStamp", "time": DateTime.now().millisecondsSinceEpoch.toString() }

  I am aware that the updateone requires the codument to be present on the db .

  the concern is however : catching such error not correctictiong the code to update / insert.
  I simply want to catch errors!
giorgiofran commented 1 year ago

Not clear. In order to catch the error use a try catch statement ....

chasseuragace commented 1 year ago

I apologize for the unclear comment.

pinger(timer) async { print("PINGER IN ACTION"); try { await ensureDatabaseIsConnected(); _db?.collection("PING").update( {"ping": "ping_timeStamp"}, { "ping": "ping_timeStamp", "time": DateTime.now().millisecondsSinceEpoch.toString() }, ); } on MongoDartError catch (e, x) { print('handel error in pinger $e'); } on Error catch (e) { print('handel error in pinger $e'); // reconnect(); } catch (e) { print('handel Exception in pinger $e'); } } }

This is a piece of code that updates a document in a collection. and as you mentioned ; I am using try catch block; however , the "MongoDartError" aren't caught !

to test i modified a file on your package the file is "mongo_dart-0.7.4+1/lib/src/database/dbcollection.dart" as follows : line 65 and onwards Future<Map<String, dynamic>> update(selector, document, {bool upsert = false, bool multiUpdate = false, WriteConcern? writeConcern}) async { throw MongoDartError("custom error message"); // <------------------------------- ------- ----- I added this for test if (db._masterConnectionVerified.serverCapabilities.supportsOpMsg) { await modernUpdate(selector, document, upsert: upsert, multi: multiUpdate, writeConcern: writeConcern); return db._getAcknowledgement(); } return legacyUpdate(selector, document, upsert: upsert, multiUpdate: multiUpdate, writeConcern: writeConcern); }

expected : MongoDartError("custom error message") this to be caught result : Unhandled exception: MongoDart Error: custom error message

0 DbCollection.update (package:mongo_dart/src/database/dbcollection.dart:69:5)

1 AppDatabase.pinger (file:///Users/ajaydahal/AndroidStudioProjects/chasseur_api_server/app/database/db.dart:68:31)

chasseuragace commented 1 year ago

turns out i need to await the update statement in order to catch the error! 😁 might close the issue as well👍