Skip to content

Commit 39435e2

Browse files
committed
fix: properly handle duplicate key errors
1 parent 6f9ca9b commit 39435e2

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

src/storage.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,13 @@ impl<'a> Store<'a> {
2929
const DUPLICATE_KEY_ERROR_CODE: i32 = 11000;
3030
match collection.insert_many(batch).ordered(false).run() {
3131
Ok(_) => Ok(()),
32-
Err(e)
33-
if matches!(
34-
e.kind.as_ref(),
35-
mongodb::error::ErrorKind::BulkWrite(f)
36-
if f.write_concern_errors.is_empty()
37-
&& f.write_errors.values().all(|b| b.code == DUPLICATE_KEY_ERROR_CODE)
38-
) =>
39-
{
40-
Ok(())
41-
}
42-
Err(e) => Err(anyhow!(e)),
32+
Err(e) => match e.kind.as_ref() {
33+
mongodb::error::ErrorKind::InsertMany(mongodb::error::InsertManyError {
34+
write_errors: Some(errors),
35+
..
36+
}) if errors.iter().all(|b| b.code == DUPLICATE_KEY_ERROR_CODE) => Ok(()),
37+
_ => Err(anyhow!(e)),
38+
},
4339
}
4440
}
4541

0 commit comments

Comments
 (0)