Error Handling Component
The Errors
component is responsible for error handling. You can retrieve its object by calling the bff::errors()
method.
Within a module/add-on, it is available through $this->errors->
.
Setting Errors
You can set errors during request processing by calling the corresponding method:
$this->errors->set('Error message');
There are several standard errors available:
// Operation is impossible
$this->errors->impossible();
// Access denied
$this->errors->accessDenied();
// Please refresh the page and try again
$this->errors->reloadPage();
To check if any errors have been set previously, you can do it like this:
$this->errors->no(); // the method will return true if there are no errors
404 Errors
You can return a 404 error by calling the following method:
$this->errors->error404();
In this case, the execution of the application is completely interrupted at this line, and the response is returned to the user. You can also return other HTTP errors:
$this->errors->errorHttp(403); // Access to the specified page is forbidden
In the case of an AJAX response, the set error will be passed in the response text. For example, in this situation:
$this->errors->set(_t('users', 'The password was specified incorrectly'));
$this->ajaxResponse(['retry'=>true]);
The following JSON message will be sent as the response:
{
"data": {
"retry": true
},
"errors": [
"The password was specified incorrectly"
]
}