Typo
[AOOS.git] / AOOSException.php
blob9e4c3561d6bb4e3c72d80a4aab294fb61a294347
1 <?php
3 AOOSCore::register("AOOSTypeException", "AOOSException.php");
5 /**
6 * AOOSException
7 * @author Sebastian Skejø
8 */
10 class AOOSException extends Exception
12 private $_core = null;
13 private $_name = null;
14 private $_msg = null;
15 private $_trace = null;
16 private $_ecode = null;
18 /**
19 * When an AOOSException is constructed it is automatically set in the given $core
20 * @param $core An AOOSCore-object
21 * @param $name The name of the exception to throw
22 * @param $msg A more detailed description of the exception
23 * @param $doTrace If this is true the expection will have a backtrace included in it's output
24 * @param $code Error code. 0 = fatal error, 1 = error, 2 = warning.
26 public function __construct($core, $name, $msg="", $doTrace=true, $code = 1)
28 $this->_core = $core;
30 $this->_name = $name;
31 $this->_msg = $msg;
32 if ($doTrace)
34 $this->_trace = debug_backtrace();
36 if ($code > 2 || $code < 0)
38 throw new AOOSException($core, $core->tr("exception_code_error"));
39 return false;
41 $this->_ecode = $code;
42 $this->core()->setException($this);
45 /**
46 * Makes sure that it is possible to call
47 * \code
48 * print new AOOSException($core, $name);
49 * \endcode
51 public function __toString()
53 $str = "<strong>";
54 switch($this->_ecode)
56 case(0):
57 $str .= "fatal_error";
58 break;
59 case(1):
60 $str .= "error";
61 break;
62 case(2):
63 $str .= "warning";
64 break;
66 $str .= "</strong>: ".$this->_name."<br /><div style='font-size: 10pt;'>".$this->_msg."</div><br /><div style='font-size: 10pt; color: #aaa'>";
67 foreach ($this->_trace as $row)
69 if (isset($row["class"]))
71 if ($row["class"] == "AOOSException")
73 continue;
75 $file = in_array("file", array_keys($row)) ? $row["file"] : "unknown file";
76 $line = in_array("line", array_keys($row)) ? $row["line"] : "unknown line";
77 $str .= "<strong>".$row["class"]."->".$row["function"]."</strong> in ".$file." on line ".$line."<br />";
80 $str .= "</div>";
81 return $str;
84 /**
85 * Returns the name of the exception
87 public function getName() { return $this->_name; }
88 /**
89 * Returns the description of the exception
91 public function getMsg() { return $this->_msg; }
92 /**
93 * Returns the error code of the exception
95 public function getEcode() { return $this->_ecode; }
96 /**
97 * Returns the core object
99 public function core() { return $this->_core; }
103 * A convienience exception class for use when exception should descripe a type error
104 * \internal Use of error in documentation is not good?
106 class AOOSTypeException extends AOOSException {
108 * Takes an AOOSCore, $core, an a $type
109 * @param $core An AOOSCore
110 * @param $type Name of the type which caused problems
112 public function __construct($core, $type, $msg) {
113 parent::__construct($core, "Type Exception", $type.$msg);
117 class AOOSLangException extends AOOSException {
118 public function __construct($core, $str) {
119 parent::__construct($core, "Couldn't find string", "String name: ".$str, true, 2);
122 // vim: number