Skip to content

Add constants for error codes #1665

@tyilo

Description

@tyilo

I only really have a use for const DUPLICATE_KEY_ERROR: i32 = 11_000;, but I guess the others should be added as well.


It might also make sense to add a is_duplicate_key_error(&self) -> bool to WriteError, IndexedWriteError and CommandError as that would simplify this code that I'm using:

#[must_use]
pub fn is_duplicate_key_error(e: &mongodb::error::Error) -> bool {
    const DUPLICATE_KEY_ERROR: i32 = 11_000;

    match &*e.kind {
        ErrorKind::Write(mongodb::error::WriteFailure::WriteError(e)) => {
            e.code == DUPLICATE_KEY_ERROR
        }
        ErrorKind::Command(command_error) => command_error.code == DUPLICATE_KEY_ERROR,
        ErrorKind::InsertMany(insert_many_error) => insert_many_error
            .write_errors
            .iter()
            .flatten()
            .any(|e| e.code == DUPLICATE_KEY_ERROR),
        ErrorKind::BulkWrite(bulk_write_error) => bulk_write_error
            .write_errors
            .values()
            .any(|e| e.code == DUPLICATE_KEY_ERROR),
        _ => false,
    }
}

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions