|
4 | 4 |
|
5 | 5 | namespace Bone; |
6 | 6 |
|
| 7 | +use Closure; |
| 8 | + |
7 | 9 | class Exception extends \Exception |
8 | 10 | { |
9 | 11 | const SHIVER_ME_TIMBERS = 'Application Error'; |
10 | 12 | const LOST_AT_SEA = 'Page not found.'; |
11 | 13 | const GHOST_SHIP = 'Record not found.'; |
| 14 | + |
| 15 | + public static function getShutdownHandler(): Closure |
| 16 | + { |
| 17 | + return function () { |
| 18 | + $error = error_get_last(); |
| 19 | + |
| 20 | + if ($error['type'] === E_ERROR) { |
| 21 | + $split = preg_split("/Stack\strace\:\n/", $error["message"]); |
| 22 | + $split2 = preg_split("/\sin\s/", $split[0]); |
| 23 | + $message = $split2[0]; |
| 24 | + $where = $split2[1]; |
| 25 | + $lines = explode("\n", $split[1]); |
| 26 | + $trace = ''; |
| 27 | + $row = 'odd'; |
| 28 | + |
| 29 | + foreach ($lines as $line) { |
| 30 | + $split = preg_split("/\s+/", $line, 2); |
| 31 | + $traceNo = $split[0]; |
| 32 | + $lineInfo = $split[1]; |
| 33 | + $trace .= "<tr class='$row'><td>$traceNo</td><td>$lineInfo</td></tr>"; |
| 34 | + $row = $row === 'odd' ? 'even' : 'odd'; |
| 35 | + } |
| 36 | + |
| 37 | + if (\getenv('APPLICATION_ENV') === 'production') { |
| 38 | + $message = 'There was an error'; |
| 39 | + $where = 'We apologise for the inconvenience.'; |
| 40 | + $trace = ''; |
| 41 | + } |
| 42 | + |
| 43 | + $content = " |
| 44 | +<html> |
| 45 | +<head> |
| 46 | + <style type='text/css'> |
| 47 | + #error { |
| 48 | + background: rgb(0,212,255); |
| 49 | + background: radial-gradient(circle, rgba(0,212,255,1) 0%, rgba(4,23,62,1) 78%); |
| 50 | + display: flex; |
| 51 | + justify-content: center; |
| 52 | + font-family: Helvetica |
| 53 | + } |
| 54 | + h1, h3 { |
| 55 | + font-weight: initial; |
| 56 | + } |
| 57 | + #details { |
| 58 | + width: 75%; |
| 59 | + background-color: #eee; |
| 60 | + padding: 20px; |
| 61 | + margin: 20px; |
| 62 | + } |
| 63 | + table { |
| 64 | + border: 1px solid #999; |
| 65 | + } |
| 66 | + tr { |
| 67 | + |
| 68 | + } |
| 69 | + tr.odd { |
| 70 | + background-color: #ddd; |
| 71 | + } |
| 72 | + tr.even { |
| 73 | + |
| 74 | + } |
| 75 | + td { |
| 76 | + padding: 10px; |
| 77 | + } |
| 78 | + </style> |
| 79 | +</head> |
| 80 | +<body id='error'> |
| 81 | +<div id='details'> |
| 82 | + <h1>$message</h1> |
| 83 | + <h3>$where</h3> |
| 84 | + <table> |
| 85 | + $trace |
| 86 | + </table> |
| 87 | + </div> |
| 88 | +</body> |
| 89 | + |
| 90 | +</html>"; |
| 91 | + echo $content; |
| 92 | + } |
| 93 | + }; |
| 94 | + } |
12 | 95 | } |
0 commit comments