]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-90 weblib: Introducing mdie() - close sibling of Perl's die()
authormartinlanghoff <martinlanghoff>
Wed, 27 Feb 2008 02:52:28 +0000 (02:52 +0000)
committermartinlanghoff <martinlanghoff>
Wed, 27 Feb 2008 02:52:28 +0000 (02:52 +0000)
So that commandline scripts can die with a message and
a nonzero errorcode

lib/weblib.php

index e96ca62287a75ab9825a89555b812f2f06819c99..6b5318850682769e1c84240c0eb621138dc20c63 100644 (file)
@@ -5771,6 +5771,23 @@ function print_error ($errorcode, $module='', $link='', $a=NULL) {
                  get_string('moreinformation').'</a></p>';
     error($message, $link);
 }
+
+/**
+ * Print an error to STDOUT and exit with a non-zero code. For commandline scripts.
+ * Default errorcode is 1.
+ *
+ * Very useful for perl-like error-handling:
+ * 
+ * do_somethting() or mdie("Something went wrong");
+ *
+ * @param string  $msg       Error message
+ * @param integer $errorcode Error code to emit 
+ */
+function mdie($msg='', $errorcode=1) {
+    trigger_error($msg);
+    exit($errorcode);
+}
+
 /**
  * Returns a string of html with an image of a help icon linked to a help page on a number of help topics.
  * Should be used only with htmleditor or textarea.