Skip to content

Commit aec375e

Browse files
committed
Updated to version 1.1.1
1 parent b4650c7 commit aec375e

File tree

13 files changed

+233
-183
lines changed

13 files changed

+233
-183
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# CHANGELOG
22

3+
## 1.1.1 - 2017-02-21
4+
* Added `Josantonius\ErrorHandler\ErrorHandler->getPreviewCode()` method.
5+
* Deleted `Josantonius\ErrorHandler\ErrorHandler->catchThrowable()` method.
6+
* Deleted `Josantonius\ErrorHandler\ErrorHandler->catchException()` method.
7+
* Changed `Josantonius\ErrorHandler\ErrorHandler->catchError()` method to `Josantonius\ErrorHandler\ErrorHandler->error()`.
8+
* Changed `Josantonius\ErrorHandler\ErrorHandler->prepareException()` method to `Josantonius\ErrorHandler\ErrorHandler->exception()`.
9+
* Changed `Josantonius\ErrorHandler\ErrorHandler->show()` method to `Josantonius\ErrorHandler\ErrorHandler->render()`.
10+
311
## 1.1.0 - 2017-01-30
412
* Compatible with PHP 5.6 or higher.
513

README-ES.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,17 @@ use Josantonius\ErrorHandler\ErrorHandler;
4949
Métodos disponibles en esta librería:
5050

5151
```php
52-
ErrorHandler->catchThrowable();
53-
ErrorHandler->catchException();
54-
ErrorHandler->catchError();
52+
ErrorHandler->error();
5553
ErrorHandler->exception();
56-
ErrorHandler->prepareException();
5754
ErrorHandler->getErrorType();
58-
ErrorHandler->show();
55+
ErrorHandler->render();
5956

6057
### Imágenes
6158

62-
Vista desde PHP
63-
![image](resources/images/example-error-handler-php-library.png)
64-
![image](resources/images/example-exception-handler-php-library.png)
65-
66-
Vista desde HHVM
67-
![image](resources/images/example-error-handler-hhvm-php-library.png)
68-
![image](resources/images/example-exception-handler-hhvm-php-library.png)
59+
![image](resources/images/exception.png)
60+
![image](resources/images/error.png)
61+
![image](resources/images/notice.png)
62+
![image](resources/images/warning.png)
6963

7064
```
7165
### Uso

README.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,16 @@ use Josantonius\ErrorHandler\ErrorHandler;
4949
Available methods in this library:
5050

5151
```php
52-
ErrorHandler->catchThrowable();
53-
ErrorHandler->catchException();
54-
ErrorHandler->catchError();
52+
ErrorHandler->error();
5553
ErrorHandler->exception();
56-
ErrorHandler->prepareException();
57-
ErrorHandler->getErrorType();
58-
ErrorHandler->show();
5954
```
6055

6156
### Images
6257

63-
View from PHP
64-
![image](resources/images/example-error-handler-php-library.png)
65-
![image](resources/images/example-exception-handler-php-library.png)
66-
67-
View from HHVM
68-
![image](resources/images/example-error-handler-hhvm-php-library.png)
69-
![image](resources/images/example-exception-handler-hhvm-php-library.png)
58+
![image](resources/images/exception.png)
59+
![image](resources/images/error.png)
60+
![image](resources/images/notice.png)
61+
![image](resources/images/warning.png)
7062

