Skip to content

Commit

Permalink
[Feature][NA] Error handling (#10)
Browse files Browse the repository at this point in the history
* Use middleware for exception handling

* Do minor cleanup
  • Loading branch information
florenceshelley authored and tunimosmann committed Sep 19, 2019
1 parent 474e983 commit afffa97
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 29 deletions.
6 changes: 1 addition & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ logger.info('🤖 Initializing middleware')
// you hit an endpoint in your terminal. It's here to help you debug.
app.use(morgan('tiny', { stream: logger.stream }))

app.use(
cors({
origin: `http://${process.env.PROJECT_NAME}-frontend.bridgeschoolapp.io`
})
)
app.use(cors({ origin }))

app.use(bodyParser.json()).use(bodyParser.urlencoded({ extended: true }))
app.use(cors({ origin }))
Expand Down
6 changes: 2 additions & 4 deletions src/routes/channels/channels.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const axios = require('axios')
const { parametize } = require('../../utils/parametize')
const parametize = require('../../utils/parametize')

const channelsController = async (req, res, next) => {
const params = parametize({
Expand All @@ -19,6 +19,4 @@ const channelsController = async (req, res, next) => {
}
}

module.exports = {
channelsController
}
module.exports = channelsController
2 changes: 1 addition & 1 deletion src/routes/channels/channels.router.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express')

const { channelsController } = require('./channels.controller')
const channelsController = require('./channels.controller')

const router = express.Router()

Expand Down
33 changes: 15 additions & 18 deletions src/routes/polls/polls.controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const axios = require('axios')
const { parametize } = require('../../utils/parametize')
const db = require('../../db')

const parametize = require('../../utils/parametize')
const createSlackBlock = require('../../utils/createSlackBlock')

const sendPolltoSlack = async (interactionBlock, channel) => {
Expand All @@ -18,11 +18,11 @@ const sendPolltoSlack = async (interactionBlock, channel) => {
const res = await axios.post(url)
return res.data
} catch (err) {
next(err)
return err
}
}

const addPollsController = async (req, res) => {
const addPollsController = async (req, res, next) => {
const { question, channel_name, channel_id } = req.body
const newPoll = {
question: question,
Expand All @@ -42,10 +42,11 @@ const addPollsController = async (req, res) => {
.then(async docRef => {
const block = createSlackBlock(docRef.id, question)
const sendPolltoSlackData = await sendPolltoSlack(block, channel_id)

if (sendPolltoSlackData.ok) {
res.status(200).json({
id: docRef.id,
message: 'Poll successfully created in db and sent to slack'
message: 'Poll successfully created and sent to slack'
})
} else {
res.status(200).json({
Expand All @@ -56,12 +57,10 @@ const addPollsController = async (req, res) => {
})
}
})
.catch(error => {
res.json({ error })
})
.catch(error => next(error))
}

const getPollsController = async (req, res) => {
const getPollsController = async (req, res, next) => {
db.collection('/polls')
.get()
.then(snapshot => {
Expand All @@ -74,17 +73,15 @@ const getPollsController = async (req, res) => {
})
})
})
.catch(error => {
res.json({ error })
})
.catch(error => next(error))
}

const getPollByIdController = async (req, res) => {
const getPollByIdController = async (req, res, next) => {
db.collection('polls')
.doc(req.params.id)
.get()
.then(snapshot => res.status(200).json(snapshot.data()))
.catch(error => res.json({ error }))
.catch(error => next(error))
}

const getCurrentResponse = async block_id => {
Expand All @@ -96,12 +93,12 @@ const getCurrentResponse = async block_id => {
.then(snapshot => {
currentResponse = snapshot.data().response
})
.catch(error => console.log(error))
.catch(error => next(error))

return currentResponse
}

const updatePollsController = async (req, res) => {
const updatePollsController = async (req, res, next) => {
const action = JSON.parse(req.body.payload).actions[0]
const { block_id, value } = action

Expand All @@ -118,15 +115,15 @@ const updatePollsController = async (req, res) => {
response: newResponse
})
.then(res.status(200).send('Poll successfully updated'))
.catch(error => res.json({ error }))
.catch(error => next(error))
}

const deletePollsController = async (req, res) => {
const deletePollsController = async (req, res, next) => {
db.collection('polls')
.doc(req.params.id)
.delete()
.then(res.status(200).send('Poll successfully deleted'))
.catch(error => res.json({ error }))
.catch(error => next(error))
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/parametize.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ const parametize = params =>
return (string += prefix + toInterpolate)
}, '')

module.exports = { parametize }
module.exports = parametize

0 comments on commit afffa97

Please sign in to comment.