Skip to content

Commit 4fa9d9e

Browse files
committed
add some helper. update namespace
1 parent 9c7aa45 commit 4fa9d9e

12 files changed

+221
-17
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
"autoload": {
2020
"psr-4": {
21-
"MyLib\\SimpleEvent\\" : "src/"
21+
"MyLib\\Error\\" : "src/"
2222
}
2323
},
2424
"suggest": {

src/ErrorHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* file that was distributed with this source code.
99
*/
1010

11-
namespace MyLib\Errors;
11+
namespace MyLib\Error;
1212

1313
use MyLib\ObjUtil\Obj;
1414
use MyLib\SysUtil\Helper\PhpHelper;

src/ErrorPayload.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
* @from https://github.com/phalcon/phalcon-devtools/blob/master/scripts/Phalcon/Error/AppError.php
88
*/
99

10-
namespace MyLib\Errors;
10+
namespace MyLib\Error;
1111

1212
/**
1313
* Class ErrorPayload
14-
* @package MyLib\Errors
14+
* @package MyLib\Error
1515
*
1616
* @method int type()
1717
* @method string message()

src/Handler/AbstractError.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
77
*/
88

9-
namespace MyLib\Errors\Handler;
9+
namespace MyLib\Error\Handler;
1010

1111
use Psr\Log\LoggerInterface;
1212

src/Handler/AbstractHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
77
*/
88

9-
namespace MyLib\Errors\Handler;
9+
namespace MyLib\Error\Handler;
1010

1111
use Psr\Http\Message\ServerRequestInterface;
1212

src/Handler/ErrorRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
77
*/
88

9-
namespace MyLib\Errors\Handler;
9+
namespace MyLib\Error\Handler;
1010

1111
use Inhere\Http\Body;
1212
use Psr\Http\Message\ResponseInterface;

src/Handler/NotAllowed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
77
*/
88

9-
namespace MyLib\Errors\Handler;
9+
namespace MyLib\Error\Handler;
1010

1111
use Inhere\Http\Body;
1212
use Psr\Http\Message\ResponseInterface;

src/Handler/NotFound.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
77
*/
88

9-
namespace MyLib\Errors\Handler;
9+
namespace MyLib\Error\Handler;
1010

1111
use Inhere\Http\Body;
1212
use Psr\Http\Message\ResponseInterface;

src/Helper/PhpError.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/10/15
6+
* Time: 上午10:04
7+
*/
8+
9+
namespace MyLib\Error\Helper;
10+
11+
/**
12+
* Class PhpError
13+
* @package MyLib\Error\Helper
14+
*/
15+
class PhpError
16+
{
17+
/** @var array */
18+
public static $fatalErrors = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR];
19+
20+
/**
21+
* $lastError = error_get_last();
22+
* @param array $lastError
23+
* @param null|string $catcher
24+
* @return array
25+
*/
26+
public static function toArray(array $lastError, $catcher = null)
27+
{
28+
$digest = 'Fatal Error (' . self::codeToString($lastError['type']) . '): ' . $lastError['message'];
29+
$data = [
30+
'code' => $lastError['type'],
31+
'message' => $lastError['message'],
32+
'file' => $lastError['file'],
33+
'line' => $lastError['line'],
34+
'catcher' => __METHOD__,
35+
];
36+
37+
if ($catcher) {
38+
$data['catcher'] = $catcher;
39+
}
40+
41+
return [$digest, $data];
42+
}
43+
44+
/**
45+
* @param int $code
46+
* @return string
47+
*/
48+
public static function codeToString(int $code)
49+
{
50+
switch ($code) {
51+
case E_ERROR:
52+
return 'E_ERROR';
53+
case E_WARNING:
54+
return 'E_WARNING';
55+
case E_PARSE:
56+
return 'E_PARSE';
57+
case E_NOTICE:
58+
return 'E_NOTICE';
59+
case E_CORE_ERROR:
60+
return 'E_CORE_ERROR';
61+
case E_CORE_WARNING:
62+
return 'E_CORE_WARNING';
63+
case E_COMPILE_ERROR:
64+
return 'E_COMPILE_ERROR';
65+
case E_COMPILE_WARNING:
66+
return 'E_COMPILE_WARNING';
67+
case E_USER_ERROR:
68+
return 'E_USER_ERROR';
69+
case E_USER_WARNING:
70+
return 'E_USER_WARNING';
71+
case E_USER_NOTICE:
72+
return 'E_USER_NOTICE';
73+
case E_STRICT:
74+
return 'E_STRICT';
75+
case E_RECOVERABLE_ERROR:
76+
return 'E_RECOVERABLE_ERROR';
77+
case E_DEPRECATED:
78+
return 'E_DEPRECATED';
79+
case E_USER_DEPRECATED:
80+
return 'E_USER_DEPRECATED';
81+
}
82+
83+
return 'Unknown PHP error';
84+
}
85+
}

