/jserror: Refactor to handle errors better

This commit is contained in:
Richard Hansen 2021-10-07 19:46:07 -04:00
parent 788eb86d84
commit e909072776

View file

@ -14,18 +14,19 @@ exports.expressCreateServer = (hookName, args, cb) => {
}); });
}); });
const parseJserrorForm = async (req) => await new Promise((resolve, reject) => {
const form = new formidable.IncomingForm();
form.on('error', (err) => reject(err));
form.parse(req, (err, fields) => err != null ? reject(err) : resolve(fields.errorInfo));
});
// The Etherpad client side sends information about client side javscript errors // The Etherpad client side sends information about client side javscript errors
args.app.post('/jserror', (req, res) => { args.app.post('/jserror', (req, res, next) => {
new formidable.IncomingForm().parse(req, (err, fields, files) => { (async () => {
let data; const data = JSON.parse(await parseJserrorForm(req));
try {
data = JSON.parse(fields.errorInfo);
} catch (e) {
return res.end();
}
clientLogger.warn(`${data.msg} --`, data); clientLogger.warn(`${data.msg} --`, data);
res.end('OK'); res.end('OK');
}); })().catch((err) => next(err || new Error(err)));
}); });
// Provide a possibility to query the latest available API version // Provide a possibility to query the latest available API version