-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathExceptionalClient.php
More file actions
185 lines (159 loc) · 4.78 KB
/
ExceptionalClient.php
File metadata and controls
185 lines (159 loc) · 4.78 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Exception handler and client for getexceptional.com
*
* @author Jan Lehnardt <jan@php.net>
**/
class ExceptionalClient
{
/**
* Installs the ExceptinoalClient class as the default exception handler
*
**/
function __construct($api_key)
{
$this->url = "/errors/?api_key={$api_key}&protocol_version=2";
$this->host = "getexceptional.com";
$this->port = 80;
// set exception handler & keep old exception handler around
$this->previous_exception_handler = set_exception_handler(array($this, "handle_exception"));
}
function handle_exception($exception)
{
$this->exceptions[] = new ExceptionData($exception);
if($this->previous_exception_handler) {
$this->previous_exception_handler();
}
}
function __destruct()
{
echo "sending exceptions:";
// send stack of exceptions to getexceptional
foreach($this->exceptions AS $exception) {
$this->send_exception($exception);
}
}
function send_exception($exception)
{
$body = $exception->toXML();
$this->post($this->url, $body);
}
function post($url, $post_data)
{
$s = fsockopen($this->host, $this->port, $errno, $errstr);
if(!$s) {
echo "$errno: $errstr\n";
return false;
}
$request = "POST $url HTTP/1.1\r\nHost: $this->host\r\n";
if($post_data) {
$request .= "Accept: */*\r\n";
$request .= "User-Agent: exception-php-client 0.1\r\n";
$request .= "Content-Type: text/xml\r\n";
$request .= "Connection: close\r\n";
$request .= "Content-Length: ".strlen($post_data)."\r\n\r\n";
$request .= "$post_data\r\n";
} else {
$request .= "\r\n";
}
var_dump($request);
flush();
fwrite($s, $request);
$response = "";
while(!feof($s)) {
$response .= fgets($s);
}
var_dump($response);
list($this->headers, $this->body) = explode("\r\n\r\n", $response);
// return $this->body;
}
}
class ExceptionData
{
function __construct($exception)
{
$this->exception = $exception;
$this->user_ip = $_SERVER["REMOTE_ADDR"];
$this->host_ip = $_SERVER["SERVER_ADDR"];
$this->request_method = $_SERVER["REQUEST_METHOD"];
$this->request_uri = $_SERVER["REQUEST_URI"];
}
function toXML()
{
$now = date("D M j H:i:s O Y");
$env = $this->envToXML();
$session = $this->sessionToXML();
$request_parameters = $this->requestToXML();
$trace = $this->exception->getTrace();
$class = $trace[0]["class"];
$function = $trace[0]["function"];
$message = $this->exception->getMessage();
$backtrace = $this->exception->getTraceAsString();
$error_class = get_class($this->exception);
return
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<error>
<agent_id>cc25f30e09d5d2e14cbdc7b0e1da30ba0896a58c</agent_id>
<controller_name>$class</controller_name>
<error_class>$error_class</error_class>
<action_name>$function</action_name>
<environment>
$env
</environment>
<session>
</session>
<rails_root>/</rails_root>
<url>$this->request_uri</url>
<parameters>
</parameters>
<occurred_at>$now</occurred_at>
<message>$message</message>
<backtrace>$backtrace</backtrace>
</error>";
}
function envToXML()
{
return $this->_arrayToXML($_ENV);
}
function sessionToXML()
{
return $this->_arrayToXML($_SESSION);
}
function requestToXML()
{
return $this->_arrayToXML($_REQUEST);
}
function _arrayToXML($array)
{
if(!is_array($array) || empty($array)) {
return " <no>values</no>";
}
$return_value = array();
foreach($array AS $key => $value) {
$key = strToLower($key);
$return_value[] = " <$key>$value</$key>";
}
return implode("\n", $return_value);
}
}
/*
POST to http://getexceptional.com/errors/?api_key=1234yuio123yiuo&protocol_version=2
<?xml version="1.0" encoding="UTF-8"?><error><agent_id>cc25f30e09d5d2e14cbdc7b0e1da30ba0896a58c</agent_id>
<controller_name>Foo</controller_name>
<action_name>bar</action_name>
<error_class>DodgyException</error_class>
<message>this is awesome</message>
<backtrace>the craziness</backtrace>
<occurred_at>Thu Sep 11 16:05:53 -0400 2008</occurred_at>
<rails_root>/var/www/woo/woo/woo</rails_root>
<url>http://www.tryme.com</url>
<environment>
<server_name>lovely</server_name>
<foo>nice</foo>
</environment>
<session>
<foo>bar</foo>
</session>
<parameters>POST DATA</parameters>
</error>"
*/