Error   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A errorHandler() 0 5 2
B exceptionHandler() 0 41 6
1
<?php
2
3
namespace Core;
4
5
/**
6
 * error and exception handler
7
 * PHP version 7
8
 */
9
class Error
10
{
11
    /**
12
     * Error handler. Convert all errors to exceptions by throwing an errorException
13
     *
14
     * @param int $level Error level
15
     * @param string $message Error message
16
     * @param string $file Filename the error was raised in
17
     * @param int $line Line number in the file
18
     *
19
     * @return void
20
     *
21
     * @throws \ErrorException to transform all errors into exceptions if the error reporting php configuration is set
22
     *
23
     */
24
    public static function errorHandler($level, $message, $file, $line): void
25
    {
26
        if (error_reporting() !== 0) {
27
            //to keep the @ operator working
28
            throw new \ErrorException($message, 0, $level, $file, $line);
29
        }
30
    }
31
32
    /**
33
     * Exception handler. Will show a custom error page when the exception is called.
34
     * Also checks if json error and treats it as json response
35
     * @param \Exception $exception The exception
36
     *
37
     * @return void
38
     */
39
    public static function exceptionHandler($exception): void
40
    {
41
        $code = $exception->getCode();
42
43
        //If we have a json exception thrown, we return a json error and stop
44
        if (get_class($exception) === 'Core\JsonException') {
45
            $code = 400; //sending back a bad request error
46
            if (Config::DEV_ENVIRONMENT) { //If we are in dev and using ajax, we want to see the error for debugging
47
                $code = 200;
48
            }
49
            http_response_code($code);
50
            header('Content-Type: application/json');
51
            die(json_encode(['error' => $exception->getMessage()]));
52
        }
53
54
        //code is 404 (not found) or 500 (general error)
55
        if ($code != 404) {
56
            $code = 500;
57
        }
58
        $viewData = [];
59
        //always set the message to be sent
60
        $viewData['exceptionMessage'] = $exception->getMessage();
61
62
        http_response_code($code);
63
64
        //Constructing the error message to send to twig
65
        if (Config::DEV_ENVIRONMENT) {
66
            $viewData['showErrors'] = true; //sending the config option down to twig
67
            $viewData['classException'] = get_class($exception);
68
            $viewData['stackTrace'] = $exception->getTraceAsString();
69
            $viewData['thrownIn'] = $exception->getFile() . " On line " . $exception->getLine();
70
        }
71
72
        $container = new Container();
73
74
        //Making sure that the twig template renders correctly.
75
        try {
76
            $twig = $container->getTemplate();
77
            $twig->display('ErrorPages/' . $code . '.twig', $viewData);
78
        } catch (\Exception $e) {
79
            echo 'Twig Error : ' . $e->getMessage();
80
        }
81
82
83
    }
84
}