7163
### Usage
7264

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "josantonius/errorhandler",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"type": "library",
55
"description": "PHP library for handling exceptions and errors.",
66
"keywords": [

resources/images/error.png

55.7 KB
Loading

resources/images/exception.png

64.5 KB
Loading

resources/images/notice.png

50.4 KB
Loading

resources/images/warning.png

51.2 KB
Loading

src/ErrorHandler.php

Lines changed: 80 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* PHP library for handling exceptions and errors.
44
*
55
* @author Josantonius - info@josantonius.com
6-
* @copyright Copyright (c) 2016 JST PHP Framework
6+
* @copyright Copyright (c) 2016-2017
77
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
8-
* @link https://github.com/Josantonius/PHP-ErrorHandler
9-
* @since File available since 1.0.0 - Update: 2017-02-14
8+
* @link https://github.com/Josantonius/ErrorHandler
9+
* @since File available since 1.0.0 - Update: 2017-02-21
1010
*/
1111

1212
namespace Josantonius\ErrorHandler;
@@ -30,52 +30,25 @@ class ErrorHandler {
3030
protected static $stack;
3131

3232
/**
33-
* Catch errors and exceptions and execute the method.
34-
*
35-
* @since 1.0.0
36-
*/
37-
public function __construct() {
38-
39-
/**
40-
* Compatibility with exception and error handler for PHP7 and HHVM.
41-
*
42-
* @since 1.0.0
43-
*/
44-
if (!defined('HHVM_VERSION') && version_compare(phpversion(), '7.0.0', '>=')) {
45-
46-
set_exception_handler(array($this, 'catchThrowable'));
47-
48-
} else {
49-
50-
set_exception_handler(array($this, 'catchException'));
51-
}
52-
53-
set_error_handler(array($this, 'catchError'));
54-
}
55-
56-
/**
57-
* Handle exceptions catch.
33+
* Style load validator.
5834
*
5935
* @since 1.0.0
6036
*
61-
* @param object $e Throwable object
37+
* @var bool
6238
*/
63-
public function catchThrowable(Throwable $e) {
64-
65-
$this->prepareException($e);
66-
}
39+
protected static $styles = false;
6740

6841
/**
69-
* Handle exceptions catch.
42+
* Catch errors and exceptions and execute the method.
7043
*
7144
* @since 1.0.0
72-
*
73-
* @param object $e Exception object
7445
*/
75-
public function catchException(Exception $e) {
46+
public function __construct() {
47+
48+
set_exception_handler(array($this, 'exception'));
7649

77-
$this->prepareException($e);
78-
}
50+
set_error_handler(array($this, 'error'));
51+
}
7952

8053
/**
8154
* Handle error catch.
@@ -87,18 +60,21 @@ public function catchException(Exception $e) {
8760
* @param int $file → error file
8861
* @param int $line → error line
8962
*/
90-
public function catchError($code, $msg, $file, $line) {
63+
public function error($code, $msg, $file, $line) {
9164

9265
static::$stack = [
9366
'type' => $this->getErrorType($code),
9467
'message' => $msg,
9568
'file' => $file,
9669
'line' => $line,
97-
'code' => $code ? "[" . $code . "]" : "",
98-
'trace' => '',
70+
'code' => $code . " ·",
71+
'trace' => '',
72+
'preview' => '',
9973
];
10074

101-
$this->show();
75+
static::getPreviewCode($file, $line);
76+
77+
$this->render();
10278
}
10379

10480
/**
@@ -115,49 +91,41 @@ public function catchError($code, $msg, $file, $line) {
11591
* array $e->getTrace()
11692
* string $e->getTrace()[0]['file'] → file exception launcher
11793
* int $e->getTrace()[0]['line'] → line exception launcher
118-
* string $e->getTrace()[0]['function'] → function exception launcher
94+
* string $e->getTrace()[0]['function'] → function exception
11995
* string $e->getTrace()[0]['class'] → class exception launcher
12096
* string $e->getTrace()[0]['type'] → type exception launcher
12197
* array $e->getTrace()[0]['args'] → args exception launcher
12298
*
12399
* Optionally for libraries used in JST PHP Framework:
124100
* int $e->statusCode → HTTP response status code
125101
*/
126-
public function prepareException($e) {
102+
public function exception($e) {
127103

128104
static::$stack = [
129105
'type' => 'Exception',
130-
'message' => (null !== $e->getMessage()) ? $e->getMessage() : '',
131-
'file' => (null !== $e->getFile()) ? $e->getFile() : 0,
132-
'line' => (null !== $e->getLine()) ? $e->getLine() : 0,
133-
'code' => $e->getCode() ? "[" . $e->getCode() . "]" : "",
134-
'trace' => '',
106+
'message' => $e->getMessage(),
107+
'file' => $e->getFile(),
108+
'line' => $e->getLine(),
109+
'code' => $e->getCode() . " ·",
110+
'statusCode' => (isset($e->statusCode)) ? $e->statusCode : 0,
111+
'trace' => "\r\n<hr>BACKTRACE:\r\n",
112+
'preview' => "",
135113
];
136114

137-
if (is_array($e->getTrace()) && count($e->getTrace()) > 0) {
115+
static::getPreviewCode(static::$stack['file'], static::$stack['line']);
138116

139-
$tab = "\r\n";
117+
$trace = preg_split("/#[\d]/", $e->getTraceAsString());
140118

141-
foreach ($e->getTrace() as $key => $value) {
142-
143-
$tab .= "· · · · ";
119+
unset($trace[0]);
120+
121+
array_pop($trace);
144122

145-
$statusCode = (isset($e->statusCode)) ? $e->statusCode : 0;
146-
147-
static::$stack['trace'] = "\r\n\r\n<hr>" .
148-
149-
$tab . " [Trace " . ($key + 1) . "]" .
150-
$tab . " FILE: " . $value['file'] .
151-
$tab . " HTTP CODE: " . $statusCode .
152-
$tab . " FUNCTION: " . $value['function'] .
153-
$tab . " CLASS: " . $value['class'] .
154-
$tab . " ARGS: " . json_encode($value['args']) .
155-
$tab . " LINE: " . $value['line'] .
156-
$tab . " TYPE " . $value['type'] . "\r\n";
157-
}
123+
foreach ($trace as $key => $value) {
124+
125+
static::$stack['trace'] .= "\n" . $key . " ·" . $value;
158126
}
159127

160-
$this->show();
128+
$this->render();
161129
}
162130

163131
/**
@@ -204,34 +172,65 @@ protected function getErrorType($code) {
204172
case E_USER_DEPRECATED:
205173
return static::$stack['type'] = 'User Deprecated'; /* 16384 */
206174
default :
207-
return static::$stack['type'] = (string) $code;
208-
175+
return static::$stack['type'] = 'Error';
209176
}
177+
}
178+
179+
/**
180+
* Get preview of the error line.
181+
*
182+
* @since 1.1.0
183+
*
184+
* @param string $file → filepath
185+
* @param string $line → error line
186+
*/
187+
protected function getPreviewCode($file, $line) {
188+
189+
$file = file($file);
190+
191+
$start = ($line - 5 >= 0) ? $line - 5 : $line - 1;
192+
$end = ($line - 5 >= 0) ? $line + 4 : $line + 8;
193+
194+
for ($i = $start; $i < $end; $i++) {
210195

196+
if (!isset($file[$i])) {
197+
198+
continue;
199+
}
200+
201+
$text = trim($file[$i]);
202+
203+
if ($i == $line - 1) {
204+
205+
static::$stack['preview'] .=
206+
"<span class='jst-line'>" . ($i + 1) . "</span>" .
207+
"<span class='jst-mark text'>" . $text . "</span><br>";
208+
209+
continue;
210+
}
211+
212+
static::$stack['preview'] .=
213+
"<span class='jst-line'>" . ($i + 1) . "</span>" .
214+
"<span class='text'>" . $text . "</span><br>";
215+
}
211216
}
212217

213218
/**
214219
* Show alert in browser.
215220
*
216221
* @since 1.0.0
217-
*
218-
* @param string $type → error or exception
219222
*/
220-
protected function show() {
221-
222-
$css = __DIR__ . '/resources/styles.php';
223+
protected function render() {
223224

224225
static::$stack['mode'] = defined('HHVM_VERSION') ? 'HHVM' : 'PHP';
225226

226-
ob_start();
227+
if (!static::$styles) {
227228

228-
if (!isset(static::$stack['css'])) {
229+
static::$styles = true;
229230

230-
static::$stack['css'] = require($css);
231+
static::$stack['css'] = require(__DIR__ . '/resources/styles.php');
231232
}
232233

233234
require(__DIR__ . '/resources/view.php');
234-
235-
ob_end_flush();
236235
}
237236
}

src/Exception/ErrorHandlerException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* PHP library for handling exceptions and errors.
44
*
55
* @author Josantonius - info@josantonius.com
6-
* @copyright Copyright (c) 2016 JST PHP Framework
6+
* @copyright Copyright (c) 2016-2017
77
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
8-
* @link https://github.com/Josantonius/PHP-ErrorHandler
9-
* @since File available since 1.0.0 - Update: 2017-02-14
8+
* @link https://github.com/Josantonius/ErrorHandler
9+
* @since File available since 1.0.0 - Update: 2017-02-21
1010
*/
1111

1212
namespace Josantonius\ErrorHandler\Exception;

0 commit comments

Comments
 (0)