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) => {
Console.config = data;
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.
*/
Functions.configSet = function (key, value) {
// Updating value in local storage.
var serialized = JSON.stringify(value);
localStorage.setItem(key, serialized);
$.ajax({
url: 'index.php?route=/config/set',
type: 'POST',
@ -4550,15 +4552,12 @@ Functions.configSet = function (key, value) {
value: serialized,
},
success: function (data) {
// Updating value in local storage.
if (! data.success) {
if (data.error) {
Functions.ajaxShowMessage(data.error);
} else {
Functions.ajaxShowMessage(data.message);
if (data.success !== true) {
// Try to find a message to display
if (data.error || data.message || false) {
Functions.ajaxShowMessage(data.error || data.message);
}
}
// Eventually, call callback.
}
});
};
@ -4573,11 +4572,12 @@ Functions.configSet = function (key, value) {
*
* @param {string} key Configuration key.
* @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}
*/
Functions.configGet = function (key, cached, successCallback) {
Functions.configGet = function (key, cached, successCallback, failureCallback) {
var isCached = (typeof cached !== 'undefined') ? cached : true;
var value = localStorage.getItem(key);
if (isCached && value !== undefined && value !== null) {
@ -4596,12 +4596,23 @@ Functions.configGet = function (key, cached, successCallback) {
key: key
},
success: function (data) {
// Updating value in local storage.
if (data.success) {
localStorage.setItem(key, JSON.stringify(data.value));
} else {
Functions.ajaxShowMessage(data.message);
if (data.success !== true) {
// Try to find a message to display
if (data.error || data.message || false) {
Functions.ajaxShowMessage(data.error || 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
if (typeof successCallback === 'function') {
// Feed it the value previously saved like on async mode