From 45bf5180284058beec0a91ae3df72ea60a585203 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 17 Oct 2007 16:50:36 -0400 Subject: [PATCH] When the API needs to throw an exception regarding a field, use a new FieldException which holds the name of the field as well as the error. --- Api.php | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/Api.php b/Api.php index da33e77..7590789 100644 --- a/Api.php +++ b/Api.php @@ -220,7 +220,7 @@ abstract class BSApi { if ($verify === false) { - $this->_error(new Exception(sprintf(_('Validation of "%1$s" failed'), $field))); + $this->_error(new FieldException(sprintf(_('Validation of "%1$s" failed'), $field))); } else { @@ -421,7 +421,7 @@ abstract class BSApi { if (!isset($this->values["$name"])) { - $this->_error(new Exception(sprintf(_('The required field "%1$s" was not set'), $name), $name)); + $this->_error(new FieldException(sprintf(_('The required field "%1$s" was not set'), $name), $name)); } } else if ($options[F_REQ] == REQ_SET) @@ -487,7 +487,7 @@ abstract class BSApi { if ($this->values["$field"] == 0) { - return new Exception(sprintf(_('The field "%1$s" cannot be zero'), $field), $field); + return new FieldException(sprintf(_('The field "%1$s" cannot be zero'), $field), $field); } return true; @@ -501,7 +501,7 @@ abstract class BSApi { if (empty($this->values["$field"])) { - return new Exception(sprintf(_('The field "%1$s" cannot be empty'), $field), $field); + return new FieldException(sprintf(_('The field "%1$s" cannot be empty'), $field), $field); } return true; @@ -516,7 +516,7 @@ abstract class BSApi $res = BSApp::Registry()->getType('Db')->queryFirst("SELECT $field FROM {$this->prefix}{$this->table} WHERE $field = " . $this->_prepareFieldForSql($field) . (empty($this->condition) ? "" : " AND !({$this->condition})")); if ($res) { - return new Exception(sprintf(_('The "%1$s" field must contain a unique value'), $field), $field); + return new FieldException(sprintf(_('The "%1$s" field must contain a unique value'), $field), $field); } return true; @@ -574,4 +574,45 @@ class ApiException extends Exception } } +/** + * Field Exception + * + * This exception represents a problem with an API field + * + * @author rsesek + * @copyright Copyright (c)2002 - 2007, Blue Static + * @version $Id$ + * @package ISSO + * + */ +class FieldException extends Exception +{ + /** + * The name of the erroring field + * @var string + */ + private $field; + + // ################################################################### + /** + * Constructor: create a new exception + */ + public function __construct($error, $field) + { + $this->field = $field; + parent::__construct($error); + } + + // ################################################################### + /** + * Returns the name of the field the exception is for + * + * @return string + */ + public function getField() + { + return $this->field; + } +} + ?> \ No newline at end of file -- 2.22.5