• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CPS-IT / project-builder / 19698221019

26 Nov 2025 09:04AM UTC coverage: 96.489% (-0.04%) from 96.529%
19698221019

push

github

web-flow
Merge pull request #830 from CPS-IT/task/codeclimate

2226 of 2307 relevant lines covered (96.49%)

14.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.83
/src/Error/ErrorHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "cpsit/project-builder".
7
 *
8
 * Copyright (C) 2022 Elias Häußler <e.haeussler@familie-redlich.de>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace CPSIT\ProjectBuilder\Error;
25

26
use CPSIT\ProjectBuilder\IO;
27
use CuyZ\Valinor\Mapper;
28
use Throwable;
29

30
use function method_exists;
31

32
/**
33
 * ErrorHandler.
34
 *
35
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
36
 * @license GPL-3.0-or-later
37
 */
38
final class ErrorHandler
39
{
40
    public function __construct(
17✔
41
        private readonly IO\Messenger $messenger,
42
    ) {}
17✔
43

44
    public function handleException(Throwable $exception): void
13✔
45
    {
46
        $previousException = $exception->getPrevious();
13✔
47

48
        $this->messenger->error($exception->getMessage().$this->formatExceptionCode($exception));
13✔
49

50
        if ($exception instanceof Mapper\MappingError) {
13✔
51
            $this->formatMappingErrors($exception);
1✔
52
        }
53

54
        if (null !== $previousException) {
13✔
55
            $this->messenger->error(
5✔
56
                'Caused by: '.$previousException->getMessage().$this->formatExceptionCode($previousException),
5✔
57
            );
5✔
58
        }
59

60
        if ($this->messenger->isVerbose()) {
13✔
61
            $this->messenger->newLine();
1✔
62

63
            throw $exception;
1✔
64
        }
65
    }
66

67
    private function formatMappingErrors(Mapper\MappingError $error): void
1✔
68
    {
69
        /* @phpstan-ignore function.alreadyNarrowedType */
70
        if (method_exists($error, 'node')) {
1✔
71
            // @todo Remove once support for Valinor v1 is dropped
72
            $messages = Mapper\Tree\Message\Messages::flattenFromNode($error->node())->errors();
×
73
        } else {
74
            /** @var Mapper\Tree\Message\Messages $messages */
75
            /* @phpstan-ignore method.notFound, method.nonObject */
76
            $messages = $error->messages()->errors();
1✔
77
        }
78

79
        foreach ($messages as $message) {
1✔
80
            $this->messenger->error('- '.$message);
1✔
81
        }
82
    }
83

84
    private function formatExceptionCode(Throwable $exception): string
13✔
85
    {
86
        if ($exception->getCode() > 0) {
13✔
87
            return sprintf(' [%d]', $exception->getCode());
9✔
88
        }
89

90
        return '';
6✔
91
    }
92
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc