Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 15 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| SupportExceptionHandler | |
0.00% |
0 / 15 |
|
0.00% |
0 / 3 |
20 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| mapSeverity | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Modules\SupportModule\Handlers; |
| 4 | |
| 5 | use Throwable; |
| 6 | use App\Modules\SupportModule\Services\IncidentReporterService; |
| 7 | |
| 8 | class SupportExceptionHandler |
| 9 | { |
| 10 | protected IncidentReporterService $reporter; |
| 11 | |
| 12 | public function __construct() |
| 13 | { |
| 14 | $this->reporter = service('incidentReporter'); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Capture centralisée des exceptions |
| 19 | * (appelée depuis Config\Exceptions::handler) |
| 20 | */ |
| 21 | public function handle(Throwable $exception, int $statusCode = 500): void |
| 22 | { |
| 23 | try { |
| 24 | $this->reporter->report($exception, [ |
| 25 | 'source' => 'global_exception_handler', |
| 26 | 'level' => $this->mapSeverity($statusCode), |
| 27 | 'status_code' => $statusCode, |
| 28 | 'channel' => 'exception_handler', |
| 29 | ]); |
| 30 | |
| 31 | } catch (Throwable $e) { |
| 32 | |
| 33 | // fallback 1 : log système CI4 |
| 34 | log_message('critical', '[SupportModule-FATAL] ' . $e->getMessage()); |
| 35 | |
| 36 | // fallback 2 : toujours tracer l’erreur initiale |
| 37 | log_message('error', '[SupportModule-ORIGINAL] ' . $exception->getMessage()); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Normalisation de la sévérité |
| 43 | */ |
| 44 | protected function mapSeverity(int $statusCode): string |
| 45 | { |
| 46 | return match (true) { |
| 47 | $statusCode >= 500 => 'critical', |
| 48 | $statusCode >= 400 => 'error', |
| 49 | default => 'warning', |
| 50 | }; |
| 51 | } |
| 52 | } |