Passed
Push — develop ( 3fe5f6...046eda )
by Nikolay
05:49 queued 13s
created

handleAndLogTheException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
declare(strict_types=1);
21
22
namespace MikoPBX\Common\Providers;
23
24
25
use MikoPBX\Core\System\Util;
26
use MikoPBX\Modules\PbxExtensionUtils;
27
use Throwable;
28
use Whoops\Handler\JsonResponseHandler;
29
use Whoops\Handler\PlainTextHandler;
30
use Whoops\Handler\PrettyPageHandler;
31
use Whoops\Run;
32
use Whoops;
33
use Phalcon\Di\DiInterface;
34
use Phalcon\Di\ServiceProviderInterface;
35
/**
36
 * Registers whoops error handler service provider.
37
 *
38
 * @package MikoPBX\Common\Providers
39
 */
40
class WhoopsErrorHandlerProvider implements ServiceProviderInterface
41
{
42
    public const SERVICE_NAME = 'whoopsErrorHandler';
43
44
    /**
45
     * Registers whoops error handler service provider
46
     *
47
     * @param DiInterface $di The DI container.
48
     */
49
    public function register(DiInterface $di): void
50
    {
51
        $di->set(
52
            self::SERVICE_NAME,
53
            function () {
54
                $whoops = new Run();
55
                $whoops->pushHandler(function ($exception) {
56
                    return WhoopsErrorHandlerProvider::handleModuleException($exception);
57
                });
58
                $whoops->pushHandler(function ($exception) {
59
                    return WhoopsErrorHandlerProvider::handleAndLogTheException($exception);
60
                });
61
                if (Whoops\Util\Misc::isAjaxRequest()) {
62
                    $handler = new JsonResponseHandler();
63
                } elseif (Whoops\Util\Misc::isCommandLine()) {
64
                    $handler = new PlainTextHandler();
65
                } else {
66
                    $handler = new PrettyPageHandler();
67
                }
68
                $whoops->appendHandler($handler);
69
                $whoops->register();
70
                return $whoops;
71
            }
72
        );
73
    }
74
75
    /**
76
     * Handles exception and write message into syslog.
77
     *
78
     * @param Throwable $exception The exception to handle.
79
     * @return int The Whoops handler status.
80
     */
81
    private static function handleAndLogTheException(Throwable $exception): int
82
    {
83
        $message = WhoopsErrorHandlerProvider::makePrettyErrorDescription($exception);
84
        Util::sysLogMsg(__METHOD__, $message, LOG_ERR);
85
        return \Whoops\Handler\Handler::DONE;
86
    }
87
88
    /**
89
     * Handles exception and disables the corresponding module.
90
     *
91
     * @param Throwable $exception The exception to handle.
92
     * @return int The Whoops handler status.
93
     */
94
    private static function handleModuleException(Throwable $exception): int
95
    {
96
        $exceptionFile = $exception->getFile();
97
        PbxExtensionUtils::disableBadModule($exceptionFile);
98
        return \Whoops\Handler\Handler::DONE;
99
    }
100
101
102
    /**
103
     * Generates an error description for a given exception.
104
     *
105
     * @param Throwable $exception The exception for which to generate the error description.
106
     * @param bool $jsonResult Indicates whether the error description should be in JSON format.
107
     * @return string The generated error description.
108
     */
109
    public static function makePrettyErrorDescription(Throwable $exception, bool $jsonResult = false): string
110
    {
111
        $whoops = new Run();
112
        if ($jsonResult) {
113
            $whoops->pushHandler(new JsonResponseHandler());
114
        } else {
115
            $whoops->pushHandler(new PlainTextHandler());
116
        }
117
        $whoops->register();
118
        $whoops->allowQuit(false);
119
        return $whoops->handleException($exception);
120
    }
121
}