1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BEdita, API-first content management framework |
4
|
|
|
* Copyright 2018 ChannelWeb Srl, Chialab Srl |
5
|
|
|
* |
6
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace BEdita\WebTools\Error; |
15
|
|
|
|
16
|
|
|
use Cake\Error\ExceptionRenderer as CakeExceptionRenderer; |
17
|
|
|
use Cake\Log\LogTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Custom exception renderer class. |
21
|
|
|
* Handle with templates 500 and 400 (for status code < 500). |
22
|
|
|
*/ |
23
|
|
|
class ExceptionRenderer extends CakeExceptionRenderer |
24
|
|
|
{ |
25
|
|
|
use LogTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritDoc} |
29
|
|
|
*/ |
30
|
|
|
protected function _template(\Exception $exception, $method, $code) : string |
31
|
|
|
{ |
32
|
|
|
$exception = $this->_unwrap($exception); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$template = 'error500'; |
35
|
|
|
if ($code < 500) { |
36
|
|
|
$template = 'error400'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this->template = $template; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
|
|
protected function _outputMessageSafe($template) |
46
|
|
|
{ |
47
|
|
|
$builder = $this->controller->viewBuilder(); |
48
|
|
|
$builder->setLayoutPath('') |
49
|
|
|
->setTemplatePath('Error'); |
50
|
|
|
|
51
|
|
|
// first try to use AppView class. Fallback to internal template on failure |
52
|
|
|
try { |
53
|
|
|
$view = $this->controller->createView(); |
54
|
|
|
$this->controller->response->body($view->render($template, 'error')); |
|
|
|
|
55
|
|
|
} catch (\Exception $e) { |
56
|
|
|
// first log the new exception to trace the new error too. |
57
|
|
|
$this->log($e->getMessage()); |
58
|
|
|
|
59
|
|
|
$helpers = ['Form', 'Html']; |
60
|
|
|
$this->controller->helpers = $helpers; |
61
|
|
|
$builder->setHelpers($helpers, false); |
62
|
|
|
|
63
|
|
|
$view = $this->controller->createView('View'); |
64
|
|
|
$this->controller->response->body($view->render('BEdita/WebTools.' . $template, 'BEdita/WebTools.error')); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->controller->response->type('html'); |
|
|
|
|
68
|
|
|
|
69
|
|
|
return $this->controller->response; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|