Question

I'm using \lithium\data\source\MongoDB::update() to do an upsert in safe mode. On some updates, there is an expected MongoCursorException due to a duplicate key being passed. The try/catch below does not catch the error, and it bubbles back up to an ErrorHandler I have attached to Dispatcher::run().

try {
    $result = Items::update($record, $conditions, array('upsert' => true, 'safe'   => true));
} catch (MongoCursorException $e) {
    $result = false;
} catch (Exception $e) {
    $result = false;
}
Was it helpful?

Solution

You need to add use statements to the top of your file, or put a backslash in front of the exception class names to indicate they are from the global scope and not your namespace. I prefer the use statements at the top of the class.

use MongoCursorException;
use Exception;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top