apteryx.indivia.net/include/OExceptions.php
2015-12-19 19:06:06 +01:00

79 lines
No EOL
2.5 KiB
PHP
Executable file

<?php
interface IException
{
/* Protected methods inherited from Exception class */
public function getMessage(); // Exception message
public function getCode(); // User-defined Exception code
public function getFile(); // Source filename
public function getLine(); // Source line
public function getTrace(); // An array of the backtrace()
public function getTraceAsString(); // Formated string of trace
/* Overrideable methods inherited from Exception class */
public function __toString(); // formated string for display
public function __construct($message = null, $code = 0);
}
abstract class CustomException extends Exception implements IException
{
private $trace; // Unknown
private $string; // Unknown
protected $message = 'Unknown exception'; // Exception message
protected $code = 0; // User-defined exception code
protected $file; // Source filename of exception
protected $line; // Source line of exception
public function __construct($message = null, $code = 0)
{
if (!$message) {
throw new $this('Unknown '. get_class($this));
}
parent::__construct("Err: ".$message."\n", $code);
}
public function __toString() {
$err=$this->message;
if($this->code!=0)
$err= get_class($this).
" '{$this->message}' in {$this->file}({$this->line})\n".
"{$this->getTraceAsString()}";
return $err;
}
}
class DbErrException extends CustomException{};
class DbResException extends DbErrException{};
class UsageException extends CustomException{
public function __construct($message = null, $code = 0)
{
if (!$message and $code == 0) {
throw new $this('Unknown '. get_class($this));
} elseif ($message != null and $code > 0) {
$argv="Err: ".$message."\n";
$message="Bad arg: ".$argv." is not a valid command, with ".$code." arguments\n";
$code=0;
}
parent::__construct($message, $code);
}
public function __toString() {
return $this->message;
}
}
class OtConfException extends CustomException{};
class FileException extends CustomException{
public function __toString() {
return $this->message;
}
};
?>