-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptiontrap.php
More file actions
executable file
·146 lines (127 loc) · 4.53 KB
/
Exceptiontrap.php
File metadata and controls
executable file
·146 lines (127 loc) · 4.53 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
<?php
require_once dirname(__FILE__) . '/Exceptiontrap/Data.php';
require_once dirname(__FILE__) . '/Exceptiontrap/Sender.php';
class Exceptiontrap
{
public static $apiKey;
public static $ssl;
public static $notifierName = 'exceptiontrap-php';
public static $notifierVersion = '1.1.1';
// $notifierUrl = 'https://github.com/itmlabs/exceptiontrap-php';
public static $apiVersion = '1';
public static $apiUrl = 'exceptiontrap.com/notifier/api/v1/problems.json';
public static $timeout = 2;
private static $oldExceptionHandler;
private static $oldErrorHandler;
public static $controller;
public static $action;
public static $module;
public static $environment;
public static $filterParams = array();
private static $customParams = array();
private static $ignoreList = array();
/**
* @var array List of the error types that should be catched
*/
private static $error_types_to_catch = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR);
/**
* Set the request components (module, controller and action)
*
* @param array $data List of request components
*/
public static function setRequestComponents($data = array())
{
if (isset($data['controller'])) self::$controller = $data['controller'];
if (isset($data['action'])) self::$action = $data['action'];
if (isset($data['module'])) self::$module = $data['module'];
}
/**
* Set a list of params that should be filtered (value = [FILTERED])
*
* @param array $filterParams List of params
*/
public static function setFilterParams($filterParams = array())
{
self::$filterParams = $filterParams;
}
public static function setCustomParam($name, $value)
{
self::$customParams[$name] = $value; # TODO: Use in ExceptiontrapData
}
/**
* Set a list of exceptions that should be ignored (not sent) (overwrites setup)
*
* @param array $ignoreList List of exceptions names
*/
public static function setIgnoreList($ignoreList)
{
self::$ignoreList = $ignoreList;
}
/**
* Activate the exceptiontrap
*
* @param string $apiKey The api-key of your application at exceptiontrap.com, may be overwritten by env
* @param boolean $ssl Use SSL for the connection
* @param string $environment The envíronment, in which the application is running
* @param array $ignoreList List of exceptions names which should be ignored (not sent)
*/
public static function setup($apiKey = null, $ssl = false, $environment = 'production', $ignoreList = array())
{
// Use env api key over initializer api key, if set there. For example when used with Heroku and the Exceptiontrap Heroku addon
self::$apiKey = getenv('EXCEPTIONTRAP_API_KEY') ? getenv('EXCEPTIONTRAP_API_KEY') : $apiKey;
self::$environment = $environment;
self::$ssl = $ssl;
self::$ignoreList = $ignoreList;
// self::$client = $client;
// self::$timeout = $timeout;
if (self::$apiKey != "") {
self::installNotifier();
}
}
/* TODO: Move to Catcher class */
private static function installNotifier()
{
// self::$oldErrorHandler = set_error_handler(array('Exceptiontrap', 'handleError'));
self::$oldExceptionHandler = set_exception_handler(array('Exceptiontrap', 'handleException'));
register_shutdown_function(array('Exceptiontrap', 'handleShutdown'));
}
public static function handleError($code, $message, $file, $line, $shutdown = false)
{
// if FATAL error or similar, delegate to exception handler
if (in_array($code, self::$error_types_to_catch)) {
self::handleException(new ErrorException($message, $code, $code, $file, $line));
}
// call old error handler
if (self::$oldErrorHandler && !$shutdown) {
call_user_func(self::$oldErrorHandler, $code, $message, $file, $line);
}
}
/**
* Handles a given exception
*
* @param Exception $exception
*/
public static function handleException($exception)
{
// get data
$data = new ExceptiontrapData($exception);
// send or ignore
if (!in_array(get_class($exception), self::$ignoreList)){
ExceptiontrapSender::notify($data); // send it with notifier
}
// call old exception handler
if (self::$oldExceptionHandler) {
call_user_func(self::$oldExceptionHandler, $exception);
}
}
/**
* Handles the php shutdown function for fatal errors
*/
public static function handleShutdown()
{
if ($error = error_get_last()) {
self::handleError($error['type'], $error['message'], $error['file'], $error['line'], true);
}
}
/* - End Catcher */
}