|
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
|
|
|
class Formatter |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Returns all basic information about the exception in a simple array |
|
13
|
|
|
* for further convertion to other languages |
|
14
|
|
|
* @param Inspector $inspector |
|
15
|
|
|
* @param bool $shouldAddTrace |
|
16
|
|
|
* @return array |
|
17
|
|
|
*/ |
|
18
|
1 |
|
public static function formatExceptionAsDataArray(Inspector $inspector, $shouldAddTrace) |
|
19
|
|
|
{ |
|
20
|
1 |
|
$exception = $inspector->getException(); |
|
21
|
|
|
$response = [ |
|
22
|
1 |
|
'type' => get_class($exception), |
|
23
|
1 |
|
'message' => $exception->getMessage(), |
|
24
|
1 |
|
'code' => $exception->getCode(), |
|
25
|
1 |
|
'file' => $exception->getFile(), |
|
26
|
1 |
|
'line' => $exception->getLine(), |
|
27
|
1 |
|
]; |
|
28
|
|
|
|
|
29
|
1 |
|
if ($shouldAddTrace) { |
|
30
|
|
|
$frames = $inspector->getFrames(); |
|
31
|
|
|
$frameData = []; |
|
32
|
|
|
|
|
33
|
|
|
foreach ($frames as $frame) { |
|
34
|
|
|
/** @var Frame $frame */ |
|
35
|
|
|
$frameData[] = [ |
|
36
|
|
|
'file' => $frame->getFile(), |
|
37
|
|
|
'line' => $frame->getLine(), |
|
38
|
|
|
'function' => $frame->getFunction(), |
|
39
|
|
|
'class' => $frame->getClass(), |
|
40
|
|
|
'args' => $frame->getArgs(), |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$response['trace'] = $frameData; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
return $response; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public static function formatExceptionPlain(Inspector $inspector) |
|
51
|
|
|
{ |
|
52
|
1 |
|
$message = $inspector->getException()->getMessage(); |
|
53
|
1 |
|
$frames = $inspector->getFrames(); |
|
54
|
|
|
|
|
55
|
1 |
|
$plain = $inspector->getExceptionName(); |
|
56
|
1 |
|
$plain .= ' thrown with message "'; |
|
57
|
1 |
|
$plain .= $message; |
|
58
|
1 |
|
$plain .= '"'."\n\n"; |
|
59
|
|
|
|
|
60
|
1 |
|
$plain .= "Stacktrace:\n"; |
|
61
|
1 |
|
foreach ($frames as $i => $frame) { |
|
62
|
1 |
|
$plain .= "#". (count($frames) - $i - 1). " "; |
|
63
|
1 |
|
$plain .= $frame->getClass() ?: ''; |
|
64
|
1 |
|
$plain .= $frame->getClass() && $frame->getFunction() ? ":" : ""; |
|
65
|
1 |
|
$plain .= $frame->getFunction() ?: ''; |
|
66
|
1 |
|
$plain .= ' in '; |
|
67
|
1 |
|
$plain .= ($frame->getFile() ?: '<#unknown>'); |
|
68
|
1 |
|
$plain .= ':'; |
|
69
|
1 |
|
$plain .= (int) $frame->getLine(). "\n"; |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
return $plain; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|