cvs: peardoc /en/core/pear/pear raiseerror.xml seterrorhandling.xml
| From: | Greg Beaver | Date: | Tue, 26 Aug 2003 05:08:11 +0000 |
| Subject: | cvs: peardoc /en/core/pear/pear raiseerror.xml seterrorhandling.xml | ||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-1400@lists.php.net to get a copy of this message | ||
cellog Tue Aug 26 01:08:11 2003 EDT
Added files:
/peardoc/en/core/pear/pear raiseerror.xml
Modified files:
/peardoc/en/core/pear/pear seterrorhandling.xml
Log:
adding documentation for PEAR::raiseError(), and upgrading setErrorHandling() documentation from
its previously rather sparse state.
Index: peardoc/en/core/pear/pear/seterrorhandling.xml diff -u peardoc/en/core/pear/pear/seterrorhandling.xml:1.3 peardoc/en/core/pear/pear/seterrorhandling.xml:1.4 --- peardoc/en/core/pear/pear/seterrorhandling.xml:1.3 Sat Aug 23 10:25:17 2003 +++ peardoc/en/core/pear/pear/seterrorhandling.xml Tue Aug 26 01:08:11 2003 @@ -22,12 +22,14 @@ <refsect1 id="core.pear.pear.seterrorhandling.desc"> &title.desc; <para> - <function>setErrorHandling</function> can be invoked both as - objects and statically. If called - statically, <function>setErrorHandling</function> sets the default behaviour for all - PEAR objects. If called as an object member, - <function>setErrorHandling</function> sets - the default behaviour for that object. + <function>setErrorHandling</function> can be invoked as both a standard + object method ($obj->setErrorHandling) and as a static method + (PEAR::setErrorHandling). If called + statically, <function>PEAR::setErrorHandling</function> sets the default + error handling behaviour for all + PEAR objects (global error handling behaviour). If called as an object method, + <function>$obj->setErrorHandling</function> sets + the default error handling for only that object (local error handling behavior). </para> </refsect1> <refsect1 id="core.pear.pear.seterrorhandling.parameter"> @@ -41,8 +43,8 @@ <listitem> <para> <constant>PEAR_ERROR_RETURN</constant> If an error occurs, a - <classname>PEAR_Error</classname> is returned by the function, - where the error happened. + &class.pear-error; is returned from the error-generation + method (normally <link linkend="core.pear.pear.raiseerror"><function>raiseError</function></link>.) </para> </listitem> <listitem> @@ -56,28 +58,34 @@ <para> <constant>PEAR_ERROR_TRIGGER</constant> Like <constant> PEAR_ERROR_RETURN</constant>, but the PHP builtin-function - <function>trigger_error</function> will be executed additionally. + <function>trigger_error</function> will be called in PEAR_Error's + constructor with the error message. </para> </listitem> <listitem> <para> - <constant>PEAR_ERROR_DIE</constant> The script will be finish and - an error message will be printed, if an error occurs. + <constant>PEAR_ERROR_DIE</constant> The script will terminate and + an error message will be printed on instantiation of a PEAR_Error. </para> </listitem> <listitem> <para> - <constant>PEAR_ERROR_CALLBACK</constant> If a error occurs, the - indicated error handler function is called. This function has to - support a parameter to receive the &class.pear-error; object. + <constant>PEAR_ERROR_CALLBACK</constant> If a error occurs, the + callback passed to <parameter>$options</parameter> is called. </para> </listitem> + <listitem> + <para> + <constant>PEAR_ERROR_EXCEPTION</constant> If Zend Engine 2 is present, + then an exception will be thrown using the &class.pear-error; object. + </para> + </listitem> </itemizedlist> </para> </listitem> <listitem> <para> - <parameter>mixed $options</parameter> - the value for the options + <parameter>mixed $options</parameter> - the value for <parameter>$options</parameter> depends on <parameter>$mode</parameter> <itemizedlist> <listitem> @@ -85,29 +93,128 @@ <constant>PEAR_ERROR_PRINT</constant> and <constant>PEAR_ERROR_DIE</constant> support an optional <function>printf</function> format string used - when printing the error message. + when printing the error message. This format string should contain + a single %s, which will be used to insert the error message into the + string. Use the string to enclose the error message with other + useful information not handled by the error message prefix and + error message. </para> </listitem> <listitem> <para> - <constant>PEAR_ERROR_TRIGGER</constant> requires a user - level indication ( possible constants: <constant>E_USER_NOTICE</constant>, + <constant>PEAR_ERROR_TRIGGER</constant> requires an user error + level constant used by <function>trigger_error</function> (possible constants: <constant>E_USER_NOTICE</constant>, <constant>E_USER_WARNING</constant> or - <constant>E_USER_ERROR</constant>). + <constant>E_USER_ERROR</constant>). Note that if the error constant + is not one of these valid error constants, a PHP warning will be + triggered. </para> </listitem> <listitem> <para> - <constant>PEAR_ERROR_CALLBACK</constant> requires the - indication of the function to invoke as string. Or, - if the function a class member, an array containing - the class name and the function name. + <constant>PEAR_ERROR_CALLBACK</constant>The callback must be a function + name in the format described in the <ulink url="&url.php.callback;">Pseudo-Type</ulink> + section of the PHP manual (either a string, or an array). The callback must accept a single + parameter, the &class.pear-error; object generated by an error condition. + Note that if the callback is not a valid callback, a PHP warning will + be triggered. </para> </listitem> </itemizedlist> </para> </listitem> </itemizedlist> + </para> + <para> + Here's an example of a few ways to use setErrorHandling: + <programlisting role="php"> +<![CDATA[ +<?php +require_once 'PEAR.php'; +// dummy error constant for this example +define('MYCLASS_ERROR_CODE', 1); + +// demonstration of default global error handling +// in this case, all PEAR Errors will trigger a PHP warning +PEAR::setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_WARNING); +// Note that the file and line number will be in the constructor of PEAR_Error +// in PEAR.php +PEAR::raiseError('test warning', MYCLASS_ERROR_CODE); + +// this error specifies a mode, and overrides the default global error handling +$e = PEAR::raiseError('return only', MYCLASS_ERROR_CODE, PEAR_ERROR_RETURN); + +PEAR::setErrorHandling(PEAR_ERROR_PRINT, "Gronk error: %s<br />\n"); + +// prints "Gronk error: test warning<br />\n" +PEAR::raiseError('test warning', MYCLASS_ERROR_CODE); + +/** + * Fake class to demonstrate error handling + * @package myClass + */ +class myClass extends PEAR { + /** + * Demonstration of default local error handling + */ + function myClass() + { + // object method callback + $this->setErrorHandling(PEAR_ERROR_CALLBACK, array(&$this, 'handleErr')); + // prints "custom handler...is working" + PEAR::raiseError('custom handler', MYCLASS_ERROR_CODE); + // static class method callback + $this->setErrorHandling(PEAR_ERROR_CALLBACK, + array('myClass', 'handleErrStatic')); + PEAR::raiseError('custom handler', MYCLASS_ERROR_CODE); + // function callback + $this->setErrorHandling(PEAR_ERROR_CALLBACK, 'standardCallback'); + PEAR::raiseError('custom handler', MYCLASS_ERROR_CODE); + } + + /** + * Callback set by the constructor + * @param PEAR_Error The error object + */ + function handleErr($error) + { + $this->lastError = $error->getMessage(); + print $error->getMessage() . "...is working\n"; + } + + /** + * Static callback set by the constructor + * + * Note that in PHP 5, $this is not set if the method is declared with + * the "static" access modifier. In PHP 4, $this is set, but is not + * set to the myClass object, so don't use it! + * @param PEAR_Error The error object + */ + function handleErr($error) + { + print 'static ' . $error->getMessage() . "...is working\n"; + } +} + +/** + * @param PEAR_Error The error object + */ +function standardCallback($error) +{ + print 'normal function callback: ' . $error-getMessage(); +} +// This causes the printing of three messages through error callbacks: +// "custom handler...is working" +// "static custom handler... is working" +// "normal function callback: custom handler" +$mine = new myClass; + +PEAR::setErrorHandling(PEAR_ERROR_DIE); +// terminates the script with the error message "oops" +PEAR::raiseError('oops', MYCLASS_ERROR_CODE); +?> +]]> + </programlisting> </para> </refsect1> </refentry> Index: peardoc/en/core/pear/pear/raiseerror.xml +++ peardoc/en/core/pear/pear/raiseerror.xml <refentry id="core.pear.pear.raiseerror"> <refnamediv> <refname>PEAR::raiseError()</refname> <refpurpose>Create a new &class.pear-error; object and optionally specify error-handling instructions</refpurpose> </refnamediv> <refsynopsisdiv> <funcsynopsis> <funcsynopsisinfo> require_once 'PEAR.php'; </funcsynopsisinfo> <funcprototype> <funcdef>PEAR_Error <function>PEAR::raiseError</function></funcdef> <paramdef> string <parameter>$message</parameter> </paramdef> <paramdef> int <parameter><optional>$code</optional></parameter> </paramdef> <paramdef> int <parameter><optional>$mode</optional></parameter> </paramdef> <paramdef> int|array <parameter><optional>$options</optional></parameter> </paramdef> <paramdef> mixed <parameter><optional>$userinfo</optional></parameter> </paramdef> <paramdef> string <parameter><optional>$error_class</optional></parameter> </paramdef> <paramdef> boolean <parameter><optional>$skipmsg</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> </refsynopsisdiv> <refsect1 id="core.pear.pear.raiseerror.desc"> &title.desc; <para> <function>raiseError</function> </para> </refsect1> <refsect1 id="core.pear.pear.raiseerror.parameter"> &title.param; <para> <variablelist> <varlistentry> <term> &type.string; <parameter>$message</parameter> </term> <listitem> <para> Error message. The default message is "unknown error" if left blank. </para> </listitem> </varlistentry> <varlistentry> <term> &type.int; <parameter>$code</parameter> </term> <listitem> <para> Error code. It is recommended to use an error code for even the simplest errors, in order to simplify error handling and processing. </para> </listitem> </varlistentry> <varlistentry> <term> &type.int; <parameter>$mode</parameter> </term> <listitem> <para> Error mode. This is one of <constant>PEAR_ERROR_RETURN</constant>, <constant>PEAR_ERROR_PRINT</constant>, <constant>PEAR_ERROR_TRIGGER</constant>, <constant>PEAR_ERROR_DIE</constant>, <constant>PEAR_ERROR_CALLBACK</constant>, or <constant>PEAR_ERROR_EXCEPTION</constant>. See <link linkend="core.pear.pear.seterrorhandling"><function>setErrorHandling</function></link> for detailed information and examples of the meaning of these constants. </para> </listitem> </varlistentry> <varlistentry> <term> &type.mixed; <parameter>$options</parameter> </term> <listitem> <para> Error options. This depends on the value of <parameter>$mode</parameter>, and is documented in linkend="core.pear.pear.seterrorhandling"><function>setErrorHandling</function></link>. </para> </listitem> </varlistentry> <varlistentry> <term> &type.mixed; <parameter>$userinfo</parameter> </term> <listitem> <para> Optional user information. This can be used to store any error-specific information, and has an unspecified format. </para> </listitem> </varlistentry> <varlistentry> <term> &type.string; <parameter>$error_class</parameter> </term> <listitem> <para> Error class name to use as the error object. The default error class is &class.pear-error;. Use this parameter to specify another class to use, such as a custom class extending &class.pear-error; </para> </listitem> </varlistentry> <varlistentry> <term> &type.bool; <parameter>$skipmsg</parameter> </term> <listitem> <para> Use this parameter if you are using a custom class that does not accept an error message in its constructor. <emphasis>Never</emphasis> use this parameter without the <parameter>$error_class</parameter> parameter - it will not work. </para> </listitem> </varlistentry> </variablelist> </para> </refsect1> <refsect1 id="core.pear.pear.raiseerror.return"> &title.returns; <para> A &class.pear-error; object is returned, unless PEAR_ERROR_DIE terminates execution or a PEAR_ERROR_EXCEPTION is never handled. </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->
Index: peardoc/en/core/pear/pear/seterrorhandling.xml diff -u peardoc/en/core/pear/pear/seterrorhandling.xml:1.3 peardoc/en/core/pear/pear/seterrorhandling.xml:1.4 --- peardoc/en/core/pear/pear/seterrorhandling.xml:1.3 Sat Aug 23 10:25:17 2003 +++ peardoc/en/core/pear/pear/seterrorhandling.xml Tue Aug 26 01:08:11 2003 @@ -22,12 +22,14 @@ <refsect1 id="core.pear.pear.seterrorhandling.desc"> &title.desc; <para> - <function>setErrorHandling</function> can be invoked both as - objects and statically. If called - statically, <function>setErrorHandling</function> sets the default behaviour for all - PEAR objects. If called as an object member, - <function>setErrorHandling</function> sets - the default behaviour for that object. + <function>setErrorHandling</function> can be invoked as both a standard + object method ($obj->setErrorHandling) and as a static method + (PEAR::setErrorHandling). If called + statically, <function>PEAR::setErrorHandling</function> sets the default + error handling behaviour for all + PEAR objects (global error handling behaviour). If called as an object method, + <function>$obj->setErrorHandling</function> sets + the default error handling for only that object (local error handling behavior). </para> </refsect1> <refsect1 id="core.pear.pear.seterrorhandling.parameter"> @@ -41,8 +43,8 @@ <listitem> <para> <constant>PEAR_ERROR_RETURN</constant> If an error occurs, a - <classname>PEAR_Error</classname> is returned by the function, - where the error happened. + &class.pear-error; is returned from the error-generation + method (normally <link linkend="core.pear.pear.raiseerror"><function>raiseError</function></link>.) </para> </listitem> <listitem> @@ -56,28 +58,34 @@ <para> <constant>PEAR_ERROR_TRIGGER</constant> Like <constant> PEAR_ERROR_RETURN</constant>, but the PHP builtin-function - <function>trigger_error</function> will be executed additionally. + <function>trigger_error</function> will be called in PEAR_Error's + constructor with the error message. </para> </listitem> <listitem> <para> - <constant>PEAR_ERROR_DIE</constant> The script will be finish and - an error message will be printed, if an error occurs. + <constant>PEAR_ERROR_DIE</constant> The script will terminate and + an error message will be printed on instantiation of a PEAR_Error. </para> </listitem> <listitem> <para> - <constant>PEAR_ERROR_CALLBACK</constant> If a error occurs, the - indicated error handler function is called. This function has to - support a parameter to receive the &class.pear-error; object. + <constant>PEAR_ERROR_CALLBACK</constant> If a error occurs, the + callback passed to <parameter>$options</parameter> is called. </para> </listitem> + <listitem> + <para> + <constant>PEAR_ERROR_EXCEPTION</constant> If Zend Engine 2 is present, + then an exception will be thrown using the &class.pear-error; object. + </para> + </listitem> </itemizedlist> </para> </listitem> <listitem> <para> - <parameter>mixed $options</parameter> - the value for the options + <parameter>mixed $options</parameter> - the value for <parameter>$options</parameter> depends on <parameter>$mode</parameter> <itemizedlist> <listitem> @@ -85,29 +93,128 @@ <constant>PEAR_ERROR_PRINT</constant> and <constant>PEAR_ERROR_DIE</constant> support an optional <function>printf</function> format string used - when printing the error message. + when printing the error message. This format string should contain + a single %s, which will be used to insert the error message into the + string. Use the string to enclose the error message with other + useful information not handled by the error message prefix and + error message. </para> </listitem> <listitem> <para> - <constant>PEAR_ERROR_TRIGGER</constant> requires a user - level indication ( possible constants: <constant>E_USER_NOTICE</constant>, + <constant>PEAR_ERROR_TRIGGER</constant> requires an user error + level constant used by <function>trigger_error</function> (possible constants: <constant>E_USER_NOTICE</constant>, <constant>E_USER_WARNING</constant> or - <constant>E_USER_ERROR</constant>). + <constant>E_USER_ERROR</constant>). Note that if the error constant + is not one of these valid error constants, a PHP warning will be + triggered. </para> </listitem> <listitem> <para> - <constant>PEAR_ERROR_CALLBACK</constant> requires the - indication of the function to invoke as string. Or, - if the function a class member, an array containing - the class name and the function name. + <constant>PEAR_ERROR_CALLBACK</constant>The callback must be a function + name in the format described in the <ulink url="&url.php.callback;">Pseudo-Type</ulink> + section of the PHP manual (either a string, or an array). The callback must accept a single + parameter, the &class.pear-error; object generated by an error condition. + Note that if the callback is not a valid callback, a PHP warning will + be triggered. </para> </listitem> </itemizedlist> </para> </listitem> </itemizedlist> + </para> + <para> + Here's an example of a few ways to use setErrorHandling: + <programlisting role="php"> +<![CDATA[ +<?php +require_once 'PEAR.php'; +// dummy error constant for this example +define('MYCLASS_ERROR_CODE', 1); + +// demonstration of default global error handling +// in this case, all PEAR Errors will trigger a PHP warning +PEAR::setErrorHandling(PEAR_ERROR_TRIGGER, E_USER_WARNING); +// Note that the file and line number will be in the constructor of PEAR_Error +// in PEAR.php +PEAR::raiseError('test warning', MYCLASS_ERROR_CODE); + +// this error specifies a mode, and overrides the default global error handling +$e = PEAR::raiseError('return only', MYCLASS_ERROR_CODE, PEAR_ERROR_RETURN); + +PEAR::setErrorHandling(PEAR_ERROR_PRINT, "Gronk error: %s<br />\n"); + +// prints "Gronk error: test warning<br />\n" +PEAR::raiseError('test warning', MYCLASS_ERROR_CODE); + +/** + * Fake class to demonstrate error handling + * @package myClass + */ +class myClass extends PEAR { + /** + * Demonstration of default local error handling + */ + function myClass() + { + // object method callback + $this->setErrorHandling(PEAR_ERROR_CALLBACK, array(&$this, 'handleErr')); + // prints "custom handler...is working" + PEAR::raiseError('custom handler', MYCLASS_ERROR_CODE); + // static class method callback + $this->setErrorHandling(PEAR_ERROR_CALLBACK, + array('myClass', 'handleErrStatic')); + PEAR::raiseError('custom handler', MYCLASS_ERROR_CODE); + // function callback + $this->setErrorHandling(PEAR_ERROR_CALLBACK, 'standardCallback'); + PEAR::raiseError('custom handler', MYCLASS_ERROR_CODE); + } + + /** + * Callback set by the constructor + * @param PEAR_Error The error object + */ + function handleErr($error) + { + $this->lastError = $error->getMessage(); + print $error->getMessage() . "...is working\n"; + } + + /** + * Static callback set by the constructor + * + * Note that in PHP 5, $this is not set if the method is declared with + * the "static" access modifier. In PHP 4, $this is set, but is not + * set to the myClass object, so don't use it! + * @param PEAR_Error The error object + */ + function handleErr($error) + { + print 'static ' . $error->getMessage() . "...is working\n"; + } +} + +/** + * @param PEAR_Error The error object + */ +function standardCallback($error) +{ + print 'normal function callback: ' . $error-getMessage(); +} +// This causes the printing of three messages through error callbacks: +// "custom handler...is working" +// "static custom handler... is working" +// "normal function callback: custom handler" +$mine = new myClass; + +PEAR::setErrorHandling(PEAR_ERROR_DIE); +// terminates the script with the error message "oops" +PEAR::raiseError('oops', MYCLASS_ERROR_CODE); +?> +]]> + </programlisting> </para> </refsect1> </refentry> Index: peardoc/en/core/pear/pear/raiseerror.xml +++ peardoc/en/core/pear/pear/raiseerror.xml <refentry id="core.pear.pear.raiseerror"> <refnamediv> <refname>PEAR::raiseError()</refname> <refpurpose>Create a new &class.pear-error; object and optionally specify error-handling instructions</refpurpose> </refnamediv> <refsynopsisdiv> <funcsynopsis> <funcsynopsisinfo> require_once 'PEAR.php'; </funcsynopsisinfo> <funcprototype> <funcdef>PEAR_Error <function>PEAR::raiseError</function></funcdef> <paramdef> string <parameter>$message</parameter> </paramdef> <paramdef> int <parameter><optional>$code</optional></parameter> </paramdef> <paramdef> int <parameter><optional>$mode</optional></parameter> </paramdef> <paramdef> int|array <parameter><optional>$options</optional></parameter> </paramdef> <paramdef> mixed <parameter><optional>$userinfo</optional></parameter> </paramdef> <paramdef> string <parameter><optional>$error_class</optional></parameter> </paramdef> <paramdef> boolean <parameter><optional>$skipmsg</optional></parameter> </paramdef> </funcprototype> </funcsynopsis> </refsynopsisdiv> <refsect1 id="core.pear.pear.raiseerror.desc"> &title.desc; <para> <function>raiseError</function> </para> </refsect1> <refsect1 id="core.pear.pear.raiseerror.parameter"> &title.param; <para> <variablelist> <varlistentry> <term> &type.string; <parameter>$message</parameter> </term> <listitem> <para> Error message. The default message is "unknown error" if left blank. </para> </listitem> </varlistentry> <varlistentry> <term> &type.int; <parameter>$code</parameter> </term> <listitem> <para> Error code. It is recommended to use an error code for even the simplest errors, in order to simplify error handling and processing. </para> </listitem> </varlistentry> <varlistentry> <term> &type.int; <parameter>$mode</parameter> </term> <listitem> <para> Error mode. This is one of <constant>PEAR_ERROR_RETURN</constant>, <constant>PEAR_ERROR_PRINT</constant>, <constant>PEAR_ERROR_TRIGGER</constant>, <constant>PEAR_ERROR_DIE</constant>, <constant>PEAR_ERROR_CALLBACK</constant>, or <constant>PEAR_ERROR_EXCEPTION</constant>. See <link linkend="core.pear.pear.seterrorhandling"><function>setErrorHandling</function></link> for detailed information and examples of the meaning of these constants. </para> </listitem> </varlistentry> <varlistentry> <term> &type.mixed; <parameter>$options</parameter> </term> <listitem> <para> Error options. This depends on the value of <parameter>$mode</parameter>, and is documented in linkend="core.pear.pear.seterrorhandling"><function>setErrorHandling</function></link>. </para> </listitem> </varlistentry> <varlistentry> <term> &type.mixed; <parameter>$userinfo</parameter> </term> <listitem> <para> Optional user information. This can be used to store any error-specific information, and has an unspecified format. </para> </listitem> </varlistentry> <varlistentry> <term> &type.string; <parameter>$error_class</parameter> </term> <listitem> <para> Error class name to use as the error object. The default error class is &class.pear-error;. Use this parameter to specify another class to use, such as a custom class extending &class.pear-error; </para> </listitem> </varlistentry> <varlistentry> <term> &type.bool; <parameter>$skipmsg</parameter> </term> <listitem> <para> Use this parameter if you are using a custom class that does not accept an error message in its constructor. <emphasis>Never</emphasis> use this parameter without the <parameter>$error_class</parameter> parameter - it will not work. </para> </listitem> </varlistentry> </variablelist> </para> </refsect1> <refsect1 id="core.pear.pear.raiseerror.return"> &title.returns; <para> A &class.pear-error; object is returned, unless PEAR_ERROR_DIE terminates execution or a PEAR_ERROR_EXCEPTION is never handled. </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->