Skip to content

Commit 7b0d1d6

Browse files
committed
merge DeveloperExceptionPage into ExceptionHandler
1 parent 7751156 commit 7b0d1d6

File tree

3 files changed

+59
-108
lines changed

3 files changed

+59
-108
lines changed

lib/Exception/ExceptionHandlerMiddleware.php

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use DevNet\Web\Http\HttpContext;
1414
use DevNet\Web\Middleware\IMiddleware;
1515
use DevNet\Web\Middleware\RequestDelegate;
16+
use DevNet\Web\View\ViewManager;
1617
use Throwable;
1718

1819
use function Devnet\System\await;
@@ -42,7 +43,7 @@ public function async_invoke(HttpContext $context, RequestDelegate $next): void
4243
throw new $error;
4344
}
4445

45-
// Need to remove the previous headers and body of the response the send only the error report.
46+
// must remove the previous headers and body of the response the send only the error report.
4647
$context->Response->Body->truncate(0);
4748
$headerNames = array_keys($context->Response->Headers->getAll());
4849
foreach ($headerNames as $name) {
@@ -58,10 +59,60 @@ public function async_invoke(HttpContext $context, RequestDelegate $next): void
5859
return;
5960
}
6061

61-
// Handle the error exception as Http status code.
62+
// Display the error exception page report.
63+
$data = $this->parse($error);
64+
$view = new ViewManager(__DIR__ . '/Views');
65+
await($context->Response->writeAsync($view->render('ExceptionView', $data)));
66+
}
67+
}
68+
69+
public function parse(Throwable $error): array
70+
{
71+
$severities = [
72+
E_ERROR => 'Fatal Error',
73+
E_WARNING => 'Warning',
74+
E_PARSE => 'Parse Error',
75+
E_NOTICE => 'Notice',
76+
E_CORE_ERROR => 'Core Error',
77+
E_CORE_WARNING => 'Core Warning',
78+
E_COMPILE_ERROR => 'Compile Error',
79+
E_COMPILE_WARNING => 'Compile Warning',
80+
E_USER_ERROR => 'User Error',
81+
E_USER_WARNING => 'User Warning',
82+
E_USER_NOTICE => 'User Notice',
83+
E_STRICT => 'Strict Error',
84+
E_RECOVERABLE_ERROR => 'Recoverable Error',
85+
E_DEPRECATED => 'Deprecated',
86+
E_USER_DEPRECATED => 'User Deprecated'
87+
];
88+
89+
$trace = $error->getTrace();
90+
if ($error instanceof \ErrorException) {
91+
$severity = $severities[$error->getSeverity()];
92+
} else {
93+
$severity = $severities[E_ERROR];
94+
}
95+
96+
$firstFile = $trace[0]['file'] ?? null;
97+
98+
if ($error->getFile() == $firstFile) {
99+
array_shift($trace);
100+
}
101+
102+
if ($error->getCode() == 0) {
103+
$code = '';
104+
} else {
62105
$code = $error->getCode();
63-
$code = $code >= 400 ? $code : 500;
64-
$context->Response->setStatusCode($code);
65106
}
107+
108+
$data['error'] = $severity;
109+
$data['message'] = $error->getMessage();
110+
$data['class'] = get_class($error);
111+
$data['code'] = $code;
112+
$data['file'] = $error->getFile();
113+
$data['line'] = $error->getLine();
114+
$data['trace'] = $trace;
115+
116+
return $data;
66117
}
67118
}

lib/Exception/ExceptionPageMiddleware.php

Lines changed: 0 additions & 102 deletions
This file was deleted.

lib/Hosting/WebHost.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use DevNet\System\Dependency\IServiceProvider;
1515
use DevNet\System\Dependency\ServiceCollection;
1616
use DevNet\System\Runtime\LauncherProperties;
17-
use DevNet\Web\Exception\ExceptionPageMiddleware;
17+
use DevNet\Web\Exception\ExceptionHandlerMiddleware;
1818
use DevNet\Web\Http\HttpContext;
1919
use DevNet\Web\Http\HttpContextFactory;
2020
use DevNet\Web\Middleware\IApplicationBuilder;
@@ -37,7 +37,9 @@ public function __construct(IApplicationBuilder $AppBuilder)
3737

3838
public function start(Closure $configure): void
3939
{
40-
$this->appBuilder->use(new ExceptionPageMiddleware());
40+
if ($this->appBuilder->Environment->isDevelopment()) {
41+
$this->appBuilder->use(new ExceptionHandlerMiddleware());
42+
}
4143

4244
if (PHP_SAPI == 'cli') {
4345
$configure($this->appBuilder);

0 commit comments

Comments
 (0)