Always send JSON responses

This commit is contained in:
Ben Grant 2022-08-20 21:52:09 +10:00
parent 7d0f3898de
commit 4ab944b13c
9 changed files with 20 additions and 19 deletions

View file

@ -14,7 +14,8 @@
"build:dev": "NODE_ENV=development parcel build --no-cache", "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", "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", "build": "parcel build",
"start": "node ./dist/index.js" "start": "node ./dist/index.js",
"lint": "eslint index.js ./routes"
}, },
"dependencies": { "dependencies": {
"@google-cloud/datastore": "^7.0.0", "@google-cloud/datastore": "^7.0.0",

View file

@ -77,7 +77,7 @@ const createEvent = async (req, res) => {
} }
} catch (e) { } catch (e) {
console.error(e) console.error(e)
res.sendStatus(400) res.status(400).send({ error: 'An error occurred while creating the event' })
} }
} }

View file

@ -35,7 +35,7 @@ const createPerson = async (req, res) => {
await req.datastore.insert(entity) await req.datastore.insert(entity)
res.sendStatus(201) res.status(201).send({ success: 'Created' })
// Update stats // Update stats
const personCountResult = (await req.datastore.get(req.datastore.key([req.types.stats, 'personCount'])))[0] || null 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 { } else {
res.sendStatus(400) res.status(400).send({ error: 'Unable to create person' })
} }
} else { } else {
res.sendStatus(404) res.status(404).send({ error: 'Event does not exist' })
} }
} catch (e) { } catch (e) {
console.error(e) console.error(e)
res.sendStatus(400) res.status(400).send({ error: 'An error occurred while creating the person' })
} }
} }

View file

@ -18,11 +18,11 @@ const getEvent = async (req, res) => {
visited: dayjs().unix() visited: dayjs().unix()
}) })
} else { } else {
res.sendStatus(404) res.status(404).send({ error: 'Event not found' })
} }
} catch (e) { } catch (e) {
console.error(e) console.error(e)
res.sendStatus(404) res.status(404).send({ error: 'Event not found' })
} }
} }

View file

@ -13,7 +13,7 @@ const getPeople = async (req, res) => {
res.send({ people }) res.send({ people })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
res.sendStatus(404) res.status(404).send({ error: 'Person not found' })
} }
} }

View file

@ -14,7 +14,7 @@ const login = async (req, res) => {
if (personResult.password) { if (personResult.password) {
const passwordsMatch = person && person.password && await bcrypt.compare(person.password, personResult.password) const passwordsMatch = person && person.password && await bcrypt.compare(person.password, personResult.password)
if (!passwordsMatch) { 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, created: personResult.created,
}) })
} else { } else {
res.sendStatus(404) res.status(404).send({ error: 'Person does not exist' })
} }
} catch (e) { } catch (e) {
console.error(e) console.error(e)
res.sendStatus(404) res.status(400).send({ error: 'An error occurred' })
} }
} }

View file

@ -2,7 +2,7 @@ import dayjs from 'dayjs'
const taskCleanup = async (req, res) => { const taskCleanup = async (req, res) => {
if (req.header('X-Appengine-Cron') === undefined) { 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() const threeMonthsAgo = dayjs().subtract(3, 'month').unix()

View file

@ -2,7 +2,7 @@ import dayjs from 'dayjs'
const taskRemoveOrphans = async (req, res) => { const taskRemoveOrphans = async (req, res) => {
if (req.header('X-Appengine-Cron') === undefined) { 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() const threeMonthsAgo = dayjs().subtract(3, 'month').unix()

View file

@ -15,7 +15,7 @@ const updatePerson = async (req, res) => {
if (personResult.password) { if (personResult.password) {
const passwordsMatch = person.password && await bcrypt.compare(person.password, personResult.password) const passwordsMatch = person.password && await bcrypt.compare(person.password, personResult.password)
if (!passwordsMatch) { 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, availability: person.availability,
}) })
res.sendStatus(200) res.status(200).send({ success: 'Updated' })
} else { } else {
res.sendStatus(400) res.status(400).send({ error: 'Availability must be set' })
} }
} else { } else {
res.sendStatus(404) res.status(404).send({ error: 'Person not found' })
} }
} catch (e) { } catch (e) {
console.error(e) console.error(e)
res.sendStatus(400) res.status(400).send('An error occurred')
} }
} }