Improve adminsetting (#6412)

- added a try/catch block around the fsp.writeFile
- replaced all the console.log with logger
This commit is contained in:
Helder Sepulveda 2024-05-27 15:35:20 -04:00 committed by GitHub
parent 79aa3bb27c
commit 5e78417f39
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,6 +3,7 @@
import {PadQueryResult, PadSearchQuery} from "../../types/PadSearchQuery"; import {PadQueryResult, PadSearchQuery} from "../../types/PadSearchQuery";
import {PadType} from "../../types/PadType"; import {PadType} from "../../types/PadType";
import log4js from 'log4js';
const eejs = require('../../eejs'); const eejs = require('../../eejs');
const fsp = require('fs').promises; const fsp = require('fs').promises;
@ -15,6 +16,7 @@ const api = require('../../db/API');
const queryPadLimit = 12; const queryPadLimit = 12;
const logger = log4js.getLogger('adminSettings');
exports.socketio = (hookName: string, {io}: any) => { exports.socketio = (hookName: string, {io}: any) => {
@ -28,7 +30,7 @@ exports.socketio = (hookName: string, {io}: any) => {
try { try {
data = await fsp.readFile(settings.settingsFilename, 'utf8'); data = await fsp.readFile(settings.settingsFilename, 'utf8');
} catch (err) { } catch (err) {
return console.log(err); return logger.error(`Error loading settings: ${err}`);
} }
// if showSettingsInAdminPage is set to false, then return NOT_ALLOWED in the result // if showSettingsInAdminPage is set to false, then return NOT_ALLOWED in the result
if (settings.showSettingsInAdminPage === false) { if (settings.showSettingsInAdminPage === false) {
@ -39,8 +41,12 @@ exports.socketio = (hookName: string, {io}: any) => {
}); });
socket.on('saveSettings', async (newSettings: string) => { socket.on('saveSettings', async (newSettings: string) => {
console.log('Admin request to save settings through a socket on /admin/settings'); logger.info('Admin request to save settings through a socket on /admin/settings');
await fsp.writeFile(settings.settingsFilename, newSettings); try {
await fsp.writeFile(settings.settingsFilename, newSettings);
} catch (err) {
logger.error(`Error saving settings: ${err}`);
}
socket.emit('saveprogress', 'saved'); socket.emit('saveprogress', 'saved');
}); });
@ -210,6 +216,7 @@ exports.socketio = (hookName: string, {io}: any) => {
socket.on('deletePad', async (padId: string) => { socket.on('deletePad', async (padId: string) => {
const padExists = await padManager.doesPadExists(padId); const padExists = await padManager.doesPadExists(padId);
if (padExists) { if (padExists) {
logger.info(`Deleting pad: ${padId}`);
const pad = await padManager.getPad(padId); const pad = await padManager.getPad(padId);
await pad.remove(); await pad.remove();
socket.emit('results:deletePad', padId); socket.emit('results:deletePad', padId);
@ -217,7 +224,7 @@ exports.socketio = (hookName: string, {io}: any) => {
}) })
socket.on('restartServer', async () => { socket.on('restartServer', async () => {
console.log('Admin request to restart server through a socket on /admin/settings'); logger.info('Admin request to restart server through a socket on /admin/settings');
settings.reloadSettings(); settings.reloadSettings();
await plugins.update(); await plugins.update();
await hooks.aCallAll('loadSettings', {settings}); await hooks.aCallAll('loadSettings', {settings});
@ -230,4 +237,3 @@ exports.socketio = (hookName: string, {io}: any) => {
const searchPad = async (query: PadSearchQuery) => { const searchPad = async (query: PadSearchQuery) => {
} }