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