From 8a83c3f61082fa71dfad7576c4ab81d0783ce468 Mon Sep 17 00:00:00 2001 From: moyo-paystack Date: Sun, 23 Aug 2020 05:16:37 +0100 Subject: [PATCH 1/6] error response handler --- controllers/index.js | 4 ++-- controllers/playlist.js | 7 +++---- controllers/track.js | 7 +++---- responses/errorResponse.js | 8 ++++++++ 4 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 responses/errorResponse.js diff --git a/controllers/index.js b/controllers/index.js index 01c16e1..53139f9 100644 --- a/controllers/index.js +++ b/controllers/index.js @@ -6,6 +6,7 @@ const serverMethods = require('../helpers/server-methods'); const slack = require('../helpers/slack'); const resetScript = require('../scripts/reset'); const logger = require('../helpers/logger'); +const errorResponse = require('../responses/errorResponse'); const { app: appConfig } = require('../config'); @@ -93,8 +94,7 @@ module.exports = { data: playlist, }); } catch (err) { - logger.error(err); - return res.status(500).send({ message: 'An error occurred' }); + return errorResponse(res, err); } }, diff --git a/controllers/playlist.js b/controllers/playlist.js index af20ea2..b79a48e 100644 --- a/controllers/playlist.js +++ b/controllers/playlist.js @@ -1,5 +1,5 @@ const spotify = require('../helpers/spotify'); -const logger = require('../helpers/logger'); +const errorResponse = require('../responses/errorResponse'); module.exports = { @@ -19,8 +19,7 @@ module.exports = { data: playlist, }); } catch (err) { - logger.error(err); - return res.status(500).send({ status: false, message: 'An error occurred' }); + return errorResponse(res, err); } }, @@ -32,7 +31,7 @@ module.exports = { data: playlists, }); } catch (err) { - return res.status(500).send({ status: false, message: 'An error occurred' }); + return errorResponse(res, err); } }, }; diff --git a/controllers/track.js b/controllers/track.js index 87a6911..dd13ac2 100644 --- a/controllers/track.js +++ b/controllers/track.js @@ -1,5 +1,5 @@ const spotify = require('../helpers/spotify'); -const logger = require('../helpers/logger'); +const errorResponse = require('../responses/errorResponse'); module.exports = { @@ -14,8 +14,7 @@ module.exports = { data: trackFeatures, }); } catch (err) { - logger.error(err); - return res.status(500).send({ message: 'An error occurred' }); + return errorResponse(res, err); } }, @@ -35,7 +34,7 @@ module.exports = { data, }); } catch (err) { - return res.status(500).send({ message: 'An error occurred' }); + return errorResponse(res, err); } }, }; diff --git a/responses/errorResponse.js b/responses/errorResponse.js new file mode 100644 index 0000000..042fd3b --- /dev/null +++ b/responses/errorResponse.js @@ -0,0 +1,8 @@ +const logger = require('../helpers/logger'); + +const errorResponse = (res, err) => { + logger.error(err); + return res.status(500).send({ success: false, message: 'An error occurred' }); +}; + +module.exports = errorResponse; From a03e254c43572c9e5d389100b5504f4a564c3cdd Mon Sep 17 00:00:00 2001 From: moyo-paystack Date: Mon, 24 Aug 2020 10:33:06 +0100 Subject: [PATCH 2/6] handle 404 endpoints, improve api response handling --- controllers/index.js | 4 ++-- controllers/notFound.js | 12 ++++++++++++ controllers/playlist.js | 29 ++++++++++++++--------------- controllers/track.js | 27 +++++++++++++++------------ responses/clientErrorResponse.js | 11 +++++++++++ responses/errorResponse.js | 8 -------- responses/serverErrorResponse.js | 15 +++++++++++++++ responses/successResponse.js | 12 ++++++++++++ routes/notFound.js | 9 +++++++++ server.js | 2 ++ 10 files changed, 92 insertions(+), 37 deletions(-) create mode 100644 controllers/notFound.js create mode 100644 responses/clientErrorResponse.js delete mode 100644 responses/errorResponse.js create mode 100644 responses/serverErrorResponse.js create mode 100644 responses/successResponse.js create mode 100644 routes/notFound.js diff --git a/controllers/index.js b/controllers/index.js index 53139f9..19ceb07 100644 --- a/controllers/index.js +++ b/controllers/index.js @@ -6,7 +6,7 @@ const serverMethods = require('../helpers/server-methods'); const slack = require('../helpers/slack'); const resetScript = require('../scripts/reset'); const logger = require('../helpers/logger'); -const errorResponse = require('../responses/errorResponse'); +const serverErrorResponse = require('../responses/serverErrorResponse'); const { app: appConfig } = require('../config'); @@ -94,7 +94,7 @@ module.exports = { data: playlist, }); } catch (err) { - return errorResponse(res, err); + return serverErrorResponse(res, err); } }, diff --git a/controllers/notFound.js b/controllers/notFound.js new file mode 100644 index 0000000..58bf80b --- /dev/null +++ b/controllers/notFound.js @@ -0,0 +1,12 @@ +const clientErrorResponse = require('../responses/clientErrorResponse'); + +/** + * Controller for handling routes that don't exist + * @param {*} req + * @param {*} res + */ +const notFound = (req, res) => { + return clientErrorResponse(res, 400, 'This route doesn\'t exist yet'); +} + +module.exports = notFound; diff --git a/controllers/playlist.js b/controllers/playlist.js index b79a48e..ba8affd 100644 --- a/controllers/playlist.js +++ b/controllers/playlist.js @@ -1,37 +1,36 @@ const spotify = require('../helpers/spotify'); -const errorResponse = require('../responses/errorResponse'); +const serverErrorResponse = require('../responses/serverErrorResponse'); +const clientErrorResponse = require('../responses/clientErrorResponse'); +const successResponse = require('../responses/successResponse'); module.exports = { + /** + * Retrieve a single playlist + */ getPlaylistByID: async (req, res) => { try { const { id } = req.params; const playlist = await spotify.findPlaylist(id); if (!playlist) { - return res.status(404).send({ - status: false, - message: 'Playlist not found', - }); + return clientErrorResponse(res, 404, 'Playlist not found'); } - return res.status(200).send({ - status: true, - data: playlist, - }); + return successResponse(res, 200, 'Playlist retrieved', playlist); } catch (err) { - return errorResponse(res, err); + return serverErrorResponse(res, err); } }, + /** + * Retrieve all playlists + */ getAllPlaylists: async (req, res) => { try { const playlists = await spotify.findAllPlaylists(); - return res.status(200).send({ - status: true, - data: playlists, - }); + return successResponse(res, 200, 'Playlists retrieved', playlists); } catch (err) { - return errorResponse(res, err); + return serverErrorResponse(res, err); } }, }; diff --git a/controllers/track.js b/controllers/track.js index dd13ac2..e3a8a81 100644 --- a/controllers/track.js +++ b/controllers/track.js @@ -1,40 +1,43 @@ const spotify = require('../helpers/spotify'); -const errorResponse = require('../responses/errorResponse'); +const serverErrorResponse = require('../responses/serverErrorResponse'); +const clientErrorResponse = require('../responses/clientErrorResponse'); +const successResponse = require('../responses/successResponse'); module.exports = { + /** + * Get track audio features + */ getTrackAudioFeatures: async (req, res) => { try { const { id: trackId } = req.params; await spotify.performAuthentication(); const trackFeatures = await spotify.getAudioFeaturesForTrack(trackId); - return res.status(200).send({ - status: true, - data: trackFeatures, - }); + + return successResponse(res, 200, 'Audio features retrieved', trackFeatures); } catch (err) { - return errorResponse(res, err); + return serverErrorResponse(res, err); } }, + /** + * Get track data + */ getTrackData: async (req, res) => { try { const { track_ids: ids } = req.body; const result = await spotify.performAuthentication(); if (result && result.code === 401) { - return res.status(401).send({ message: result.message }); + return clientErrorResponse(res, result.code, result.message); } const data = await spotify.getTrackData(ids); - return res.status(200).send({ - status: true, - data, - }); + return successResponse(res, 200, 'Track data retrieved', data); } catch (err) { - return errorResponse(res, err); + return serverErrorResponse(res, err); } }, }; diff --git a/responses/clientErrorResponse.js b/responses/clientErrorResponse.js new file mode 100644 index 0000000..4407e6c --- /dev/null +++ b/responses/clientErrorResponse.js @@ -0,0 +1,11 @@ +/** + * Method to return client error responses + * @param {*} res + * @param {*} code + * @param {*} message + */ +const clientErrorResponse = (res, code, message) => { + return res.status(code).send({ success: false, message }); +}; + +module.exports = clientErrorResponse; diff --git a/responses/errorResponse.js b/responses/errorResponse.js deleted file mode 100644 index 042fd3b..0000000 --- a/responses/errorResponse.js +++ /dev/null @@ -1,8 +0,0 @@ -const logger = require('../helpers/logger'); - -const errorResponse = (res, err) => { - logger.error(err); - return res.status(500).send({ success: false, message: 'An error occurred' }); -}; - -module.exports = errorResponse; diff --git a/responses/serverErrorResponse.js b/responses/serverErrorResponse.js new file mode 100644 index 0000000..36593f7 --- /dev/null +++ b/responses/serverErrorResponse.js @@ -0,0 +1,15 @@ +const logger = require('../helpers/logger'); +const slack = require('../helpers/slack'); + +/** + * Method to return 5xx errors + * @param {*} res + * @param {*} error + */ +const serverErrorResponse = (res, error, code = 500) => { + logger.error(error); + slack.sendMonitorMessage(error); + return res.status(code).send({ success: false, message: 'An error occurred' }); +}; + +module.exports = serverErrorResponse; diff --git a/responses/successResponse.js b/responses/successResponse.js new file mode 100644 index 0000000..6b638a2 --- /dev/null +++ b/responses/successResponse.js @@ -0,0 +1,12 @@ +/** + * Handler for success responses + * @param {*} res + * @param {*} code + * @param {*} message + * @param {*} data + */ +const successResponse = (res, code, message, data) => { + return res.status(code).send({ success: false, message, data }); +}; + +module.exports = successResponse; diff --git a/routes/notFound.js b/routes/notFound.js new file mode 100644 index 0000000..e34846f --- /dev/null +++ b/routes/notFound.js @@ -0,0 +1,9 @@ +const express = require('express'); + +const router = express.Router(); + +const notFound = require('../controllers/notFound'); + +router.get('/', notFound); + +module.exports = router; diff --git a/server.js b/server.js index f3c043c..2748132 100644 --- a/server.js +++ b/server.js @@ -14,6 +14,7 @@ const logger = require('./helpers/logger'); const indexRouter = require('./routes/index'); const playlistRouter = require('./routes/playlists'); const trackRouter = require('./routes/track'); +const notFoundRouter = require('./routes/notFound'); require('./config/db'); @@ -26,6 +27,7 @@ app.disable('x-powered-by'); app.use('/', indexRouter); app.use('/playlists', playlistRouter); app.use('/track', trackRouter); +app.use('*', notFoundRouter); // listen for requests :) const listener = app.listen(appConfig.port, () => { From 86dd927b48401ff7f3310c1d6aec39887e37e6c0 Mon Sep 17 00:00:00 2001 From: moyo-paystack Date: Mon, 24 Aug 2020 10:43:24 +0100 Subject: [PATCH 3/6] fix lint issues --- controllers/notFound.js | 10 ++++------ responses/clientErrorResponse.js | 12 ++++++------ responses/serverErrorResponse.js | 4 ++-- responses/successResponse.js | 14 +++++++------- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/controllers/notFound.js b/controllers/notFound.js index 58bf80b..b5f43f9 100644 --- a/controllers/notFound.js +++ b/controllers/notFound.js @@ -2,11 +2,9 @@ const clientErrorResponse = require('../responses/clientErrorResponse'); /** * Controller for handling routes that don't exist - * @param {*} req - * @param {*} res + * @param {*} req + * @param {*} res */ -const notFound = (req, res) => { - return clientErrorResponse(res, 400, 'This route doesn\'t exist yet'); -} - +const notFound = (req, res) => clientErrorResponse(res, 400, 'This route doesn\'t exist yet'); + module.exports = notFound; diff --git a/responses/clientErrorResponse.js b/responses/clientErrorResponse.js index 4407e6c..6ef61f2 100644 --- a/responses/clientErrorResponse.js +++ b/responses/clientErrorResponse.js @@ -1,11 +1,11 @@ /** * Method to return client error responses - * @param {*} res - * @param {*} code - * @param {*} message + * @param {*} res + * @param {*} code + * @param {*} message */ -const clientErrorResponse = (res, code, message) => { - return res.status(code).send({ success: false, message }); -}; + +/* eslint-disable-next-line max-len */ +const clientErrorResponse = (res, code, message) => res.status(code).send({ success: false, message }); module.exports = clientErrorResponse; diff --git a/responses/serverErrorResponse.js b/responses/serverErrorResponse.js index 36593f7..637ee9c 100644 --- a/responses/serverErrorResponse.js +++ b/responses/serverErrorResponse.js @@ -3,8 +3,8 @@ const slack = require('../helpers/slack'); /** * Method to return 5xx errors - * @param {*} res - * @param {*} error + * @param {*} res + * @param {*} error */ const serverErrorResponse = (res, error, code = 500) => { logger.error(error); diff --git a/responses/successResponse.js b/responses/successResponse.js index 6b638a2..07d3fca 100644 --- a/responses/successResponse.js +++ b/responses/successResponse.js @@ -1,12 +1,12 @@ /** * Handler for success responses - * @param {*} res - * @param {*} code - * @param {*} message - * @param {*} data + * @param {*} res + * @param {*} code + * @param {*} message + * @param {*} data */ -const successResponse = (res, code, message, data) => { - return res.status(code).send({ success: false, message, data }); -}; + +/* eslint-disable-next-line max-len */ +const successResponse = (res, code, message, data) => res.status(code).send({ success: false, message, data }); module.exports = successResponse; From 5d5f157ad325e763b70289c1f56cf228fc519a09 Mon Sep 17 00:00:00 2001 From: moyo-paystack Date: Mon, 24 Aug 2020 11:23:03 +0100 Subject: [PATCH 4/6] move responses to one file --- controllers/index.js | 2 +- controllers/notFound.js | 2 +- controllers/playlist.js | 8 ++++--- controllers/track.js | 8 ++++--- responses/clientErrorResponse.js | 11 --------- responses/index.js | 39 ++++++++++++++++++++++++++++++++ responses/serverErrorResponse.js | 15 ------------ responses/successResponse.js | 12 ---------- 8 files changed, 51 insertions(+), 46 deletions(-) delete mode 100644 responses/clientErrorResponse.js create mode 100644 responses/index.js delete mode 100644 responses/serverErrorResponse.js delete mode 100644 responses/successResponse.js diff --git a/controllers/index.js b/controllers/index.js index 19ceb07..343b899 100644 --- a/controllers/index.js +++ b/controllers/index.js @@ -6,7 +6,7 @@ const serverMethods = require('../helpers/server-methods'); const slack = require('../helpers/slack'); const resetScript = require('../scripts/reset'); const logger = require('../helpers/logger'); -const serverErrorResponse = require('../responses/serverErrorResponse'); +const { serverErrorResponse } = require('../responses'); const { app: appConfig } = require('../config'); diff --git a/controllers/notFound.js b/controllers/notFound.js index b5f43f9..e8b03e7 100644 --- a/controllers/notFound.js +++ b/controllers/notFound.js @@ -1,4 +1,4 @@ -const clientErrorResponse = require('../responses/clientErrorResponse'); +const { clientErrorResponse } = require('../responses'); /** * Controller for handling routes that don't exist diff --git a/controllers/playlist.js b/controllers/playlist.js index ba8affd..7c72e00 100644 --- a/controllers/playlist.js +++ b/controllers/playlist.js @@ -1,7 +1,9 @@ const spotify = require('../helpers/spotify'); -const serverErrorResponse = require('../responses/serverErrorResponse'); -const clientErrorResponse = require('../responses/clientErrorResponse'); -const successResponse = require('../responses/successResponse'); +const { + successResponse, + clientErrorResponse, + serverErrorResponse, +} = require('../responses'); module.exports = { diff --git a/controllers/track.js b/controllers/track.js index e3a8a81..fb94613 100644 --- a/controllers/track.js +++ b/controllers/track.js @@ -1,7 +1,9 @@ const spotify = require('../helpers/spotify'); -const serverErrorResponse = require('../responses/serverErrorResponse'); -const clientErrorResponse = require('../responses/clientErrorResponse'); -const successResponse = require('../responses/successResponse'); +const { + successResponse, + clientErrorResponse, + serverErrorResponse, +} = require('../responses'); module.exports = { diff --git a/responses/clientErrorResponse.js b/responses/clientErrorResponse.js deleted file mode 100644 index 6ef61f2..0000000 --- a/responses/clientErrorResponse.js +++ /dev/null @@ -1,11 +0,0 @@ -/** - * Method to return client error responses - * @param {*} res - * @param {*} code - * @param {*} message - */ - -/* eslint-disable-next-line max-len */ -const clientErrorResponse = (res, code, message) => res.status(code).send({ success: false, message }); - -module.exports = clientErrorResponse; diff --git a/responses/index.js b/responses/index.js new file mode 100644 index 0000000..fb60a2c --- /dev/null +++ b/responses/index.js @@ -0,0 +1,39 @@ +const logger = require('../helpers/logger'); +const slack = require('../helpers/slack'); + +/** + * Handler for success responses + * @param {Object} res + * @param {Number} code + * @param {String} message + * @param {Array|Object} data + */ +/* eslint-disable-next-line max-len */ +const successResponse = (res, code, message, data) => res.status(code).send({ success: true, message, data }); + +/** + * Method to return client error responses + * @param {Object} res + * @param {Number} code + * @param {String} message + */ +/* eslint-disable-next-line max-len */ +const clientErrorResponse = (res, code, message) => res.status(code).send({ success: false, message }); + +/** + * Method to return 5xx errors + * @param {Object} res + * @param {Object} error + * @param {Number} code + */ +const serverErrorResponse = (res, error, code = 500) => { + logger.error(error); + slack.sendMonitorMessage(error); + return res.status(code).send({ success: false, message: 'An error occurred' }); +}; + +module.exports = { + successResponse, + clientErrorResponse, + serverErrorResponse, +}; diff --git a/responses/serverErrorResponse.js b/responses/serverErrorResponse.js deleted file mode 100644 index 637ee9c..0000000 --- a/responses/serverErrorResponse.js +++ /dev/null @@ -1,15 +0,0 @@ -const logger = require('../helpers/logger'); -const slack = require('../helpers/slack'); - -/** - * Method to return 5xx errors - * @param {*} res - * @param {*} error - */ -const serverErrorResponse = (res, error, code = 500) => { - logger.error(error); - slack.sendMonitorMessage(error); - return res.status(code).send({ success: false, message: 'An error occurred' }); -}; - -module.exports = serverErrorResponse; diff --git a/responses/successResponse.js b/responses/successResponse.js deleted file mode 100644 index 07d3fca..0000000 --- a/responses/successResponse.js +++ /dev/null @@ -1,12 +0,0 @@ -/** - * Handler for success responses - * @param {*} res - * @param {*} code - * @param {*} message - * @param {*} data - */ - -/* eslint-disable-next-line max-len */ -const successResponse = (res, code, message, data) => res.status(code).send({ success: false, message, data }); - -module.exports = successResponse; From 305f21e11951d63bbff9bbb181a9c58d91ed7567 Mon Sep 17 00:00:00 2001 From: moyo-paystack Date: Mon, 24 Aug 2020 11:32:48 +0100 Subject: [PATCH 5/6] remove eslint max-len exception --- responses/index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/responses/index.js b/responses/index.js index fb60a2c..a1da96a 100644 --- a/responses/index.js +++ b/responses/index.js @@ -8,8 +8,9 @@ const slack = require('../helpers/slack'); * @param {String} message * @param {Array|Object} data */ -/* eslint-disable-next-line max-len */ -const successResponse = (res, code, message, data) => res.status(code).send({ success: true, message, data }); +const successResponse = (res, code, message, data) => ( + res.status(code).send({ success: true, message, data }) +); /** * Method to return client error responses @@ -17,8 +18,9 @@ const successResponse = (res, code, message, data) => res.status(code).send({ su * @param {Number} code * @param {String} message */ -/* eslint-disable-next-line max-len */ -const clientErrorResponse = (res, code, message) => res.status(code).send({ success: false, message }); +const clientErrorResponse = (res, code, message) => ( + res.status(code).send({ success: false, message }) +); /** * Method to return 5xx errors From c424177c8636011a1c72e002a924cf11b6cf67d9 Mon Sep 17 00:00:00 2001 From: moyo-paystack Date: Fri, 25 Sep 2020 10:37:04 +0100 Subject: [PATCH 6/6] fix lint errors --- controllers/track.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/controllers/track.js b/controllers/track.js index 862afaa..39d7532 100644 --- a/controllers/track.js +++ b/controllers/track.js @@ -16,13 +16,9 @@ module.exports = { if (!tracks.length) return res.status(200).send({ status: true, message: 'All tracks have their analytics set' }); await spotify.performAuthentication(); spotify.getAudioAnalyticsForTracks(tracks); - return res.status(200).send({ - status: true, - message: 'Populating analytics...', - }); + return successResponse(res, 200, 'Populating analytics...'); } catch (err) { - logger.error(err); - return res.status(500).send({ message: 'An error occurred' }); + return serverErrorResponse(res, err); } },