-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathErrorController.php
More file actions
96 lines (86 loc) · 3.06 KB
/
ErrorController.php
File metadata and controls
96 lines (86 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
declare(strict_types=1);
/**
* Passbolt ~ Open source password manager for teams
* Copyright (c) Passbolt SA (https://www.passbolt.com)
*
* Licensed under GNU Affero General Public License version 3 of the or any later version.
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Passbolt SA (https://www.passbolt.com)
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
* @link https://www.passbolt.com Passbolt(tm)
* @since 2.0.0
*/
namespace App\Controller;
use App\Error\Exception\ExceptionWithErrorsDetailInterface;
use App\Utility\UserAction;
use App\View\AjaxView;
use Cake\Controller\Controller;
use Cake\Event\EventInterface;
use Cake\Routing\Router;
use Cake\View\JsonView;
/**
* Error Handling Controller
*
* Controller used by ExceptionRenderer to render error responses.
*
* Note: We are not extending from AppController because it can cause problems when loading Authentication component.
*
* @see: https://github.com/cakephp/cakephp/issues/17655
* @property \App\Controller\Component\UserComponent $User
* @property \App\Controller\Component\QueryStringComponent $QueryString
*/
class ErrorController extends Controller
{
/**
* @inheritDoc
*/
public function initialize(): void
{
parent::initialize();
$this->loadComponent('User');
$this->loadComponent('QueryString');
// Init user action.
UserAction::initFromRequest($this->User->getAccessControl(), $this->request);
}
/**
* @inheritDoc
*/
public function viewClasses(): array
{
return [JsonView::class, AjaxView::class];
}
/**
* @inheritDoc
*/
public function beforeRender(EventInterface $event)
{
// Required to support automatic view switching for 'ajax', which was supported by deprecated RequestHandlerComponent
if ($this->request->is('ajax')) {
$this->viewBuilder()->setClassName('Ajax');
}
if ($this->request->is('json')) {
// If the body is a that exposes the getErrors functionality
// for example ValidationRulesException
$error = $this->viewBuilder()->getVar('error');
$body = '';
if ($error instanceof ExceptionWithErrorsDetailInterface) {
$body = $error->getErrors();
}
$header = [
'id' => UserAction::getInstance()->getUserActionId(),
'status' => 'error',
'servertime' => time(),
'action' => UserAction::getInstance()->getActionId(),
'message' => $this->viewBuilder()->getVar('message'),
'url' => Router::url(),
'code' => $this->viewBuilder()->getVar('code'),
];
$this->set(compact('header', 'body'));
$this->viewBuilder()->setOption('serialize', ['header', 'body',]);
}
$this->viewBuilder()->setTemplatePath('Error');
}
}