Merge pull request #2748 from chakflying/feat/improve-err-code

Feat: Use error message to detect and set status code
This commit is contained in:
Louis Lam 2023-02-22 01:01:53 +08:00 committed by GitHub
commit 42033c692f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 25 deletions

View file

@ -1,5 +1,5 @@
let express = require("express"); let express = require("express");
const { allowDevAllOrigin, allowAllOrigin, percentageToColor, filterAndJoin, send403 } = require("../util-server"); const { allowDevAllOrigin, allowAllOrigin, percentageToColor, filterAndJoin, sendHttpError } = require("../util-server");
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const apicache = require("../modules/apicache"); const apicache = require("../modules/apicache");
const Monitor = require("../model/monitor"); const Monitor = require("../model/monitor");
@ -175,7 +175,7 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response
response.type("image/svg+xml"); response.type("image/svg+xml");
response.send(svg); response.send(svg);
} catch (error) { } catch (error) {
send403(response, error.message); sendHttpError(response, error.message);
} }
}); });
@ -242,7 +242,7 @@ router.get("/api/badge/:id/uptime/:duration?", cache("5 minutes"), async (reques
response.type("image/svg+xml"); response.type("image/svg+xml");
response.send(svg); response.send(svg);
} catch (error) { } catch (error) {
send403(response, error.message); sendHttpError(response, error.message);
} }
}); });
@ -303,7 +303,7 @@ router.get("/api/badge/:id/ping/:duration?", cache("5 minutes"), async (request,
response.type("image/svg+xml"); response.type("image/svg+xml");
response.send(svg); response.send(svg);
} catch (error) { } catch (error) {
send403(response, error.message); sendHttpError(response, error.message);
} }
}); });
@ -373,7 +373,7 @@ router.get("/api/badge/:id/avg-response/:duration?", cache("5 minutes"), async (
response.type("image/svg+xml"); response.type("image/svg+xml");
response.send(svg); response.send(svg);
} catch (error) { } catch (error) {
send403(response, error.message); sendHttpError(response, error.message);
} }
}); });
@ -464,7 +464,7 @@ router.get("/api/badge/:id/cert-exp", cache("5 minutes"), async (request, respon
response.type("image/svg+xml"); response.type("image/svg+xml");
response.send(svg); response.send(svg);
} catch (error) { } catch (error) {
send403(response, error.message); sendHttpError(response, error.message);
} }
}); });
@ -536,7 +536,7 @@ router.get("/api/badge/:id/response", cache("5 minutes"), async (request, respon
response.type("image/svg+xml"); response.type("image/svg+xml");
response.send(svg); response.send(svg);
} catch (error) { } catch (error) {
send403(response, error.message); sendHttpError(response, error.message);
} }
}); });

View file

@ -2,7 +2,7 @@ let express = require("express");
const apicache = require("../modules/apicache"); const apicache = require("../modules/apicache");
const { UptimeKumaServer } = require("../uptime-kuma-server"); const { UptimeKumaServer } = require("../uptime-kuma-server");
const StatusPage = require("../model/status_page"); const StatusPage = require("../model/status_page");
const { allowDevAllOrigin, send403 } = require("../util-server"); const { allowDevAllOrigin, sendHttpError } = require("../util-server");
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const Monitor = require("../model/monitor"); const Monitor = require("../model/monitor");
@ -44,10 +44,7 @@ router.get("/api/status-page/:slug", cache("5 minutes"), async (request, respons
let statusPageData = await StatusPage.getStatusPageData(statusPage); let statusPageData = await StatusPage.getStatusPageData(statusPage);
if (!statusPageData) { if (!statusPageData) {
response.statusCode = 404; sendHttpError(response, "Not Found");
response.json({
msg: "Not Found"
});
return; return;
} }
@ -55,7 +52,7 @@ router.get("/api/status-page/:slug", cache("5 minutes"), async (request, respons
response.json(statusPageData); response.json(statusPageData);
} catch (error) { } catch (error) {
send403(response, error.message); sendHttpError(response, error.message);
} }
}); });
@ -103,7 +100,7 @@ router.get("/api/status-page/heartbeat/:slug", cache("1 minutes"), async (reques
}); });
} catch (error) { } catch (error) {
send403(response, error.message); sendHttpError(response, error.message);
} }
}); });
@ -119,10 +116,7 @@ router.get("/api/status-page/:slug/manifest.json", cache("1440 minutes"), async
]); ]);
if (!statusPage) { if (!statusPage) {
response.statusCode = 404; sendHttpError(response, "Not Found");
response.json({
msg: "Not Found"
});
return; return;
} }
@ -141,7 +135,7 @@ router.get("/api/status-page/:slug/manifest.json", cache("1440 minutes"), async
}); });
} catch (error) { } catch (error) {
send403(response, error.message); sendHttpError(response, error.message);
} }
}); });

View file

@ -739,15 +739,27 @@ exports.filterAndJoin = (parts, connector = "") => {
}; };
/** /**
* Send a 403 response * Send an Error response
* @param {Object} res Express response object * @param {Object} res Express response object
* @param {string} [msg=""] Message to send * @param {string} [msg=""] Message to send
*/ */
module.exports.send403 = (res, msg = "") => { module.exports.sendHttpError = (res, msg = "") => {
res.status(403).json({ if (msg.includes("SQLITE_BUSY") || msg.includes("SQLITE_LOCKED")) {
"status": "fail", res.status(503).json({
"msg": msg, "status": "fail",
}); "msg": msg,
});
} else if (msg.toLowerCase().includes("not found")) {
res.status(404).json({
"status": "fail",
"msg": msg,
});
} else {
res.status(403).json({
"status": "fail",
"msg": msg,
});
}
}; };
function timeObjectConvertTimezone(obj, timezone, timeObjectToUTC = true) { function timeObjectConvertTimezone(obj, timezone, timeObjectToUTC = true) {