Installer: minor fixes.
[AOOS.git] / AOOSException.php
blob0b3a5d9512f7cffeb4cb277004a199c9d944edbe
1 <?php
3 /**
4 * AOOSException
5 * @author Sebastian Skejø
6 */
8 class AOOSException extends Exception
10 private $_core = null;
11 private $_name = null;
12 private $_msg = null;
13 private $_trace = null;
14 private $_ecode = null;
16 /**
17 * When an AOOSException is constructed it is automatically set in the given $core
18 * @param $core An AOOSCore-object
19 * @param $name The name of the exception to throw
20 * @param $msg A more detailed description of the exception
21 * @param $doTrace If this is true the expection will have a backtrace included in it's output
22 * @param $code Error code. 0 = fatal error, 1 = error, 2 = warning.
24 public function __construct($core, $name, $msg="", $doTrace=true, $code = 1)
26 $this->_core = $core;
28 $this->_name = $name;
29 $this->_msg = $msg;
30 if ($doTrace)
32 $this->_trace = debug_backtrace();
34 if ($code > 2 || $code < 0)
36 throw new AOOSException($core, $core->tr("exception_code_error"));
37 return false;
39 $this->_ecode = $code;
40 $this->core()->setException($this);
43 /**
44 * Makes sure that it is possible to call
45 * \code
46 * print new AOOSException($core, $name);
47 * \endcode
49 public function __toString()
51 $str = "<strong>";
52 switch($this->_ecode)
54 case(0):
55 $str .= "fatal_error";
56 break;
57 case(1):
58 $str .= "error";
59 break;
60 case(2):
61 $str .= "warning";
62 break;
64 $str .= "</strong>: ".$this->_name."<br /><div style='font-size: 10pt;'>".$this->_msg."</div><br /><div style='font-size: 10pt; color: #aaa'>";
65 foreach ($this->_trace as $row)
67 if (isset($row["class"]))
69 if ($row["class"] == "AOOSException")
71 continue;
73 $file = in_array("file", array_keys($row)) ? $row["file"] : "unknown file";
74 $line = in_array("line", array_keys($row)) ? $row["line"] : "unknown line";
75 $str .= "<strong>".$row["class"]."->".$row["function"]."</strong> in ".$file." on line ".$line."<br />";
78 $str .= "</div>";
79 return $str;
82 /**
83 * Returns the name of the exception
85 public function getName() { return $this->_name; }
86 /**
87 * Returns the description of the exception
89 public function getMsg() { return $this->_msg; }
90 /**
91 * Returns the error code of the exception
93 public function getEcode() { return $this->_ecode; }
94 /**
95 * Returns the core object
97 public function core() { return $this->_core; }
101 * A convienience exception class for use when exception should descripe a type error
102 * \internal Use of error in documentation is not good?
104 class AOOSTypeException extends AOOSException {
106 * Takes an AOOSCore, $core, an a $type
107 * @param $core An AOOSCore
108 * @param $type Name of the type which caused problems
110 public function __construct($core, $type, $msg) {
111 parent::__construct($core, "Type Exception", $type.$msg);
115 class AOOSLangException extends AOOSException {
116 public function __construct($core, $str) {
117 parent::__construct($core, "Couldn't find string", "String name: ".$str, true, 2);
120 // vim: number