Add try catch for ping

This commit is contained in:
shamoon 2023-05-20 09:48:18 -07:00
parent cead87dcd0
commit f1a33f10ed

View File

@ -15,21 +15,28 @@ export default async function handler(req, res) {
}); });
} }
let startTime = performance.now(); try {
let [status] = await httpProxy(pingURL, { let startTime = performance.now();
method: "HEAD" let [status] = await httpProxy(pingURL, {
}); method: "HEAD"
let endTime = performance.now(); });
let endTime = performance.now();
if (status > 403) { if (status > 403) {
// try one more time as a GET in case HEAD is rejected for whatever reason // try one more time as a GET in case HEAD is rejected for whatever reason
startTime = performance.now(); startTime = performance.now();
[status] = await httpProxy(pingURL); [status] = await httpProxy(pingURL);
endTime = performance.now(); endTime = performance.now();
}
return res.status(200).json({
status,
latency: endTime - startTime
});
} catch (e) {
logger.debug("Error attempting ping: %s", JSON.stringify(e));
return res.status(400).send({
error: 'Error attempting ping, see logs.',
});
} }
return res.status(200).json({
status,
latency: endTime - startTime
});
} }