A bit more error handling.

Seeing if we can consistently inform the frontend about request errors.
Also, trying to outright crash with slightly less frequency.
This commit is contained in:
Sage Vaillancourt 2022-08-26 18:50:02 -04:00
parent 61b5da846a
commit cf21c56231
1 changed files with 16 additions and 16 deletions

32
app.js
View File

@ -24,12 +24,13 @@ const buildCluster = (clusterConfig, connect = true) => {
const kafka = new Kafka(kafkaConfig)
const admin = kafka.admin()
admin.connect().catch(console.error)
return c({
const cluster = {
kafka,
admin,
config: clusterConfig
})
}
admin.connect().catch(e => console.error(cluster.error = e.toString()))
return cluster
}
const clusters =
@ -50,15 +51,19 @@ app.use(express.static(path.join(__dirname, 'public')));
const router = express.Router()
process.on('unhandledRejection', console.error)
/* GET topics listing. */
router.get('/topics/:cluster', async (req, res, _next) => {
const legalName = topicName => !topicName.startsWith("__")
const topicList = await clusters[req.params.cluster]?.admin.listTopics()
if (topicList) {
try {
const topicList = (await clusters[req.params.cluster]?.admin.listTopics() || [])
res.send(topicList.filter(legalName))
} else {
res.send([])
//res.status(400).send({error: 'Invalid cluster name'})
} catch (e) {
res.status(502).send({
error: `Could not connect to cluster '${req.params.cluster}'`,
errorDetails: e.toString()
})
}
})
@ -75,6 +80,7 @@ const getClusterData = () =>
}))
router.get('/clusters', async (req, res, _next) => {
console.log('/clusters')
res.send(getClusterData())
})
@ -88,7 +94,6 @@ router.post('/clusters', async (req, res, _next) => {
})
router.post('/clusters/delete', async (req, res, _next) => {
console.log('/clusters/delete post body', req.body)
const clusterName = req.body.clusterName
// TODO: Disconnect
delete clusters[clusterName]
@ -98,10 +103,7 @@ router.post('/clusters/delete', async (req, res, _next) => {
})
router.put('/clusters', async (req, res, _next) => {
console.log('/clusters put body', req.body)
const hasPlaceholderPassword = req.body.sasl.password === passwordPlaceholder
console.log('Has placeholder password:', hasPlaceholderPassword)
const clusterName = req.body.clusterName
if (hasPlaceholderPassword) {
req.body.password = config.clusters[clusterName].password
@ -167,14 +169,12 @@ wsServer.on('connection', socket => {
})
socket.on('message', async message => {
socket.send('Loading...')
console.log('socket message')
//socket.send('Loading...')
message = JSON.parse(message)
const currentMode = message.mode === 'realTime' ? realTimeSearch : search
console.log('CLUSTERS before run', clusters)
console.log('message.cluster', message.cluster)
const cluster = clusters[message.cluster]
console.log('clusters[message.cluster]', cluster)
const run = async () => consumers.set(socket, await currentMode({
kafka: cluster.kafka,
searchCode: message.searchCode,