MongoSwiftSync driver. MongoCollection.insertMany(). Cast from 'any Error' to unrelated type 'MongoError.BulkWriteError' always fails

Hi all. I’m in Swift 5.8, in Xcode 14.3, on macOS 13.3.1, MacBook Pro M1 Max. Locally hosted MongoDb Community Server 6.
I have this function:

func fInsertSome(_ sCollection: String, documents: [some Codable]) throws {
	let zCollection = zDb.collection(sCollection.lowercased()) // returns actual collection from collection name
	let azDocuments = try documents.map { try BSONEncoder().encode($0) }
	do {
		try zCollection.insertMany(azDocuments)
	}
	catch {
		if let errorCode = (error as? MongoSwift.MongoError.BulkWriteError)?.writeFailures?.first?.code, errorCode == 11000 {
			// one of the documents has a duplicate key.  If so, switch to writing them one by one, and handle each one individually:
			for document in documents {
				do {
					let zDocument = try BSONEncoder().encode(document)
					try zCollection.insertOne(zDocument)
				}
				catch {
					if let errorCode = (error as? MongoSwift.MongoError.WriteError)?.writeFailure?.code, errorCode == 11000 {
					}
					else {
						print("This error occurred when trying to insertOne: ", error)
						throw error
					}
				}
			}
		}
		else {
			print("This error occurred when trying to insertMany: ", error)
			throw error
		}
	}
}

I’m getting a warning on the two if let errorCode ... errorCode == 11000 lines:
“Cast from ‘any Error’ to unrelated type ‘MongoError.BulkWriteError’ always fails”

I’ve read the documentation (here) that says these two functions throws four types of errors each (including the two I’m using: MongoError.BulkWriteError, MongoError.WriteError), but it seems I’m not casting it correctly, or something…?

I’ve tried a few things, including some suggestions from ChatGPT, but neither it nor I can figure it out, so far.

Can anyone here help please?

Thanks in advance!

To be honest, I didn’t even think ChatGPT would even know MongoSH, let alone the drivers.

Those are very specific technologies that really aren’t as broad/common.

But I think the problem is your errors, what is supposed to be first? and code? You have it in both your BulkWriteError, and your WriteError, what are you trying to achieve with this?

What are you trying to define exactly?

@David_Thorp

The way that should be executed should be a

do {
    let result = try collection.bulkWrite(writes, options: options)
    print("Bulk write result: \(result)")
} catch let error {
    print("Error during bulk write: \(error)")
}

Because you want to make sure you’re defining your options properly, and in what session.

Because you’re supposed to define what exactly it’s supposed to do based on what has happened, so then you’d go in line for the insertOne or insertMany issue.

You also should be invoking write options for bulk writes. You can do this by putting in: let options = BulkWriteOptions() before you put in the bulkwriteerror.

@David_Thorp Does this make sense?

Oh, and if you want to include sessions you can do:

do {
    let result = try collection.bulkWrite(writes, options: options, session: session)
    print("Bulk write result: \(BulkWriteResult)")
} catch let error {
    print("Error during bulk write: \(error)")
}

@David_Thorp I’m going to play with this for a few, I need a break from my project anyway, this is bugging me now…

@David_Thorp Yeah, you have to throw it in a Do Catch Block, otherwise it shouldn’t work, Xcode has validated syntax and it deploys.

do {
    let result = try zCollection.bulkWrite(requests)
    // BulkWrit result goes here
} catch let error as MongoError.BulkWriteError {
    // This handles the error
    print("Bulk write error: \(error)")
} catch {
    // This is for other errors.
    print("Error: \(error)")
}

This is how Do…Catch… works.

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/errorhandling/