Fix #17620 - Fix callbacks of Functions.configGet if there is an error and better handle error messages

Ref: https://github.com/phpmyadmin/phpmyadmin/issues/17585#issuecomment-1193770253
Closes: #17585
Fixes: #17620

Signed-off-by: William Desportes <williamdes@wdes.fr>
This commit is contained in:
William Desportes 2022-08-15 11:57:17 +02:00
parent 22b5102cae
commit a9356e4d16
No known key found for this signature in database
GPG Key ID: 90A0EF1B8251A889
2 changed files with 29 additions and 14 deletions

View File

@ -64,6 +64,10 @@ var Console = {
Functions.configGet('Console', false, (data) => { Functions.configGet('Console', false, (data) => {
Console.config = data; Console.config = data;
Console.setupAfterInit(); Console.setupAfterInit();
}, () => {
Console.config = {};// Avoid null pointers in setupAfterInit()
// Fetching data failed, still perform the console init
Console.setupAfterInit();
}); });
}, },

View File

@ -4537,8 +4537,10 @@ Functions.getImage = function (image, alternate, attributes) {
* @param {object} value Configuration value. * @param {object} value Configuration value.
*/ */
Functions.configSet = function (key, value) { Functions.configSet = function (key, value) {
// Updating value in local storage.
var serialized = JSON.stringify(value); var serialized = JSON.stringify(value);
localStorage.setItem(key, serialized); localStorage.setItem(key, serialized);
$.ajax({ $.ajax({
url: 'index.php?route=/config/set', url: 'index.php?route=/config/set',
type: 'POST', type: 'POST',
@ -4550,15 +4552,12 @@ Functions.configSet = function (key, value) {
value: serialized, value: serialized,
}, },
success: function (data) { success: function (data) {
// Updating value in local storage. if (data.success !== true) {
if (! data.success) { // Try to find a message to display
if (data.error) { if (data.error || data.message || false) {
Functions.ajaxShowMessage(data.error); Functions.ajaxShowMessage(data.error || data.message);
} else {
Functions.ajaxShowMessage(data.message);
} }
} }
// Eventually, call callback.
} }
}); });
}; };
@ -4573,11 +4572,12 @@ Functions.configSet = function (key, value) {
* *
* @param {string} key Configuration key. * @param {string} key Configuration key.
* @param {boolean} cached Configuration type. * @param {boolean} cached Configuration type.
* @param {Function} successCallback The callback to call after the value is received * @param {Function} successCallback The callback to call after the value is successfully received
* @param {Function} failureCallback The callback to call when the value can not be received
* *
* @return {void} * @return {void}
*/ */
Functions.configGet = function (key, cached, successCallback) { Functions.configGet = function (key, cached, successCallback, failureCallback) {
var isCached = (typeof cached !== 'undefined') ? cached : true; var isCached = (typeof cached !== 'undefined') ? cached : true;
var value = localStorage.getItem(key); var value = localStorage.getItem(key);
if (isCached && value !== undefined && value !== null) { if (isCached && value !== undefined && value !== null) {
@ -4596,12 +4596,23 @@ Functions.configGet = function (key, cached, successCallback) {
key: key key: key
}, },
success: function (data) { success: function (data) {
// Updating value in local storage. if (data.success !== true) {
if (data.success) { // Try to find a message to display
localStorage.setItem(key, JSON.stringify(data.value)); if (data.error || data.message || false) {
} else { Functions.ajaxShowMessage(data.error || data.message);
Functions.ajaxShowMessage(data.message); }
// Call the callback if it is defined
if (typeof failureCallback === 'function') {
failureCallback();
}
// return here, exit non success mode
return;
} }
// Updating value in local storage.
localStorage.setItem(key, JSON.stringify(data.value));
// Call the callback if it is defined // Call the callback if it is defined
if (typeof successCallback === 'function') { if (typeof successCallback === 'function') {
// Feed it the value previously saved like on async mode // Feed it the value previously saved like on async mode