From 5cb94cfaac9179157f7f5c6113cfd3fbcd1ee573 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Sun, 21 Jul 2013 02:39:26 +0000
Subject: [PATCH] Add error class for automatic error logging

---
 include/class.error.php | 51 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 include/class.error.php

diff --git a/include/class.error.php b/include/class.error.php
new file mode 100644
index 000000000..bdc4d7a57
--- /dev/null
+++ b/include/class.error.php
@@ -0,0 +1,51 @@
+<?php
+/*********************************************************************
+    class.error.php
+
+    Error handling for PHP < 5.0. Allows for returning a formal error from a
+    function since throwing it isn't available. Also allows for consistent
+    logging and debugging of errors in the osTicket system log.
+
+    Peter Rotich <peter@osticket.com>
+    Jared Hancock <jared@osticket.com>
+    Copyright (c)  2006-2013 osTicket
+    http://www.osticket.com
+
+    Released under the GNU General Public License WITHOUT ANY WARRANTY.
+    See LICENSE.TXT for details.
+
+    vim: expandtab sw=4 ts=4 sts=4:
+**********************************************************************/
+
+class Error /* extends Exception */ {
+    var $title = '';
+
+    function Error($message) {
+        call_user_func_array(array($this,'__construct'), func_get_args());
+    }
+    function __construct($message) {
+        global $ost;
+
+        $message = str_replace(ROOT_DIR, '(root)/', $message);
+
+        if ($ost->getConfig()->getLogLevel() == 3)
+            $message .= "\n\n" . $this->formatBacktrace(debug_backtrace());
+
+        $ost->logError($this->getTitle(), $message);
+    }
+
+    function getTitle() {
+        return get_class($this) . ": {$this->title}";
+    }
+
+    function formatBacktrace($bt) {
+        $buffer = array();
+        foreach ($bt as $i=>$frame)
+            $buffer[] = sprintf("#%d %s%s%s at [%s:%d]", $i,
+                $frame['class'], $frame['type'], $frame['function'],
+                str_replace(ROOT_DIR, '', $frame['file']), $frame['line']);
+        return implode("\n", $buffer);
+    }
+}
+
+?>
-- 
GitLab