Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Finder ajax error handling #39973

Merged
merged 4 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion administrator/components/com_finder/tmpl/indexer/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;

Text::script('COM_FINDER_INDEXER_MESSAGE_COMPLETE', true);
Text::script('COM_FINDER_INDEXER_MESSAGE_COMPLETE');
Text::script('COM_FINDER_AN_ERROR_HAS_OCCURRED');
Text::script('COM_FINDER_MESSAGE_RETURNED');
Text::script('JLIB_JS_AJAX_ERROR_OTHER');
Text::script('JLIB_JS_AJAX_ERROR_PARSE');

/** @var Joomla\CMS\WebAsset\WebAssetManager $wa */
$wa = $this->document->getWebAssetManager();
Expand Down
30 changes: 15 additions & 15 deletions build/media_source/com_finder/js/finder.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@

Joomla.request({
url: `${Joomla.getOptions('finder-search').url}&q=${target.value}`,
method: 'GET',
data: { q: target.value },
perform: true,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
onSuccess: (resp) => {
const response = JSON.parse(resp);
if (Object.prototype.toString.call(response.suggestions) === '[object Array]') {
target.awesomplete.list = response.suggestions;
}
},
onError: (xhr) => {
if (xhr.status > 0) {
Joomla.renderMessages(Joomla.ajaxErrorsMessages(xhr));
}
},
promise: true,
}).then((xhr) => {
let response;
try {
response = JSON.parse(xhr.responseText);
} catch (e) {
Joomla.renderMessages(Joomla.ajaxErrorsMessages(xhr, 'parsererror'));
return;
}

if (Object.prototype.toString.call(response.suggestions) === '[object Array]') {
target.awesomplete.list = response.suggestions;
}
}).catch((xhr) => {
Joomla.renderMessages(Joomla.ajaxErrorsMessages(xhr));
});
}
};
Expand Down
41 changes: 26 additions & 15 deletions build/media_source/com_finder/js/indexer.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,33 @@
return true;
};

const handleFailure = (xhr) => {
const handleFailure = (error) => {
const progressHeader = document.getElementById('finder-progress-header');
const progressMessage = document.getElementById('finder-progress-message');
let data;

let data = (typeof xhr === 'object' && xhr.responseText) ? xhr.responseText : null;
data = data ? JSON.parse(data) : null;
if (error instanceof Error) {
// Encode any html in the message
const div = document.createElement('div');
div.textContent = error.message;
data = div.innerHTML;

if (error instanceof SyntaxError) {
data = Joomla.Text._('JLIB_JS_AJAX_ERROR_PARSE').replace('%s', data);
}
} else if (typeof error === 'object' && error.responseText) {
data = error.responseText;
try {
data = JSON.parse(data);
} catch (e) {
data = Joomla.Text._('JLIB_JS_AJAX_ERROR_OTHER').replace('%s', error.status);
}
}

removeElement('progress');

const header = data ? data.header : Joomla.Text._('COM_FINDER_AN_ERROR_HAS_OCCURRED');
const message = data ? data.message : `${Joomla.Text._('COM_FINDER_MESSAGE_RETURNED')}<br>${data}`;
const header = data && data.header ? data.header : Joomla.Text._('COM_FINDER_AN_ERROR_HAS_OCCURRED');
const message = data && data.message ? data.message : `${Joomla.Text._('COM_FINDER_MESSAGE_RETURNED')}<br>${data}`;

if (progressHeader) {
progressHeader.innerText = header;
Expand All @@ -155,16 +171,11 @@
getRequest = (task) => {
Joomla.request({
url: `${path}&task=${task}${token}`,
method: 'GET',
data: '',
perform: true,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
onSuccess: (response) => {
handleResponse(JSON.parse(response));
},
onError: (xhr) => {
handleFailure(xhr);
},
promise: true,
}).then((xhr) => {
handleResponse(JSON.parse(xhr.responseText));
}).catch((error) => {
handleFailure(error);
});
};

Expand Down
3 changes: 3 additions & 0 deletions components/com_finder/tmpl/search/default_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
if ($this->params->get('show_autosuggest', 1)) {
$this->document->getWebAssetManager()->usePreset('awesomplete');
$this->document->addScriptOptions('finder-search', ['url' => Route::_('index.php?option=com_finder&task=suggestions.suggest&format=json&tmpl=component', false)]);

Text::script('JLIB_JS_AJAX_ERROR_OTHER');
Text::script('JLIB_JS_AJAX_ERROR_PARSE');
}

?>
Expand Down
5 changes: 4 additions & 1 deletion modules/mod_finder/tmpl/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
$output .= $input;
}

Text::script('MOD_FINDER_SEARCH_VALUE', true);
Text::script('MOD_FINDER_SEARCH_VALUE');

/** @var Joomla\CMS\WebAsset\WebAssetManager $wa */
$wa = $app->getDocument()->getWebAssetManager();
Expand All @@ -51,6 +51,9 @@
if ($params->get('show_autosuggest', 1)) {
$wa->usePreset('awesomplete');
$app->getDocument()->addScriptOptions('finder-search', ['url' => Route::_('index.php?option=com_finder&task=suggestions.suggest&format=json&tmpl=component', false)]);

Text::script('JLIB_JS_AJAX_ERROR_OTHER');
Text::script('JLIB_JS_AJAX_ERROR_PARSE');
}

$wa->useScript('com_finder.finder');
Expand Down