Skip to content

Commit 433121e

Browse files
committed
separate ExceptionHandler from ExceptionHandlerMiddleware
1 parent 897da9c commit 433121e

File tree

4 files changed

+84
-55
lines changed

4 files changed

+84
-55
lines changed

lib/Diagnostics/ExceptionHandler.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* @author Mohammed Moussaoui
5+
* @license MIT license. For more license information, see the LICENSE file in the root directory.
6+
* @link https://github.com/DevNet-Framework
7+
*/
8+
9+
namespace DevNet\Core\Diagnostics;
10+
11+
use DevNet\Core\View\ViewManager;
12+
use Throwable;
13+
14+
class ExceptionHandler
15+
{
16+
private ViewManager $view;
17+
18+
public function __construct()
19+
{
20+
$this->view = new ViewManager(__DIR__);
21+
}
22+
23+
public function handle(Throwable $error): string
24+
{
25+
$data = $this->parse($error);
26+
return $this->view->render('ExceptionView', $data);
27+
}
28+
29+
public function parse(Throwable $error): array
30+
{
31+
$severities = [
32+
E_ERROR => 'Fatal Error',
33+
E_WARNING => 'Warning',
34+
E_PARSE => 'Parse Error',
35+
E_NOTICE => 'Notice',
36+
E_CORE_ERROR => 'Core Error',
37+
E_CORE_WARNING => 'Core Warning',
38+
E_COMPILE_ERROR => 'Compile Error',
39+
E_COMPILE_WARNING => 'Compile Warning',
40+
E_USER_ERROR => 'User Error',
41+
E_USER_WARNING => 'User Warning',
42+
E_USER_NOTICE => 'User Notice',
43+
E_STRICT => 'Strict Error',
44+
E_RECOVERABLE_ERROR => 'Recoverable Error',
45+
E_DEPRECATED => 'Deprecated',
46+
E_USER_DEPRECATED => 'User Deprecated'
47+
];
48+
49+
$trace = $error->getTrace();
50+
if ($error instanceof \ErrorException) {
51+
$severity = $severities[$error->getSeverity()];
52+
} else {
53+
$severity = $severities[E_ERROR];
54+
}
55+
56+
$firstFile = $trace[0]['file'] ?? null;
57+
58+
if ($error->getFile() == $firstFile) {
59+
array_shift($trace);
60+
}
61+
62+
if ($error->getCode() == 0) {
63+
$code = '';
64+
} else {
65+
$code = $error->getCode();
66+
}
67+
68+
$data['error'] = $severity;
69+
$data['message'] = $error->getMessage();
70+
$data['class'] = get_class($error);
71+
$data['code'] = $code;
72+
$data['file'] = $error->getFile();
73+
$data['line'] = $error->getLine();
74+
$data['trace'] = $trace;
75+
76+
return $data;
77+
}
78+
}
File renamed without changes.
File renamed without changes.

lib/Middlewares/ExceptionHandlerMiddleware.php

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
namespace DevNet\Core\Middlewares;
1010

11-
use DevNet\System\MethodTrait;
11+
use DevNet\Core\Diagnostics\ExceptionHandler;
1212
use DevNet\Http\Message\HttpContext;
1313
use DevNet\Http\Middleware\IMiddleware;
1414
use DevNet\Http\Middleware\RequestDelegate;
15-
use DevNet\Core\View\ViewManager;
15+
use DevNet\System\MethodTrait;
1616
use Throwable;
1717

1818
use function DevNet\System\await;
@@ -22,10 +22,12 @@ class ExceptionHandlerMiddleware implements IMiddleware
2222
use MethodTrait;
2323

2424
private ?string $errorHandlingPath;
25+
private ExceptionHandler $handler;
2526

2627
public function __construct(?string $errorHandlingPath = null)
2728
{
2829
$this->errorHandlingPath = $errorHandlingPath;
30+
$this->handler = new ExceptionHandler();
2931
}
3032

3133
public function async_invoke(HttpContext $context, RequestDelegate $next): void
@@ -59,59 +61,8 @@ public function async_invoke(HttpContext $context, RequestDelegate $next): void
5961
}
6062

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

0 commit comments

Comments
 (0)