From 0844c5d93bbbd9da9164ceee079bd5d3f61e7bcd Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Wed, 27 Feb 2008 02:52:39 +0000 Subject: [PATCH] MDL-90 weblib: Introducing mdie() - close sibling of Perl's die() So that commandline scripts can die with a message and a nonzero errorcode --- lib/weblib.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/weblib.php b/lib/weblib.php index 9631f95cc4c..33904173857 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -5734,6 +5734,23 @@ function print_error ($errorcode, $module='', $link='', $a=NULL) { get_string('moreinformation').'

'; 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.