diff --git a/src/deployments.ts b/src/deployments.ts index 1c52706..e2010d6 100644 --- a/src/deployments.ts +++ b/src/deployments.ts @@ -51,14 +51,14 @@ export class RegHelper { const records = await this.registry.resolveNames([name]); for (const r of records) { // r can be null - if (!r) { + if (r) { this.cache.set(r.id, r); if (Array.isArray(r.names)) { for (const n of r.names) { this.cache.set(n, r); } } - } + } } return records[0]; } diff --git a/src/main.ts b/src/main.ts index d1d3d4a..f635370 100644 --- a/src/main.ts +++ b/src/main.ts @@ -55,40 +55,60 @@ const adjustFromState = (regStatus: any[]) => { } app.get('/', async (_req, res) => { - const reg = new RegHelper(); - const regStatus = adjustFromState(await reg.deploymentRequestStatus()); - res.send(regStatus); + try { + const reg = new RegHelper(); + const regStatus = adjustFromState(await reg.deploymentRequestStatus()); + res.send(regStatus); + } catch (e) { + console.error(e); + res.sendStatus(500); + } }); app.get('/:id', async (req, res) => { - const reg = new RegHelper(); - const records = adjustFromState(await reg.deploymentRequestStatus(req.params.id)); - if (records.length) { - res.send(records[0]); - } else { - res.sendStatus(410); + try { + const reg = new RegHelper(); + const records = adjustFromState(await reg.deploymentRequestStatus(req.params.id)); + if (records.length) { + res.send(records[0]); + } else { + res.sendStatus(410); + } + } catch (e) { + console.error(e); + res.sendStatus(500); } }); app.get('/:id/log', async (req, res) => { - const logFile = logForRequest(req.params.id); - if (!logFile) { - res.sendStatus(410); - return; - } + try { + const logFile = logForRequest(req.params.id); + if (!logFile) { + res.sendStatus(410); + return; + } - res.send(readFileSync(logFile)); + res.send(readFileSync(logFile)); + } catch (e) { + console.error(e); + res.sendStatus(500); + } }); // deprecated app.get('/log/:id', async (req, res) => { - const logFile = logForRequest(req.params.id); - if (!logFile) { - res.sendStatus(410); - return; - } + try { + const logFile = logForRequest(req.params.id); + if (!logFile) { + res.sendStatus(410); + return; + } - res.send(readFileSync(logFile)); + res.send(readFileSync(logFile)); + } catch (e) { + console.error(e); + res.sendStatus(500); + } }); app.listen(Config.LISTEN_PORT, Config.LISTEN_ADDR, () => {