-
Notifications
You must be signed in to change notification settings - Fork 0
/
exception.php
119 lines (109 loc) · 3.97 KB
/
exception.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* @file arkascha/urithmetic/exception.php
* @copyright 2013-2014 Christian Reiner, Hamburg, Germany, mailto:[email protected]
* @license GNU Affero General Public license version 3 (AGPL)
* @author Christian Reiner <[email protected]>
* @brief Part of package 'PHP Urithmetic', an implementation of unit based arithmetic
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the license, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace arkascha\urithmetic;
/**
* @class Exception
* @brief Package specific exception class
* @author Christian Reiner <[email protected]>
* @description
* This is a library specific exception class.
* Exceptions are PHP exceptions (inheritance) extended by an additional 'data' attribute.
* This attribute stores some additional runtime information about the occurance.
* It is an array structure holding technical information.
*/
class Exception extends \Exception
{
const TITLE = 'Urithmetic Exception';
const UNDEFINED = 1;
const UNSUITED = 2;
const UNCOMPATIBLE = 3;
/**
* @member data
* @type array
* @brief Associative array holding arbitrary additional technical information about the incident.
*/
protected $data;
/**
* @method morphPHPException
* @brief Morphs a given PHPexception into a library specific exception. Typically used inside catch blocks.
* @param $e exception
* @return PDException
* @access public
*/
static public function morphPHPException ($e)
{
if ( ! is_a('Exception', $e))
return new Exception('Thrown unsupported object', array('class' =>get_class($e),
'object'=>$e));
return new Exception('Technical PHP exception', array('class' =>get_class($e),
'errno'=>$e->getCode(),
'error'=>$e->getMessage()));
} // function morphPHPException
/**
* @method __construct
* @brief Constructor, instiantiates a valid php exception object
* @param $message string Human readable error message
* @param $data array Optional technical data that might help to debug problems
* @access public
*/
public function __construct ($code, $class, $method, $message, $data=array())
{
parent::__construct(sprintf('%s::%s: %s', $class, $method, $message), $code);
$this->data = is_array($data) ? $data : array($data);
// convert more complex elements to strings
foreach ($this->data as &$data)
if (is_array($data))
$data = print_r($data,true);
} // function __construct
/**
* @method getData
* @brief Return data structure generated at runtime
* @access public
*/
public function getData()
{
return $this->data;
} // function getData
/**
* @method __toString
* @brief Returns exception as human readable error string
* @access public
*/
public function __toString()
{
return sprintf('%1$s: [%2$s] %3$s', static::TITLE, $this->code, $this->message);
} // function __toString
/**
* @method __toHeader
* @brief returns exception as http headers
* @access public
*/
public function __toHeader()
{
header('HTTP/1.1 500 Internal Server Error');
header('Content-type: text/plain');
header(sprintf('%1$s: [%2$s] %3$s', static::TITLE, urlencode($this->code, $this->message)));
} // function toHeader
} // class Exception
?>