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

letsdrink / ouzo / 7170637656

11 Dec 2023 04:47PM UTC coverage: 85.672% (-0.3%) from 85.99%
7170637656

push

github

web-flow
Refactored exception/error handlers. (#322)

14 of 30 new or added lines in 6 files covered. (46.67%)

2 existing lines in 1 file now uncovered.

4963 of 5793 relevant lines covered (85.67%)

67.63 hits per line

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

48.39
/src/Ouzo/Core/ExceptionHandling/ExceptionHandler.php
1
<?php
2
/*
3
 * Copyright (c) Ouzo contributors, https://github.com/letsdrink/ouzo
4
 * This file is made available under the MIT License (view the LICENSE file for more information).
5
 */
6

7
namespace Ouzo\ExceptionHandling;
8

9
use Exception;
10
use Ouzo\Config;
11
use Ouzo\Routing\RouterException;
12
use Ouzo\UserException;
13

14
class ExceptionHandler
15
{
16
    private bool $errorHandled = false;
17
    private bool $isCli;
18
    private Renderer $errorRenderer;
19

20
    public function __construct(?Renderer $errorRenderer = null)
21
    {
22
        global $argv;
23
        $this->isCli = isset($argv[0]);
15✔
24
        $this->errorRenderer = is_null($errorRenderer) ? ($this->isCli ? new CliErrorRenderer() : new ErrorRenderer()) : $errorRenderer;
15✔
25
    }
26

27
    public function handleException($exception): void
28
    {
29
        if (!$this->runOuzoExceptionHandler($exception)) {
6✔
30
            $this->runDefaultHandler($exception);
3✔
31
        }
32
    }
33

34
    protected function runOuzoExceptionHandler($exception): bool
35
    {
36
        if ($exception instanceof UserException) {
6✔
37
            $this->renderUserError(OuzoExceptionData::forException(500, $exception));
×
38
            return true;
×
39
        }
40
        if ($exception instanceof RouterException) {
6✔
41
            $this->handleError(OuzoExceptionData::forException(404, $exception));
3✔
42
            return true;
3✔
43
        }
44
        if ($exception instanceof OuzoException) {
3✔
45
            $this->handleError($exception->asExceptionData());
×
46
            return true;
×
47
        }
48
        return false;
3✔
49
    }
50

51
    protected function runDefaultHandler($exception): void
52
    {
53
        $this->handleError(OuzoExceptionData::forException(500, $exception));
3✔
54
    }
55

56
    public function handleExceptionData(OuzoExceptionData $exceptionData): void
57
    {
58
        $this->handleError($exceptionData);
×
59
    }
60

61
    public function lastErrorHandled(): bool
62
    {
NEW
63
        return $this->errorHandled;
×
64
    }
65

66
    protected function handleError($exception): void
67
    {
68
        $this->renderError($exception);
6✔
69
    }
70

71
    private function renderUserError($exception): void
72
    {
NEW
73
        if (!$this->isCli) {
×
NEW
74
            header('Contains-Error-Message: User');
×
75
        }
76
        $this->renderError($exception, 'user_exception');
×
77
    }
78

79
    protected function renderError(OuzoExceptionData $exceptionData, $viewName = 'exception'): void
80
    {
81
        try {
82
            ExceptionLogger::newInstance($exceptionData)->log();
6✔
83
            $this->errorRenderer->render($exceptionData, $viewName);
6✔
84
            $this->errorHandled = true;
6✔
85
        } catch (Exception $e) {
×
86
            echo "Framework critical error. Exception thrown in exception handler.<br>\n";
×
87
            ExceptionLogger::forException($e)->log();
×
88
            if (Config::getValue('debug')) {
×
89
                echo "<hr>\n";
×
90
                echo "Message: " . $e->getMessage() . "<br>\n";
×
91
                echo "Trace: " . $e->getTraceAsString() . "<br>\n";
×
92
            }
93
        }
94
    }
95
}
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

© 2025 Coveralls, Inc