← Back to team overview

anewt-developers team mailing list archive

[Branch ~uws/anewt/anewt.uws] Rev 1703: [core] Support $code parameter in AnewtException

 

------------------------------------------------------------
revno: 1703
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt.uws
timestamp: Tue 2009-07-21 20:57:43 +0200
message:
  [core] Support $code parameter in AnewtException
  
  Add some argument handling magic to also support an error
  $code number as the first argument, while still allowing a
  sprintf-like format string and a variable number of values.
  Added some examples for this to the API docs.
modified:
  core/exception.lib.php

=== modified file 'core/exception.lib.php'
--- core/exception.lib.php	2009-03-26 19:17:14 +0000
+++ core/exception.lib.php	2009-07-21 18:57:43 +0000
@@ -16,17 +16,42 @@
 	/**
 	 * Create a new AnewtException.
 	 *
+	 * The \c $code parameter is optional (even though its the first parameter).
+	 * If the first parameter is an integer, it is used as the \c Exception
+	 * code. Otherwise, it is assumed to be a (format) string. The remaining
+	 * arguments are treated just like the \c sprintf() does.
+	 *
+	 * Examples:
+	 *
+	 * - <code>throw new AnewtException('Error');</code>
+	 * - <code>throw new AnewtException('Error: %s', $value);</code>
+	 * - <code>throw new AnewtException(123, 'Error: %s (%s)', $value1, $value2);</code>
+	 *
+	 * \param $code
+	 *   Optional error code.
 	 * \param $fmt
 	 *   A error message, optionally with sprintf format specifiers
 	 * \param $args
 	 *   Zero or more values passed to vsprintf
 	 */
-	function __construct($fmt, $args=null)
+	function __construct($code, $fmt=null, $args=null)
 	{
 		$args = func_get_args();
+
+		/* Use first argument as code only if it is an integer */
+		$code = null;
+		if (is_int($args[0]))
+			$code = array_shift($args);
+
+		/* Treat remaining arguments like sprintf() */
 		$fmt = array_shift($args);
 		assert('is_string($fmt);');
-		parent::__construct(vsprintf($fmt, $args));
+		$message = vsprintf($fmt, $args);
+
+		if (is_null($code))
+			parent::__construct($message);
+		else
+			parent::__construct($message, $code);
 	}
 }
 



--
lp:anewt
https://code.launchpad.net/~uws/anewt/anewt.uws

Your team Anewt developers is subscribed to branch lp:anewt.
To unsubscribe from this branch go to https://code.launchpad.net/~uws/anewt/anewt.uws/+edit-subscription.