# MongoDB::Error - perldoc - [phpMan]

## NAME
    [MongoDB::Error](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AError/markdown) - MongoDB Driver Error classes

## VERSION
    version v2.2.2

## SYNOPSIS
        use [MongoDB::Error](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AError/markdown);
        [MongoDB::Error](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AError/markdown)->throw("a generic error");
        [MongoDB::DatabaseError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ADatabaseError/markdown)->throw(
            message => $string,
            result => $hashref,
        );

## DESCRIPTION
    This class defines a hierarchy of exception objects.

## USAGE
    Unless otherwise explicitly documented, all driver methods throw exceptions if an error occurs.

    To catch and handle errors, the [Try::Tiny](https://www.chedong.com/phpMan.php/perldoc/Try%3A%3ATiny/markdown) and [Safe::Isa](https://www.chedong.com/phpMan.php/perldoc/Safe%3A%3AIsa/markdown) modules are recommended:

        use [Try::Tiny](https://www.chedong.com/phpMan.php/perldoc/Try%3A%3ATiny/markdown);
        use [Safe::Isa](https://www.chedong.com/phpMan.php/perldoc/Safe%3A%3AIsa/markdown); # provides $_isa

        try {
            $coll->insert( $doc )
        }
        catch {
            if ( $_->$_isa("[MongoDB::DuplicateKeyError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ADuplicateKeyError/markdown)" ) ) {
                ...
            }
            else {
                ...
            }
        };

    To retry failures automatically, consider using [Try::Tiny::Retry](https://www.chedong.com/phpMan.php/perldoc/Try%3A%3ATiny%3A%3ARetry/markdown).

## EXCEPTION HIERARCHY
        [MongoDB::Error](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AError/markdown)
            |
            |->[MongoDB::AuthError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AAuthError/markdown)
            |
            |->[MongoDB::ConnectionError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AConnectionError/markdown)
            |   |
            |   |->[MongoDB::HandshakeError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AHandshakeError/markdown)
            |   |
            |   |->[MongoDB::NetworkError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ANetworkError/markdown)
            |
            |->[MongoDB::ConfigurationError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AConfigurationError/markdown)
            |
            |->[MongoDB::DatabaseError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ADatabaseError/markdown)
            |   |
            |   |->[MongoDB::CursorNotFoundError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ACursorNotFoundError/markdown)
            |   |
            |   |->[MongoDB::DuplicateKeyError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ADuplicateKeyError/markdown)
            |   |
            |   |->[MongoDB::NotMasterError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ANotMasterError/markdown)
            |   |
            |   |->[MongoDB::WriteError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AWriteError/markdown)
            |   |
            |   |->[MongoDB::WriteConcernError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AWriteConcernError/markdown)
            |
            |->[MongoDB::DecodingError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ADecodingError/markdown)
            |
            |->[MongoDB::DocumentError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ADocumentError/markdown)
            |
            |->[MongoDB::GridFSError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AGridFSError/markdown)
            |
            |->[MongoDB::InternalError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AInternalError/markdown)
            |
            |->[MongoDB::InvalidOperationError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AInvalidOperationError/markdown)
            |
            |->[MongoDB::ProtocolError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AProtocolError/markdown)
            |
            |->[MongoDB::SelectionError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ASelectionError/markdown)
            |
            |->[MongoDB::TimeoutError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ATimeoutError/markdown)
            |   |
            |   |->[MongoDB::ExecutionTimeout](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AExecutionTimeout/markdown)
            |   |
            |   |->[MongoDB::NetworkTimeout](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ANetworkTimeout/markdown)
            |
            |->[MongoDB::UsageError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AUsageError/markdown)

    All classes inherit from "[MongoDB::Error](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AError/markdown)".

    All error classes have the attribute:

    *   message — a text representation of the error

### [MongoDB::AuthError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AAuthError/markdown)
    This error indicates a problem with authentication, either in the underlying mechanism or a
    problem authenticating with the server.

### [MongoDB::ConnectionError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AConnectionError/markdown)
    Errors related to network connections.

   [MongoDB::HandshakeError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AHandshakeError/markdown)
    This error is thrown when a connection has been made, but SSL or authentication handshakes fail.

   [MongoDB::NetworkError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ANetworkError/markdown)
    This error is thrown when a socket error occurs, when the wrong number of bytes are read, or
    other wire-related errors occur.

### [MongoDB::ConfigurationError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AConfigurationError/markdown)
    This error is thrown when there is a configuration error between the MongoDB deployment and the
    configuration of the client, such as when trying to use explicit sessions on a MongoDB < 3.6

### [MongoDB::CursorNotFoundError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ACursorNotFoundError/markdown)
    This error indicates that a cursor timed out on a server.

### [MongoDB::DatabaseError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ADatabaseError/markdown)
    Errors related to database operations. Specifically, when an error of this type occurs, the
    driver has received an error condition from the server.

    Attributes include:

    *   result — response from a database command; this must implement the "last_errmsg" method

    *   code — numeric error code; see "ERROR CODES"; if no code was provided by the database, the
        "UNKNOWN_ERROR" code will be substituted instead

   [MongoDB::DuplicateKeyError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ADuplicateKeyError/markdown)
    This error indicates that a write attempted to create a document with a duplicate key in a
    collection with a unique index. The "result" attribute is a result object.

   [MongoDB::NotMasterError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ANotMasterError/markdown)
    This error indicates that a write or other state-modifying operation was attempted on a server
    that was not a primary. The "result" attribute is a [MongoDB::CommandResult](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ACommandResult/markdown) object.

   [MongoDB::WriteError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AWriteError/markdown)
    Errors indicating failure of a write command. The "result" attribute is a result object.

   [MongoDB::WriteConcernError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AWriteConcernError/markdown)
    Errors indicating failure of a write concern. The "result" attribute is a result object.

### [MongoDB::DecodingError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ADecodingError/markdown)
    This error indicates a problem during BSON decoding; it wraps the error provided by the
    underlying BSON encoder. Note: Encoding errors will be thrown as a "[MongoDB::DocumentError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ADocumentError/markdown)".

### [MongoDB::DocumentError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ADocumentError/markdown)
    This error indicates a problem with a document to be inserted or replaced into the database, or
    used as an update document.

    Attributes include:

    *   document — the document that caused the error

### [MongoDB::GridFSError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AGridFSError/markdown)
    Errors related to GridFS operations, such a corrupted file.

### [MongoDB::InternalError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AInternalError/markdown)
    Errors that indicate problems in the driver itself, typically when something unexpected is
    detected. These should be reported as potential bugs.

### [MongoDB::ProtocolError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AProtocolError/markdown)
    Errors related to the MongoDB wire protocol, typically problems parsing a database response
    packet.

### [MongoDB::SelectionError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ASelectionError/markdown)
    When server selection fails for a given operation, this is thrown. For example, attempting a
    write when no primary is available or reading with a specific mode and tag set and no servers
    match.

### [MongoDB::TimeoutError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ATimeoutError/markdown)
    These errors indicate a user-specified timeout has been exceeded.

   [MongoDB::ExecutionTimeout](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AExecutionTimeout/markdown)
    This error is thrown when a query or command fails because "max_time_ms" has been reached. The
    "result" attribute is a [MongoDB::CommandResult](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ACommandResult/markdown) object.

   [MongoDB::NetworkTimeout](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ANetworkTimeout/markdown)
    This error is thrown when a network operation exceeds a timeout, typically "connect_timeout_ms"
    or "socket_timeout_ms".

### [MongoDB::UsageError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3AUsageError/markdown)
    Indicates invalid arguments or configuration options. Not all usage errors will throw this —
    only ones originating directly from the MongoDB::* library files. Some type and usage errors
    will originate from the [Type::Tiny](https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ATiny/markdown) library if the objects are used incorrectly.

    Also used to indicate usage errors for transaction commands.

## ERROR CODES
    The following error code constants are automatically exported by this module.

            BAD_VALUE                 => 2,
            UNKNOWN_ERROR             => 8,
            NAMESPACE_NOT_FOUND       => 26,
            EXCEEDED_TIME_LIMIT       => 50,
            COMMAND_NOT_FOUND         => 59,
            WRITE_CONCERN_ERROR       => 64,
            NOT_MASTER                => 10107,
            DUPLICATE_KEY             => 11000,
            DUPLICATE_KEY_UPDATE      => 11001, # legacy before 2.6
            DUPLICATE_KEY_CAPPED      => 12582, # legacy before 2.6
            UNRECOGNIZED_COMMAND      => 13390, # mongos error before 2.4
            NOT_MASTER_NO_SLAVE_OK    => 13435,
            NOT_MASTER_OR_SECONDARY   => 13436,
            CANT_OPEN_DB_IN_READ_LOCK => 15927,

    This is a very, very small subset of error codes possible from the server, but covers some of
    the more common ones seen by drivers.

    Note:

    *   Only "[MongoDB::DatabaseError](https://www.chedong.com/phpMan.php/perldoc/MongoDB%3A%3ADatabaseError/markdown)" objects have a "code" attribute.

    *   The database uses multiple write concern error codes. The driver maps them all to
        WRITE_CONCERN_ERROR for consistency and convenience.

## ERROR LABELS
    From MongoDB 4.0 onwards, errors may contain an error labels field. This field is populated for
    extra information from either the server or the driver, depending on the error.

    Known error labels include (but are not limited to):

    *   "TransientTransactionError" - added when network errors are encountered inside a
        transaction.

    *   "UnknownTransactionCommitResult" - added when a transaction commit may not have been able to
        satisfy the provided write concern.

## AUTHORS
    *   David Golden <<david@mongodb.com>>

    *   Rassi <<rassi@mongodb.com>>

    *   Mike Friedman <<friedo@friedo.com>>

    *   Kristina Chodorow <<k.chodorow@gmail.com>>

    *   Florian Ragwitz <<rafl@debian.org>>

## COPYRIGHT AND LICENSE
    This software is Copyright (c) 2020 by MongoDB, Inc.

    This is free software, licensed under:

      The Apache License, Version 2.0, January 2004

