Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[critical] mongo errors silently ignored #66

Open
timotheecour opened this issue Nov 17, 2018 · 1 comment
Open

[critical] mongo errors silently ignored #66

timotheecour opened this issue Nov 17, 2018 · 1 comment
Assignees

Comments

@timotheecour
Copy link
Contributor

timotheecour commented Nov 17, 2018

the following code should fail on the 2nd time doAssert ai is reached because insert fails (we're inserting with same _id):

import std/[
  oids,
  asyncdispatch,
]
import nimongo/bson
import nimongo/mongo

proc test_sync2=
  var m = newMongo()
  doAssert m.connect()
  let mc = m["tmp"]["tnimongo"]

  for i in 0..<2:
    let doc = %*{
      "name": "bob2",
      "_id": 123,
    }
    let ai = mc.insert(doc)
    echo ai
    doAssert ai

test_sync2()

here's the equivalent D code, which correctly throws:

/+dub.sdl:
dependency "vibe-d" version="~>0.8.0"
+/


import vibe.db.mongo.mongo;
import std.stdio;

void main(){
  test();
}


import vibe.data.bson;
import vibe.data.json;
import vibe.db.mongo.mongo;

void test()
{
  MongoClient client = connectMongoDB("127.0.0.1");
  MongoCollection users = client.getCollection("tmp.tnimongo");
  //users.insert(Bson(["name": "bob".Bson]));

  users.insert(Bson(["name": "bob2".Bson, "_id": 10.Bson]));
  /+
  this will (correctly) throw since insert fails
  vibe.db.mongo.connection.MongoDBException@../../../../../.dub/packages/vibe-d-0.8.4/vibe-d/mongodb/vibe/db/mongo/connection.d(472): E11000 duplicate key error collection: tmp.tnimongo index: _id_ dup key: { : 10 }
----------------
t10_mongo.d:30 pure @safe bool std.exception.enforce!(bool).enforce(bool, lazy object.Throwable) [0xf69b244]
../../../../../.dub/packages/vibe-d-0.8.4/vibe-d/mongodb/vibe/db/mongo/connection.d:472 @safe void vibe.db.mongo.connection.MongoConnection.checkForError(immutable(char)[]) [0xf6d4452]
../../../../../.dub/packages/vibe-d-0.8.4/vibe-d/mongodb/vibe/db/mongo/connection.d:241 @safe void vibe.db.mongo.connection.MongoConnection.insert(immutable(char)[], vibe.db.mongo.flags.InsertFlags, vibe.data.bson.Bson[]) [0xf6d3183]
t10_mongo.d:30 @safe void vibe.db.mongo.collection.MongoCollection.insert!(vibe.data.bson.Bson).insert(vibe.data.bson.Bson, vibe.db.mongo.flags.InsertFlags) [0xf6a2017]
t10_mongo.d:26 void t10_mongo.test() [0xf69651c]
t10_mongo.d:11 _Dmain [0xf696178]
  +/
  users.insert(Bson(["name": "bob2".Bson, "_id": 10.Bson]));

  foreach(a; users.find(["name": "bob"])){
    writeln(a);
  }
}

likewise with python, which also throws

note

echo ai prints this:
the ok: true should be ok: false, and it should fail

(ok: true, n: 1, err: "", inserted_ids: @[123], bson: {
    "n" : 1,
    "ok" : 1.0
})
(ok: true, n: 0, err: "", inserted_ids: @[123], bson: {
    "n" : 0,
    "writeErrors" : [
        {
            "index" : 0,
            "code" : 11000,
            "errmsg" : "E11000 duplicate key error collection: tmp.tnimongo index: _id_ dup key: { : 123 }"
        }
    ],
    "ok" : 1.0
})
@timotheecour
Copy link
Contributor Author

friendly ping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants