ExceptionRenderer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 26
c 4
b 0
f 0
dl 0
loc 69
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A _getController() 0 8 3
A _outputMessageSafe() 0 22 2
A getHttpCode() 0 7 2
A _template() 0 8 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2018 ChannelWeb Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
16
namespace BEdita\WebTools\Error;
17
18
use BEdita\SDK\BEditaClientException;
19
use Cake\Controller\Controller;
20
use Cake\Error\Renderer\WebExceptionRenderer;
21
use Cake\Http\Response;
22
use Cake\Log\LogTrait;
23
use Exception;
24
use Throwable;
25
26
/**
27
 * Custom exception renderer class.
28
 * Handle with templates 500 and 400 (for status code < 500).
29
 */
30
class ExceptionRenderer extends WebExceptionRenderer
31
{
32
    use LogTrait;
33
34
    /**
35
     * @inheritDoc
36
     */
37
    protected function _template(Throwable $exception, string $method, int $code): string
38
    {
39
        $template = 'error500';
40
        if ($code < 500) {
41
            $template = 'error400';
42
        }
43
44
        return $this->template = $template;
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    protected function _getController(): Controller
51
    {
52
        $controller = parent::_getController();
53
        if ($this->request && $this->request->is('json')) {
54
            $controller->viewBuilder()->setClassName('Json');
55
        }
56
57
        return $controller;
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     *
63
     * Handle BEditaClientException.
64
     */
65
    protected function getHttpCode(Throwable $exception): int
66
    {
67
        if ($exception instanceof BEditaClientException) {
68
            return $exception->getCode();
69
        }
70
71
        return parent::getHttpCode($exception);
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    protected function _outputMessageSafe(string $template): Response
78
    {
79
        $builder = $this->controller->viewBuilder();
80
        $builder->setLayoutPath('')
81
            ->setTemplatePath('Error');
82
83
        // first try to use AppView class. Fallback to internal template on failure
84
        try {
85
            $view = $this->controller->createView();
86
            $body = $view->render($template, 'error');
87
        } catch (Exception $e) {
88
            // first log the new exception to trace the new error too.
89
            $this->log($e->getMessage());
90
91
            $helpers = ['Form', 'Html'];
92
            $builder->setHelpers($helpers, false);
93
94
            $view = $this->controller->createView('View');
95
            $body = $view->render('BEdita/WebTools.' . $template, 'BEdita/WebTools.error');
96
        }
97
98
        return $this->controller->getResponse()->withStringBody($body)->withType('html');
99
    }
100
}
101