From 4ab944b13c73abd695f2c11b2df37709b0c139b1 Mon Sep 17 00:00:00 2001 From: Ben Grant Date: Sat, 20 Aug 2022 21:52:09 +1000 Subject: [PATCH] Always send JSON responses --- crabfit-backend/package.json | 3 ++- crabfit-backend/routes/createEvent.js | 2 +- crabfit-backend/routes/createPerson.js | 8 ++++---- crabfit-backend/routes/getEvent.js | 4 ++-- crabfit-backend/routes/getPeople.js | 2 +- crabfit-backend/routes/login.js | 6 +++--- crabfit-backend/routes/taskCleanup.js | 2 +- crabfit-backend/routes/taskRemoveOrphans.js | 2 +- crabfit-backend/routes/updatePerson.js | 10 +++++----- 9 files changed, 20 insertions(+), 19 deletions(-) diff --git a/crabfit-backend/package.json b/crabfit-backend/package.json index 7d6e8c2..cf3f3fd 100644 --- a/crabfit-backend/package.json +++ b/crabfit-backend/package.json @@ -14,7 +14,8 @@ "build:dev": "NODE_ENV=development parcel build --no-cache", "dev": "rm -rf .parcel-cache dist && NODE_ENV=development nodemon --exec \"yarn build:dev && yarn start\" --watch routes --watch res --watch index.js", "build": "parcel build", - "start": "node ./dist/index.js" + "start": "node ./dist/index.js", + "lint": "eslint index.js ./routes" }, "dependencies": { "@google-cloud/datastore": "^7.0.0", diff --git a/crabfit-backend/routes/createEvent.js b/crabfit-backend/routes/createEvent.js index 707aa70..bf901f9 100644 --- a/crabfit-backend/routes/createEvent.js +++ b/crabfit-backend/routes/createEvent.js @@ -77,7 +77,7 @@ const createEvent = async (req, res) => { } } catch (e) { console.error(e) - res.sendStatus(400) + res.status(400).send({ error: 'An error occurred while creating the event' }) } } diff --git a/crabfit-backend/routes/createPerson.js b/crabfit-backend/routes/createPerson.js index 4a2622e..5a437d2 100644 --- a/crabfit-backend/routes/createPerson.js +++ b/crabfit-backend/routes/createPerson.js @@ -35,7 +35,7 @@ const createPerson = async (req, res) => { await req.datastore.insert(entity) - res.sendStatus(201) + res.status(201).send({ success: 'Created' }) // Update stats const personCountResult = (await req.datastore.get(req.datastore.key([req.types.stats, 'personCount'])))[0] || null @@ -51,14 +51,14 @@ const createPerson = async (req, res) => { }) } } else { - res.sendStatus(400) + res.status(400).send({ error: 'Unable to create person' }) } } else { - res.sendStatus(404) + res.status(404).send({ error: 'Event does not exist' }) } } catch (e) { console.error(e) - res.sendStatus(400) + res.status(400).send({ error: 'An error occurred while creating the person' }) } } diff --git a/crabfit-backend/routes/getEvent.js b/crabfit-backend/routes/getEvent.js index 0822816..991a71a 100644 --- a/crabfit-backend/routes/getEvent.js +++ b/crabfit-backend/routes/getEvent.js @@ -18,11 +18,11 @@ const getEvent = async (req, res) => { visited: dayjs().unix() }) } else { - res.sendStatus(404) + res.status(404).send({ error: 'Event not found' }) } } catch (e) { console.error(e) - res.sendStatus(404) + res.status(404).send({ error: 'Event not found' }) } } diff --git a/crabfit-backend/routes/getPeople.js b/crabfit-backend/routes/getPeople.js index 30735fe..0a8c041 100644 --- a/crabfit-backend/routes/getPeople.js +++ b/crabfit-backend/routes/getPeople.js @@ -13,7 +13,7 @@ const getPeople = async (req, res) => { res.send({ people }) } catch (e) { console.error(e) - res.sendStatus(404) + res.status(404).send({ error: 'Person not found' }) } } diff --git a/crabfit-backend/routes/login.js b/crabfit-backend/routes/login.js index e572d58..059b8de 100644 --- a/crabfit-backend/routes/login.js +++ b/crabfit-backend/routes/login.js @@ -14,7 +14,7 @@ const login = async (req, res) => { if (personResult.password) { const passwordsMatch = person && person.password && await bcrypt.compare(person.password, personResult.password) if (!passwordsMatch) { - return res.status(401).send('Incorrect password') + return res.status(401).send({ error: 'Incorrect password' }) } } @@ -24,11 +24,11 @@ const login = async (req, res) => { created: personResult.created, }) } else { - res.sendStatus(404) + res.status(404).send({ error: 'Person does not exist' }) } } catch (e) { console.error(e) - res.sendStatus(404) + res.status(400).send({ error: 'An error occurred' }) } } diff --git a/crabfit-backend/routes/taskCleanup.js b/crabfit-backend/routes/taskCleanup.js index 121f776..b4feac5 100644 --- a/crabfit-backend/routes/taskCleanup.js +++ b/crabfit-backend/routes/taskCleanup.js @@ -2,7 +2,7 @@ import dayjs from 'dayjs' const taskCleanup = async (req, res) => { if (req.header('X-Appengine-Cron') === undefined) { - return res.status(400).send('This task can only be run from a cron job') + return res.status(400).send({ error: 'This task can only be run from a cron job' }) } const threeMonthsAgo = dayjs().subtract(3, 'month').unix() diff --git a/crabfit-backend/routes/taskRemoveOrphans.js b/crabfit-backend/routes/taskRemoveOrphans.js index 36b211a..95e4994 100644 --- a/crabfit-backend/routes/taskRemoveOrphans.js +++ b/crabfit-backend/routes/taskRemoveOrphans.js @@ -2,7 +2,7 @@ import dayjs from 'dayjs' const taskRemoveOrphans = async (req, res) => { if (req.header('X-Appengine-Cron') === undefined) { - return res.status(400).send('This task can only be run from a cron job') + return res.status(400).send({ error: 'This task can only be run from a cron job' }) } const threeMonthsAgo = dayjs().subtract(3, 'month').unix() diff --git a/crabfit-backend/routes/updatePerson.js b/crabfit-backend/routes/updatePerson.js index ccacd3f..921bb91 100644 --- a/crabfit-backend/routes/updatePerson.js +++ b/crabfit-backend/routes/updatePerson.js @@ -15,7 +15,7 @@ const updatePerson = async (req, res) => { if (personResult.password) { const passwordsMatch = person.password && await bcrypt.compare(person.password, personResult.password) if (!passwordsMatch) { - return res.status(401).send('Incorrect password') + return res.status(401).send({ error: 'Incorrect password' }) } } @@ -24,16 +24,16 @@ const updatePerson = async (req, res) => { availability: person.availability, }) - res.sendStatus(200) + res.status(200).send({ success: 'Updated' }) } else { - res.sendStatus(400) + res.status(400).send({ error: 'Availability must be set' }) } } else { - res.sendStatus(404) + res.status(404).send({ error: 'Person not found' }) } } catch (e) { console.error(e) - res.sendStatus(400) + res.status(400).send('An error occurred') } }