feat: better DataTables AJAX error

This commit is contained in:
Pig Fang 2018-03-18 10:49:49 +08:00
parent 924fedc116
commit a2ad367651
6 changed files with 35 additions and 5 deletions

View File

@ -26,7 +26,8 @@ module.exports = {
"showModal": false,
"showAjaxError": false,
"getQueryString": false,
"TexturePreview": false
"TexturePreview": false,
"handleDataTablesAjaxError": false
},
"parserOptions": {
"ecmaVersion": 2017

View File

@ -1132,7 +1132,7 @@ describe('tests for "common" module', () => {
const fetch = jest.fn()
.mockReturnValue(Promise.resolve({ errno: 0, msg: 'Recorded.' }));
$.fn.dataTable = { defaults: {} };
$.fn.dataTable = { defaults: {}, ext: { errMode: '' } };
window.document.cookie = '';
window.fetch = fetch;
window.blessing = {
@ -1155,4 +1155,24 @@ describe('tests for "common" module', () => {
await sendFeedback();
expect(fetch).toHaveBeenCalledTimes(1);
});
it('handle DataTables AJAX error', () => {
const { handleDataTablesAjaxError } = require(modulePath);
const showModal = jest.fn();
window.trans = jest.fn(t => t);
window.showModal = showModal;
$.fn.dataTable = { defaults: {}, ext: { errMode: '' } };
handleDataTablesAjaxError(undefined, undefined, '{}');
expect(showModal).not.toBeCalled();
handleDataTablesAjaxError(undefined, undefined, null, {
responseText: 'error'
});
expect(showModal).toBeCalledWith(
'error',
'general.fatalError',
'danger'
);
});
});

View File

@ -9,6 +9,14 @@ $.extend(true, $.fn.dataTable.defaults, {
serverSide: true
});
$.fn.dataTable.ext.errMode = 'none';
function handleDataTablesAjaxError(event, settings, json, xhr) {
if (json === null) {
showModal(xhr.responseText, trans('general.fatalError'), 'danger');
}
}
async function sendFeedback() {
if (document.cookie.replace(/(?:(?:^|.*;\s*)feedback_sent\s*=\s*([^;]*).*$)|^.*$/, '$1') !== '') {
return;
@ -39,5 +47,6 @@ async function sendFeedback() {
if (process.env.NODE_ENV === 'test') {
module.exports = {
sendFeedback,
handleDataTablesAjaxError,
};
}

View File

@ -13,7 +13,7 @@ function initPlayersTable() {
scrollY: ($('.content-wrapper').height() - $('.content-header').outerHeight()) * 0.7,
fnDrawCallback: () => $('[data-toggle="tooltip"]').tooltip(),
columnDefs: playersTableColumnDefs
});
}).on('xhr.dt', handleDataTablesAjaxError);
}
const playersTableColumnDefs = [

View File

@ -9,7 +9,7 @@ function initPluginsTable() {
ajax: url('admin/plugins/data'),
fnDrawCallback: () => $('[data-toggle="tooltip"]').tooltip(),
columnDefs: pluginsTableColumnDefs
});
}).on('xhr.dt', handleDataTablesAjaxError);
}
const pluginsTableColumnDefs = [

View File

@ -14,7 +14,7 @@ function initUsersTable() {
fnDrawCallback: () => $('[data-toggle="tooltip"]').tooltip(),
rowCallback: (row, data) => $(row).attr('id', `user-${data.uid}`),
columnDefs: usersTableColumnDefs
});
}).on('xhr.dt', handleDataTablesAjaxError);
}
const userPermissions = {