src/Helper/PhpException.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/10/9
6+
* Time: 下午11:15
7+
*/
8+
9+
namespace MyLib\Error\Helper;
10+
11+
/**
12+
* Class PhpException
13+
* @package MyLib\PhpUtil
14+
*/
15+
class PhpException
16+
{
17+
/**
18+
* @see PhpException::toHtml()
19+
* {@inheritdoc}
20+
*/
21+
public static function toString($e, $getTrace = true, $catcher = null): string
22+
{
23+
return self::toHtml($e, $getTrace, $catcher, true);
24+
}
25+
26+
/**
27+
* Converts an exception into a simple string.
28+
* @param \Exception|\Throwable $e the exception being converted
29+
* @param bool $clearHtml
30+
* @param bool $getTrace
31+
* @param null|string $catcher
32+
* @return string the string representation of the exception.
33+
*/
34+
public static function toHtml($e, $getTrace = true, $catcher = null, $clearHtml = false): string
35+
{
36+
if (!$getTrace) {
37+
$message = "Error: {$e->getMessage()}";
38+
} else {
39+
$message = sprintf(
40+
"<h3>%s(%d): %s</h3>\n<pre><strong>File: %s(Line %d)</strong>%s \n\n%s</pre>",
41+
\get_class($e),
42+
$e->getCode(),
43+
$e->getMessage(),
44+
$e->getFile(),
45+
$e->getLine(),
46+
$catcher ? "\nCatch By: $catcher" : '',
47+
$e->getTraceAsString()
48+
);
49+
}
50+
51+
return $clearHtml ? strip_tags($message) : "<div class=\"exception-box\">{$message}</div>";
52+
}
53+
54+
/**
55+
* Converts an exception into a simple array.
56+
* @param \Exception|\Throwable $e the exception being converted
57+
* @param bool $getTrace
58+
* @param null|string $catcher
59+
* @return array
60+
*/
61+
public static function toArray($e, $getTrace = true, $catcher = null)
62+
{
63+
$data = [
64+
'class' => \get_class($e),
65+
'message' => $e->getMessage(),
66+
'code' => $e->getCode(),
67+
'file' => $e->getFile() . ':' . $e->getLine(),
68+
];
69+
70+
if ($catcher) {
71+
$data['catcher'] = $catcher;
72+
}
73+
74+
if ($getTrace) {
75+
$data['trace'] = $e->getTrace();
76+
}
77+
78+
return $data;
79+
}
80+
81+
/**
82+
* Converts an exception into a json string.
83+
* @param \Exception|\Throwable $e the exception being converted
84+
* @param bool $getTrace
85+
* @param null|string $catcher
86+
* @return string the string representation of the exception.
87+
*/
88+
public static function toJson($e, $getTrace = true, $catcher = null)
89+
{
90+
if (!$getTrace) {
91+
$message = json_encode(['msg' => "Error: {$e->getMessage()}"]);
92+
} else {
93+
$map = [
94+
'code' => $e->getCode() ?: 500,
95+
'msg' => sprintf(
96+
'%s(%d): %s, File: %s(Line %d)',
97+
\get_class($e),
98+
$e->getCode(),
99+
$e->getMessage(),
100+
$e->getFile(),
101+
$e->getLine()
102+
),
103+
'data' => $e->getTrace()
104+
];
105+
106+
if ($catcher) {
107+
$map['catcher'] = $catcher;
108+
}
109+
110+
if ($getTrace) {
111+
$map['trace'] = $e->getTrace();
112+
}
113+
114+
$message = json_encode($map);
115+
}
116+
117+
return $message;
118+
}
119+
}

0 commit comments

Comments
 (0)