Permalink
Browse files

model exceptions

  • Loading branch information...
1 parent b563793 commit e66ef9f180782bb8ea3b688b5908d87b320d1272 @kolinger committed Dec 21, 2012
Showing with 173 additions and 8 deletions.
  1. +49 −0 Model/DuplicateEntryException.php
  2. +49 −0 Model/EmptyValueException.php
  3. +12 −8 Model/Facade.php
  4. +63 −0 Model/Helper.php
@@ -0,0 +1,49 @@
+<?php
+
+/**
+ * This file is part of the Square CMS
+ *
+ * Copyright (c) 2011, 2012 Tomáš Kolinger <tomas@kolinger.name>
+ *
+ * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
+ */
+
+namespace Square\Model;
+
+
+
+/**
+ * @author Tomáš Kolinger <tomas@kolinger.name>
+ */
+class DuplicateEntryException extends \Exception
+{
+
+ /**
+ * @var string
+ */
+ private $key;
+
+
+
+ /**
+ * @param string $message
+ * @param string|NULL $key
+ * @param \Exception $parent
+ */
+ public function __construct($message, $key = NULL, \Exception $parent = NULL)
+ {
+ parent::__construct($message, 0, $parent);
+ $this->key = $key;
+ }
+
+
+
+ /**
+ * @return string
+ */
+ public function getKey()
+ {
+ return $this->key;
+ }
+
+}
@@ -0,0 +1,49 @@
+<?php
+
+/**
+ * This file is part of the Square CMS
+ *
+ * Copyright (c) 2011, 2012 Tomáš Kolinger <tomas@kolinger.name>
+ *
+ * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
+ */
+
+namespace Square\Model;
+
+
+
+/**
+ * @author Tomáš Kolinger <tomas@kolinger.name>
+ */
+class EmptyValueException extends \Exception
+{
+
+ /**
+ * @var string
+ */
+ private $column;
+
+
+
+ /**
+ * @param string $message
+ * @param string|NULL $column
+ * @param \Exception $parent
+ */
+ public function __construct($message, $column = NULL, \Exception $parent = NULL)
+ {
+ parent::__construct($message, 0, $parent);
+ $this->column = $column;
+ }
+
+
+
+ /**
+ * @return string
+ */
+ public function getColumn()
+ {
+ return $this->column;
+ }
+
+}
View
@@ -86,14 +86,16 @@ public function create()
*/
public function save($entity, $flush = self::FLUSH)
{
-// try {
+ try {
$this->entityManager->persist($entity);
if ($flush == self::FLUSH) {
$this->entityManager->flush();
}
-// } catch () {
-//
-// }
+ } catch(\Doctrine\DBAL\DBALException $exception) {
+ Helper::convertException($exception);
+ } catch (\PDOException $exception) {
+ Helper::convertException($exception);
+ }
}
@@ -104,14 +106,16 @@ public function save($entity, $flush = self::FLUSH)
*/
public function remove($entity, $flush = self::FLUSH)
{
-// try {
+ try {
$this->entityManager->remove($entity);
if ($flush == self::FLUSH) {
$this->entityManager->flush();
}
-// } catch () {
-//
-// }
+ } catch(\Doctrine\DBAL\DBALException $exception) {
+ Helper::convertException($exception);
+ } catch (\PDOException $exception) {
+ Helper::convertException($exception);
+ }
}
View
@@ -0,0 +1,63 @@
+<?php
+
+/**
+ * This file is part of the Square CMS
+ *
+ * Copyright (c) 2011, 2012 Tomáš Kolinger <tomas@kolinger.name>
+ *
+ * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
+ */
+
+namespace Square\Model;
+
+
+
+/**
+ * @author Tomáš Kolinger <tomas@kolinger.name>
+ */
+class Helper extends \Nette\Object
+{
+
+ public function __construct()
+ {
+ throw new \Nette\StaticClassException;
+ }
+
+
+
+ /**
+ * @param \Exception $e
+ * @return \Doctrine\DBAL\DBALException|\Exception|\PDOException
+ * @throws EmptyValueException
+ * @throws DuplicateEntryException
+ * @throws \Nette\InvalidArgumentException
+ */
+ public static function convertException(\Exception $e)
+ {
+ if ($e instanceof \Doctrine\DBAL\DBALException) {
+ $pe = $e->getPrevious();
+ if ($pe instanceof \PDOException) {
+ $info = $pe->errorInfo;
+ } else {
+ throw new \Nette\InvalidArgumentException('Not supported DBAL exception type', 0, $e);
+ }
+ } elseif ($e instanceof \PDOException) {
+ $info = $e->errorInfo;
+ } else {
+ return $e;
+ }
+
+ if ($info[0] == 23000 && $info[1] == 1062) { // unique fail
+ $key = preg_match('#for key \'([a-z0-9\-_]*)\'#i', $e->getMessage(), $matches) ? $matches[1] : NULL;
+ throw new \Square\Model\DuplicateEntryException($e->getMessage(), $key, $e);
+ } elseif ($info[0] == 23000 && $info[1] == 1048) { // notnull fail
+ // @todo convert table column name to entity column name
+ $name = substr($info[2], strpos($info[2], "'") + 1);
+ $name = substr($name, 0, strpos($name, "'"));
+ throw new \Square\Model\EmptyValueException($e->getMessage(), $name, $e);
+ } else {
+ return $e;
+ }
+ }
+
+}

0 comments on commit e66ef9f

Please sign in to comment.