/** * Look for 'Duplicate entry' exceptions, and convert to a human-friendly message. */ protected function _handleDatabaseException(Zend_Db_Statement_Exception $e) { if (strpos($e->getMessage(), 'Duplicate entry') === false) { throw $e; } // Note the double spaces in the template string are required since quotes would be // added greedily to the parsed values. list($value, $index) = sscanf($e->getMessage(), 'SQLSTATE[23000]: Integrity constraint violation: ' . '1062 Duplicate entry %s for key %s '); // Throw an exception with a human-friendly error throw new Exception(sprintf(__('%s is already in use, please provide a unique value.'), $value)); }
/** * Constructor * * If $message is an array, the assumption is that the return value of * sqlsrv_errors() was provided. If so, it then retrieves the most recent * error from that stack, and sets the message and code based on it. * * @param null|array|string $message * @param null|int $code */ public function __construct($message = null, $code = 0) { if (is_array($message)) { // Error should be array of errors // We only need first one (?) if (isset($message[0])) { $message = $message[0]; } $code = (int) $message['code']; $message = (string) $message['message']; } parent::__construct($message, $code); }