fix: surface setMucData errors to callers via Promise
Addresses PERFORMANCE-FINDINGS #5: - setMucData now returns a Promise that rejects on DB error, missing config row, or HTTP failure instead of swallowing return true/false. - Add rowCount guard to avoid throwing on missing config row. - PUT /groupchats/:target awaits setMucData and returns 500 on failure. - Remove dead call to undefined logging() function. Part-of: <http://gitlab.vnc.biz/uxf/prosody-muc-rest/-/merge_requests/3>
This commit is contained in:
+26
-17
@@ -43,12 +43,21 @@ async function doesMucExist(jid) {
|
||||
|
||||
|
||||
function setMucData(actor, muc, data) {
|
||||
var query = "select value::jsonb as config from prosody where prosody.user=$1 and prosody.host=$2 and prosody.key=$3;"
|
||||
var queryparams = [];
|
||||
queryparams = muc.split("@");
|
||||
queryparams[2] = "_data";
|
||||
dbpool.query(query, queryparams, function (derr, dres) {
|
||||
if (derr == null) {
|
||||
return new Promise((resolve, reject) => {
|
||||
var query = "select value::jsonb as config from prosody where prosody.user=$1 and prosody.host=$2 and prosody.key=$3;"
|
||||
var queryparams = [];
|
||||
queryparams = muc.split("@");
|
||||
queryparams[2] = "_data";
|
||||
dbpool.query(query, queryparams, function (derr, dres) {
|
||||
if (derr != null) {
|
||||
console.log("[setMucData] db error for actor:", actor, "target:", muc, derr);
|
||||
return reject(derr);
|
||||
}
|
||||
if (dres.rowCount === 0) {
|
||||
let err = new Error("setMucData: no config row for " + muc);
|
||||
console.log("[setMucData]", err.message);
|
||||
return reject(err);
|
||||
}
|
||||
console.log("got data: ", dres.rows[0]);
|
||||
let e2e = "0";
|
||||
if (dres.rows[0].config.e2e) {
|
||||
@@ -144,20 +153,15 @@ function setMucData(actor, muc, data) {
|
||||
function (error, response, body) {
|
||||
if (!error && (response.statusCode == 200 || response.statusCode == 201)) {
|
||||
console.log(moment().format("LTS") + " [xmpp-rest succes for] ", actor);
|
||||
return true;
|
||||
return resolve(true);
|
||||
} else {
|
||||
console.log(moment().format("LTS") + " [xmpp-rest error] ", body, error);
|
||||
return false;
|
||||
return reject(new Error("setMucData: xmpp-rest failed for " + actor + " -> " + muc + " status=" + (response && response.statusCode) + " error=" + error));
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
} else {
|
||||
logging("setMucData", "called for actor:" + actor + " with target: " + muc + " created error: " + derr);
|
||||
return derr;
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
let dbPoolOpts = {
|
||||
@@ -308,7 +312,7 @@ router.put('/groupchats/:target', async function (req, res) {
|
||||
var query = "select prosody.value as actor from prosody where prosody.user=$1 and prosody.host=$2 and prosody.key=$3";
|
||||
// query += "union select split_part(split_part(prosody.value, $6, 1), $5, 2) as actor from prosody where prosody.user=$1 and prosody.host=$2 and prosody.key=$3";
|
||||
console.log("creating xmpp data from: ", req.body.data);
|
||||
dbpool.query(query, queryparams, function (derr, dres) {
|
||||
dbpool.query(query, queryparams, async function (derr, dres) {
|
||||
if (derr == null) {
|
||||
if (dres.rowCount > 0) {
|
||||
let nAct = {};
|
||||
@@ -331,8 +335,13 @@ router.put('/groupchats/:target', async function (req, res) {
|
||||
console.log("actor1: ", Object.keys(nAct));
|
||||
|
||||
|
||||
setMucData(actor, req.params.target, req.body.data);
|
||||
res.status(200).json(null);
|
||||
try {
|
||||
await setMucData(actor, req.params.target, req.body.data);
|
||||
res.status(200).json(null);
|
||||
} catch (e) {
|
||||
console.log("[setMucData] failed:", e.message);
|
||||
res.status(500).json({message: "failed to update muc data", error: e.message});
|
||||
}
|
||||
} else {
|
||||
res.status(410).json({err: "target does not exist - probably deleted"});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user