Skip to content
This repository was archived by the owner on Apr 24, 2021. It is now read-only.

Commit 3e0e24a

Browse files
committed
add error and ex helper class
1 parent ecf15fd commit 3e0e24a

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed

src/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\PhpUtil;
10+
11+
/**
12+
* Class PhpError
13+
* @package MyLib\PhpUtil
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/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\PhpUtil;
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)