fix: [error handling] better error handling for bookmarks, fixes #188

- show why something failed
- actually fail if a field is missing for bookmarks
pull/196/head
iglocska 2024-11-28 17:47:43 +01:00
parent d799214a41
commit cce4115418
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
4 changed files with 27 additions and 7 deletions

View File

@ -1365,7 +1365,7 @@ class CRUDComponent extends Component
if (!empty($additionalData['redirect'])) { // If a redirection occurs, we need to make sure the flash message gets displayed
$this->Controller->Flash->error($message);
}
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, $action, $data, $message, !is_null($errors) ? $errors : $data->getErrors());
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, $action, $data, $message, $errors);
} else {
$this->Controller->Flash->error($message);
$this->Controller->redirect($this->Controller->referer());

View File

@ -440,10 +440,14 @@ class RestResponseComponent extends Component
return $this->viewData($response);
}
public function ajaxFailResponse($ObjectAlias, $action, $entity, $message, $errors = [], $description = '')
public function ajaxFailResponse($ObjectAlias, $action, $entity = null, $message, $errors = [], $description = '')
{
$action = $this->__dissectAdminRouting($action);
$entity = is_array($entity) ? $entity : $entity->toArray();
if (empty($entity)) {
$entity = [];
} else {
$entity = is_array($entity) ? $entity : $entity->toArray();
}
$response = [
'success' => false,
'message' => $message,

View File

@ -243,9 +243,10 @@ class UserSettingsController extends AppController
public function saveMyBookmark()
{
if (!$this->request->is('get')) {
$result = $this->UserSettings->saveBookmark($this->ACL->getUser(), $this->request->getData());
$errors = null;
$result = $this->UserSettings->saveBookmark($this->ACL->getUser(), $this->request->getData(), $errors);
$success = !empty($result);
$message = $success ? __('Bookmark saved') : __('Could not save bookmark');
$message = $success ? __('Bookmark saved') : ($errors ?? __('Could not save bookmark'));
$this->CRUD->setResponseForController('saveBookmark', $success, $message, $result);
$responsePayload = $this->CRUD->getResponsePayload();
if (!empty($responsePayload)) {

View File

@ -95,9 +95,24 @@ class UserSettingsTable extends AppTable
return $savedData;
}
public function saveBookmark($user, $data)
public function saveBookmark($user, $data, &$message = null)
{
$setting = $this->getSettingByName($user, $this->BOOKMARK_SETTING_NAME);
$fieldsToCheck = [
'bookmark_label' => __('Label'),
'bookmark_name' => __('Name'),
'bookmark_url' => __('URL')
];
foreach ($fieldsToCheck as $field => $fieldName) {
if (empty($data[$field])) {
$message = __('Please fill in all fields, {0} missing.', $fieldName);
return null;
}
}
if (empty($data['bookmark_label']) || empty($data['bookmark_name']) || empty($data['bookmark_url'])) {
return null;
}
$bookmarkData = [
'label' => $data['bookmark_label'],
'name' => $data['bookmark_name'],
@ -107,7 +122,7 @@ class UserSettingsTable extends AppTable
if (!empty($restricted_domains)) {
$restricted_domains = explode(',', $restricted_domains);
$parsed = parse_url($bookmarkData['url']);
if (!in_array($parsed['host'], $restricted_domains)) {
if (!empty($parsed['host']) && !in_array($parsed['host'], $restricted_domains)) {
return null;
}
}