1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Whoops - php errors for cool kids |
4
|
|
|
* @author Filipe Dobreira <http://github.com/filp> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Whoops\Exception; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
use Whoops\Handler\PlainTextHandler; |
11
|
|
|
|
12
|
|
|
class Formatter |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Returns all basic information about the exception in a simple array |
16
|
|
|
* for further convertion to other languages |
17
|
|
|
* @param Inspector $inspector |
18
|
|
|
* @param bool $shouldAddTrace |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
1 |
|
public static function formatExceptionAsDataArray(Inspector $inspector, $shouldAddTrace) |
22
|
|
|
{ |
23
|
1 |
|
$exception = $inspector->getException(); |
24
|
|
|
$response = [ |
25
|
1 |
|
'type' => get_class($exception), |
26
|
1 |
|
'message' => $exception->getMessage(), |
27
|
1 |
|
'file' => $exception->getFile(), |
28
|
1 |
|
'line' => $exception->getLine(), |
29
|
1 |
|
]; |
30
|
|
|
|
31
|
1 |
|
if ($shouldAddTrace) { |
32
|
|
|
$frames = $inspector->getFrames(); |
33
|
|
|
$frameData = []; |
34
|
|
|
|
35
|
|
|
foreach ($frames as $frame) { |
36
|
|
|
/** @var Frame $frame */ |
37
|
|
|
$frameData[] = [ |
38
|
|
|
'file' => $frame->getFile(), |
39
|
|
|
'line' => $frame->getLine(), |
40
|
|
|
'function' => $frame->getFunction(), |
41
|
|
|
'class' => $frame->getClass(), |
42
|
|
|
'args' => $frame->getArgs(), |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$response['trace'] = $frameData; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
return $response; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Inspector $inspector |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
* |
57
|
|
|
* @deprecated |
58
|
|
|
*/ |
59
|
1 |
|
public static function formatExceptionPlain(Inspector $inspector) |
60
|
|
|
{ |
61
|
1 |
|
@trigger_error( |
|
|
|
|
62
|
1 |
|
sprintf( |
63
|
1 |
|
'Plain exception formatter is deprecated and will be removed in next releases. Use %s instead', |
64
|
|
|
PlainTextHandler::class . '::generateResponse' |
65
|
1 |
|
), |
66
|
|
|
E_USER_DEPRECATED |
67
|
1 |
|
); |
68
|
|
|
|
69
|
1 |
|
$message = $inspector->getException()->getMessage(); |
70
|
1 |
|
$frames = $inspector->getFrames(); |
71
|
|
|
|
72
|
1 |
|
$plain = $inspector->getExceptionName(); |
73
|
1 |
|
$plain .= ' thrown with message "'; |
74
|
1 |
|
$plain .= $message; |
75
|
1 |
|
$plain .= '"'."\n\n"; |
76
|
|
|
|
77
|
1 |
|
$plain .= "Stacktrace:\n"; |
78
|
1 |
|
foreach ($frames as $i => $frame) { |
79
|
1 |
|
$plain .= "#". (count($frames) - $i - 1). " "; |
80
|
1 |
|
$plain .= $frame->getClass() ?: ''; |
81
|
1 |
|
$plain .= $frame->getClass() && $frame->getFunction() ? ":" : ""; |
82
|
1 |
|
$plain .= $frame->getFunction() ?: ''; |
83
|
1 |
|
$plain .= ' in '; |
84
|
1 |
|
$plain .= ($frame->getFile() ?: '<#unknown>'); |
85
|
1 |
|
$plain .= ':'; |
86
|
1 |
|
$plain .= (int) $frame->getLine(). "\n"; |
87
|
1 |
|
} |
88
|
|
|
|
89
|
1 |
|
return $plain; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: