Completed
Push — master ( f16418...5ba7f3 )
by Julito
10:39
created

ExceptionController::errorAction()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 18
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 32
rs 9.0444
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Controller;
6
7
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8
use Symfony\Component\ErrorHandler\Exception\FlattenException;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpKernel\Exception\HttpException;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
class ExceptionController extends AbstractController
14
{
15
    public function showAction(FlattenException $exception)
16
    {
17
        if ('dev' === $this->getParameter('app_env')) {
18
            throw new HttpException($exception->getCode(), $exception->getMessage());
19
        }
20
21
        $showException = true;
22
        $name = $showException ? 'exception' : 'error';
23
        $code = $exception->getCode();
24
        $format = 'html';
25
        $loader = $this->container->get('twig')->getLoader();
26
        // when not in debug, try to find a template for the specific HTTP status code and format
27
        if (!$showException) {
28
            $template = sprintf('@ChamiloTheme/Exception/%s%s.%s.twig', $name, $code, $format);
29
            if ($loader->exists($template)) {
30
                return $template;
31
            }
32
        }
33
34
        // try to find a template for the given format
35
        $template = sprintf('@ChamiloTheme/Exception/%s.%s.twig', $name, $format);
36
        if ($loader->exists($template)) {
37
            return $template;
38
        }
39
40
        // default to a generic HTML exception
41
        //$request->setRequestFormat('html');
42
        $template = sprintf('@ChamiloTheme/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
43
44
        return $this->render($template, ['exception' => $exception]);
45
    }
46
47
    /**
48
     * @Route("/error")
49
     */
50
    public function errorAction(Request $request)
51
    {
52
        $message = $request->getSession()->get('error_message', '');
53
        $exception = new FlattenException();
54
        $exception->setCode(500);
55
56
        $exception->setMessage($message);
57
58
        $showException = true;
59
        $name = $showException ? 'exception' : 'error';
60
        $code = $exception->getCode();
61
        $format = 'html';
62
        $loader = $this->container->get('twig')->getLoader();
63
        // when not in debug, try to find a template for the specific HTTP status code and format
64
        if (!$showException) {
65
            $template = sprintf('@ChamiloTheme/Exception/%s%s.%s.twig', $name, $code, $format);
66
            if ($loader->exists($template)) {
67
                return $template;
68
            }
69
        }
70
71
        // try to find a template for the given format
72
        $template = sprintf('@ChamiloTheme/Exception/%s.%s.twig', $name, $format);
73
        if ($loader->exists($template)) {
74
            return $template;
75
        }
76
77
        // default to a generic HTML exception
78
        //$request->setRequestFormat('html');
79
        $template = sprintf('@ChamiloTheme/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
80
81
        return $this->render($template, ['exception' => $exception]);
82
    }
83
}
84