From db615e511195adcc224e3c3668e0d993dd441dc6 Mon Sep 17 00:00:00 2001 From: BoHong Li Date: Wed, 26 Feb 2020 10:23:46 +0800 Subject: [PATCH 1/8] fix: page broken when shortId doesn't exists Signed-off-by: BoHong Li --- lib/note/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/note/index.js b/lib/note/index.js index 47698204..d90371fe 100644 --- a/lib/note/index.js +++ b/lib/note/index.js @@ -94,14 +94,14 @@ async function showPublishNote (req, res) { includeUser: true }) - if (!canViewNote(note, req.isAuthenticated(), req.user ? req.user.id : null)) { - return errorForbidden(req) - } - if (!note) { return errorNotFound(res) } + if (!canViewNote(note, req.isAuthenticated(), req.user ? req.user.id : null)) { + return errorForbidden(req) + } + if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) { return res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid)) } From 7870b82249e963fb69052673b1a216f005972d35 Mon Sep 17 00:00:00 2001 From: BoHong Li Date: Wed, 26 Feb 2020 10:25:12 +0800 Subject: [PATCH 2/8] fix: server throw TypeError when user not sign-in Signed-off-by: BoHong Li --- lib/user/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/user/index.js b/lib/user/index.js index 74649463..36c1cd62 100644 --- a/lib/user/index.js +++ b/lib/user/index.js @@ -11,7 +11,7 @@ const { generateAvatar } = require('../letter-avatars') exports.getMe = async (req, res) => { if (!req.isAuthenticated()) { - res.status(401).send({ + return res.status(401).send({ status: 'forbidden' }) } From 6b1ce381dfcc612cb2fe34b0af34fde697ea4593 Mon Sep 17 00:00:00 2001 From: BoHong Li Date: Wed, 26 Feb 2020 11:13:45 +0800 Subject: [PATCH 3/8] fix: change errorForbidden function signature, add req into function parameter avoid incorrect function parameter passing Signed-off-by: BoHong Li --- lib/history/index.js | 6 +++--- lib/imageRouter/index.js | 2 +- lib/note/index.js | 8 ++++---- lib/response.js | 17 ++++++++--------- lib/user/index.js | 6 +++--- 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/history/index.js b/lib/history/index.js index 19a2646a..ae2d27a0 100644 --- a/lib/history/index.js +++ b/lib/history/index.js @@ -123,7 +123,7 @@ function historyGet (req, res) { }) }) } else { - return response.errorForbidden(res) + return response.errorForbidden(req, res) } } @@ -164,7 +164,7 @@ function historyPost (req, res) { }) } } else { - return response.errorForbidden(res) + return response.errorForbidden(req, res) } } @@ -188,7 +188,7 @@ function historyDelete (req, res) { }) } } else { - return response.errorForbidden(res) + return response.errorForbidden(req, res) } } diff --git a/lib/imageRouter/index.js b/lib/imageRouter/index.js index c4ab548f..9b0c4028 100644 --- a/lib/imageRouter/index.js +++ b/lib/imageRouter/index.js @@ -21,7 +21,7 @@ imageRouter.post('/uploadimage', function (req, res) { form.parse(req, function (err, fields, files) { if (err || !files.image || !files.image.path) { - response.errorForbidden(res) + response.errorForbidden(req, res) } else { if (config.debug) { logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image)) diff --git a/lib/note/index.js b/lib/note/index.js index d90371fe..017d2238 100644 --- a/lib/note/index.js +++ b/lib/note/index.js @@ -66,7 +66,7 @@ async function showNote (req, res) { } if (!newCheckViewPermission(note, req.isAuthenticated(), userId)) { - return errorForbidden(res) + return errorForbidden(req, res) } // force to use note id @@ -99,7 +99,7 @@ async function showPublishNote (req, res) { } if (!canViewNote(note, req.isAuthenticated(), req.user ? req.user.id : null)) { - return errorForbidden(req) + return errorForbidden(req, res) } if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) { @@ -150,7 +150,7 @@ async function noteActions (req, res) { } if (!canViewNote(note, req.isAuthenticated(), req.user ? req.user.id : null)) { - return errorForbidden(req) + return errorForbidden(req, res) } const action = req.params.action @@ -171,7 +171,7 @@ async function noteActions (req, res) { actionPDF(req, res, note) } else { logger.error('PDF export failed: Disabled by config. Set "allowPDFExport: true" to enable. Check the documentation for details') - errorForbidden(res) + errorForbidden(req, res) } break case 'gist': diff --git a/lib/response.js b/lib/response.js index 4779afb8..42127696 100644 --- a/lib/response.js +++ b/lib/response.js @@ -28,8 +28,7 @@ exports.checkViewPermission = checkViewPermission exports.newCheckViewPermission = newCheckViewPermission exports.responseCodiMD = responseCodiMD -function errorForbidden (res) { - const { req } = res +function errorForbidden (req, res) { if (req.user) { responseError(res, '403', 'Forbidden', 'oh no.') } else { @@ -100,7 +99,7 @@ function newNote (req, res, next) { if (req.isAuthenticated()) { owner = req.user.id } else if (!config.allowAnonymous) { - return errorForbidden(res) + return errorForbidden(req, res) } models.Note.create({ ownerId: owner, @@ -161,7 +160,7 @@ function findNote (req, res, callback, include) { } } if (!checkViewPermission(req, note)) { - return errorForbidden(res) + return errorForbidden(req, res) } else { return callback(note) } @@ -239,7 +238,7 @@ function githubActionGist (req, res, note) { var code = req.query.code var state = req.query.state if (!code || !state) { - return errorForbidden(res) + return errorForbidden(req, res) } else { var data = { client_id: config.github.clientID, @@ -279,14 +278,14 @@ function githubActionGist (req, res, note) { res.setHeader('referer', '') res.redirect(body.html_url) } else { - return errorForbidden(res) + return errorForbidden(req, res) } }) } else { - return errorForbidden(res) + return errorForbidden(req, res) } } else { - return errorForbidden(res) + return errorForbidden(req, res) } }) } @@ -334,7 +333,7 @@ function gitlabActionProjects (req, res, note) { return errorInternalError(res) }) } else { - return errorForbidden(res) + return errorForbidden(req, res) } } diff --git a/lib/user/index.js b/lib/user/index.js index 36c1cd62..4412713e 100644 --- a/lib/user/index.js +++ b/lib/user/index.js @@ -37,7 +37,7 @@ exports.getMe = async (req, res) => { exports.deleteUser = async (req, res) => { if (!req.isAuthenticated()) { - return response.errorForbidden(res) + return response.errorForbidden(req, res) } const user = await models.User.findOne({ @@ -51,7 +51,7 @@ exports.deleteUser = async (req, res) => { } if (user.deleteToken !== req.params.token) { - return response.errorForbidden(res) + return response.errorForbidden(req, res) } await user.destroy() @@ -60,7 +60,7 @@ exports.deleteUser = async (req, res) => { exports.exportMyData = (req, res) => { if (!req.isAuthenticated()) { - return response.errorForbidden(res) + return response.errorForbidden(req, res) } const archive = archiver('zip', { From 09a353ffcc0dd851f8a34a21a013754d68014317 Mon Sep 17 00:00:00 2001 From: BoHong Li Date: Wed, 26 Feb 2020 11:20:42 +0800 Subject: [PATCH 4/8] refactor: change errorNotFound function signature to avoid parameter passing error Signed-off-by: BoHong Li --- app.js | 2 +- lib/history/index.js | 8 ++++---- lib/note/index.js | 6 +++--- lib/note/noteActions.js | 4 ++-- lib/response.js | 8 ++++---- lib/user/index.js | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app.js b/app.js index 16ea46b7..449262ab 100644 --- a/app.js +++ b/app.js @@ -222,7 +222,7 @@ app.use(require('./lib/routes').router) // response not found if no any route matxches app.get('*', function (req, res) { - response.errorNotFound(res) + response.errorNotFound(req, res) }) // socket.io secure diff --git a/lib/history/index.js b/lib/history/index.js index ae2d27a0..e38af7e4 100644 --- a/lib/history/index.js +++ b/lib/history/index.js @@ -117,7 +117,7 @@ function historyGet (req, res) { if (req.isAuthenticated()) { getHistory(req.user.id, function (err, history) { if (err) return response.errorInternalError(res) - if (!history) return response.errorNotFound(res) + if (!history) return response.errorNotFound(req, res) res.send({ history: parseHistoryToArray(history) }) @@ -150,8 +150,8 @@ function historyPost (req, res) { if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(res) getHistory(req.user.id, function (err, history) { if (err) return response.errorInternalError(res) - if (!history) return response.errorNotFound(res) - if (!history[noteId]) return response.errorNotFound(res) + if (!history) return response.errorNotFound(req, res) + if (!history[noteId]) return response.errorNotFound(req, res) if (req.body.pinned === 'true' || req.body.pinned === 'false') { history[noteId].pinned = (req.body.pinned === 'true') setHistory(req.user.id, history, function (err, count) { @@ -179,7 +179,7 @@ function historyDelete (req, res) { } else { getHistory(req.user.id, function (err, history) { if (err) return response.errorInternalError(res) - if (!history) return response.errorNotFound(res) + if (!history) return response.errorNotFound(req, res) delete history[noteId] setHistory(req.user.id, history, function (err, count) { if (err) return response.errorInternalError(res) diff --git a/lib/note/index.js b/lib/note/index.js index 017d2238..33993ab1 100644 --- a/lib/note/index.js +++ b/lib/note/index.js @@ -60,7 +60,7 @@ async function showNote (req, res) { if (!note) { // if allow free url enable, auto create note if (!config.allowFreeURL || config.forbiddenNoteIDs.includes(noteId)) { - return errorNotFound(res) + return errorNotFound(req, res) } note = await createNote(userId, noteId) } @@ -95,7 +95,7 @@ async function showPublishNote (req, res) { }) if (!note) { - return errorNotFound(res) + return errorNotFound(req, res) } if (!canViewNote(note, req.isAuthenticated(), req.user ? req.user.id : null)) { @@ -146,7 +146,7 @@ async function noteActions (req, res) { const note = await getNoteById(noteId) if (!note) { - return errorNotFound(res) + return errorNotFound(req, res) } if (!canViewNote(note, req.isAuthenticated(), req.user ? req.user.id : null)) { diff --git a/lib/note/noteActions.js b/lib/note/noteActions.js index 5b1d6860..19ea3b65 100644 --- a/lib/note/noteActions.js +++ b/lib/note/noteActions.js @@ -173,7 +173,7 @@ function actionRevision (req, res, note) { if (actionId) { const time = moment(parseInt(actionId)) if (!time.isValid()) { - return errorNotFound(res) + return errorNotFound(req, res) } Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) { if (err) { @@ -181,7 +181,7 @@ function actionRevision (req, res, note) { return errorInternalError(res) } if (!content) { - return errorNotFound(res) + return errorNotFound(req, res) } res.set({ 'Access-Control-Allow-Origin': '*', // allow CORS as API diff --git a/lib/response.js b/lib/response.js index 42127696..8fdb36a3 100644 --- a/lib/response.js +++ b/lib/response.js @@ -37,7 +37,7 @@ function errorForbidden (req, res) { } } -function errorNotFound (res) { +function errorNotFound (req, res) { responseError(res, '404', 'Not Found', 'oops.') } @@ -156,7 +156,7 @@ function findNote (req, res, callback, include) { req.alias = noteId return newNote(req, res) } else { - return errorNotFound(res) + return errorNotFound(req, res) } } if (!checkViewPermission(req, note)) { @@ -313,7 +313,7 @@ function gitlabActionProjects (req, res, note) { id: req.user.id } }).then(function (user) { - if (!user) { return errorNotFound(res) } + if (!user) { return errorNotFound(req, res) } var ret = { baseURL: config.gitlab.baseURL, version: config.gitlab.version } ret.accesstoken = user.accessToken ret.profileid = user.profileid @@ -351,7 +351,7 @@ function showPublishSlide (req, res, next) { if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) { return res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid)) } note.increment('viewcount').then(function (note) { if (!note) { - return errorNotFound(res) + return errorNotFound(req, res) } var body = note.content var extracted = models.Note.extractMeta(body) diff --git a/lib/user/index.js b/lib/user/index.js index 4412713e..979be7af 100644 --- a/lib/user/index.js +++ b/lib/user/index.js @@ -23,7 +23,7 @@ exports.getMe = async (req, res) => { }) if (!user) { - return response.errorNotFound(res) + return response.errorNotFound(req, res) } const profile = models.User.getProfile(user) @@ -47,7 +47,7 @@ exports.deleteUser = async (req, res) => { }) if (!user) { - return response.errorNotFound(res) + return response.errorNotFound(req, res) } if (user.deleteToken !== req.params.token) { From 35a2135b36b86fea968022dd47d409bb439428a7 Mon Sep 17 00:00:00 2001 From: BoHong Li Date: Wed, 26 Feb 2020 11:22:59 +0800 Subject: [PATCH 5/8] refactor: change errorBadRequest function signature to avoid parameter passing error Signed-off-by: BoHong Li --- lib/auth/email/index.js | 8 ++++---- lib/auth/ldap/index.js | 2 +- lib/history/index.js | 10 +++++----- lib/middleware/checkURIValid.js | 2 +- lib/response.js | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/auth/email/index.js b/lib/auth/email/index.js index 56ed17a1..4c130b9f 100644 --- a/lib/auth/email/index.js +++ b/lib/auth/email/index.js @@ -33,8 +33,8 @@ passport.use(new LocalStrategy({ if (config.allowEmailRegister) { emailAuth.post('/register', urlencodedParser, function (req, res, next) { - if (!req.body.email || !req.body.password) return response.errorBadRequest(res) - if (!validator.isEmail(req.body.email)) return response.errorBadRequest(res) + if (!req.body.email || !req.body.password) return response.errorBadRequest(req, res) + if (!validator.isEmail(req.body.email)) return response.errorBadRequest(req, res) models.User.findOrCreate({ where: { email: req.body.email @@ -63,8 +63,8 @@ if (config.allowEmailRegister) { } emailAuth.post('/login', urlencodedParser, function (req, res, next) { - if (!req.body.email || !req.body.password) return response.errorBadRequest(res) - if (!validator.isEmail(req.body.email)) return response.errorBadRequest(res) + if (!req.body.email || !req.body.password) return response.errorBadRequest(req, res) + if (!validator.isEmail(req.body.email)) return response.errorBadRequest(req, res) setReturnToFromReferer(req) passport.authenticate('local', { successReturnToOrRedirect: config.serverURL + '/', diff --git a/lib/auth/ldap/index.js b/lib/auth/ldap/index.js index 2f3b9a11..348c0d76 100644 --- a/lib/auth/ldap/index.js +++ b/lib/auth/ldap/index.js @@ -81,7 +81,7 @@ passport.use(new LDAPStrategy({ })) ldapAuth.post('/auth/ldap', urlencodedParser, function (req, res, next) { - if (!req.body.username || !req.body.password) return response.errorBadRequest(res) + if (!req.body.username || !req.body.password) return response.errorBadRequest(req, res) setReturnToFromReferer(req) passport.authenticate('ldapauth', { successReturnToOrRedirect: config.serverURL + '/', diff --git a/lib/history/index.js b/lib/history/index.js index e38af7e4..139bd9f7 100644 --- a/lib/history/index.js +++ b/lib/history/index.js @@ -131,12 +131,12 @@ function historyPost (req, res) { if (req.isAuthenticated()) { var noteId = req.params.noteId if (!noteId) { - if (typeof req.body['history'] === 'undefined') return response.errorBadRequest(res) + if (typeof req.body['history'] === 'undefined') return response.errorBadRequest(req, res) if (config.debug) { logger.info('SERVER received history from [' + req.user.id + ']: ' + req.body.history) } try { var history = JSON.parse(req.body.history) } catch (err) { - return response.errorBadRequest(res) + return response.errorBadRequest(req, res) } if (Array.isArray(history)) { setHistory(req.user.id, history, function (err, count) { @@ -144,10 +144,10 @@ function historyPost (req, res) { res.end() }) } else { - return response.errorBadRequest(res) + return response.errorBadRequest(req, res) } } else { - if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(res) + if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(req, res) getHistory(req.user.id, function (err, history) { if (err) return response.errorInternalError(res) if (!history) return response.errorNotFound(req, res) @@ -159,7 +159,7 @@ function historyPost (req, res) { res.end() }) } else { - return response.errorBadRequest(res) + return response.errorBadRequest(req, res) } }) } diff --git a/lib/middleware/checkURIValid.js b/lib/middleware/checkURIValid.js index edb61da2..00f2341e 100644 --- a/lib/middleware/checkURIValid.js +++ b/lib/middleware/checkURIValid.js @@ -8,7 +8,7 @@ module.exports = function (req, res, next) { decodeURIComponent(req.path) } catch (err) { logger.error(err) - return response.errorBadRequest(res) + return response.errorBadRequest(req, res) } next() } diff --git a/lib/response.js b/lib/response.js index 8fdb36a3..66c063e6 100644 --- a/lib/response.js +++ b/lib/response.js @@ -41,7 +41,7 @@ function errorNotFound (req, res) { responseError(res, '404', 'Not Found', 'oops.') } -function errorBadRequest (res) { +function errorBadRequest (req, res) { responseError(res, '400', 'Bad Request', 'something not right.') } From 878717799107e2be40dc432fa92aab3029ca0bd2 Mon Sep 17 00:00:00 2001 From: BoHong Li Date: Wed, 26 Feb 2020 11:23:55 +0800 Subject: [PATCH 6/8] refactor: change errorTooLong function signature to avoid parameter passing error Signed-off-by: BoHong Li --- lib/response.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/response.js b/lib/response.js index 66c063e6..1e681558 100644 --- a/lib/response.js +++ b/lib/response.js @@ -45,7 +45,7 @@ function errorBadRequest (req, res) { responseError(res, '400', 'Bad Request', 'something not right.') } -function errorTooLong (res) { +function errorTooLong (req, res) { responseError(res, '413', 'Payload Too Large', 'Shorten your note!') } @@ -91,7 +91,7 @@ function newNote (req, res, next) { var owner = null var body = '' if (req.body && req.body.length > config.documentMaxLength) { - return errorTooLong(res) + return errorTooLong(req, res) } else if (req.body) { body = req.body } From 13ed2e6b44e8fc61e5a07edad2eb8917d1ee8b7e Mon Sep 17 00:00:00 2001 From: BoHong Li Date: Wed, 26 Feb 2020 11:26:01 +0800 Subject: [PATCH 7/8] refactor: change errorInternalError function signature to avoid parameter passing error Signed-off-by: BoHong Li --- lib/auth/email/index.js | 2 +- lib/history/index.js | 14 +++++++------- lib/note/noteActions.js | 6 +++--- lib/response.js | 12 ++++++------ lib/user/index.js | 6 +++--- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/auth/email/index.js b/lib/auth/email/index.js index 4c130b9f..5c748745 100644 --- a/lib/auth/email/index.js +++ b/lib/auth/email/index.js @@ -57,7 +57,7 @@ if (config.allowEmailRegister) { return res.redirect(config.serverURL + '/') }).catch(function (err) { logger.error('auth callback failed: ' + err) - return response.errorInternalError(res) + return response.errorInternalError(req, res) }) }) } diff --git a/lib/history/index.js b/lib/history/index.js index 139bd9f7..3826ce98 100644 --- a/lib/history/index.js +++ b/lib/history/index.js @@ -116,7 +116,7 @@ function parseHistoryToObject (history) { function historyGet (req, res) { if (req.isAuthenticated()) { getHistory(req.user.id, function (err, history) { - if (err) return response.errorInternalError(res) + if (err) return response.errorInternalError(req, res) if (!history) return response.errorNotFound(req, res) res.send({ history: parseHistoryToArray(history) @@ -140,7 +140,7 @@ function historyPost (req, res) { } if (Array.isArray(history)) { setHistory(req.user.id, history, function (err, count) { - if (err) return response.errorInternalError(res) + if (err) return response.errorInternalError(req, res) res.end() }) } else { @@ -149,13 +149,13 @@ function historyPost (req, res) { } else { if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(req, res) getHistory(req.user.id, function (err, history) { - if (err) return response.errorInternalError(res) + if (err) return response.errorInternalError(req, res) if (!history) return response.errorNotFound(req, res) if (!history[noteId]) return response.errorNotFound(req, res) if (req.body.pinned === 'true' || req.body.pinned === 'false') { history[noteId].pinned = (req.body.pinned === 'true') setHistory(req.user.id, history, function (err, count) { - if (err) return response.errorInternalError(res) + if (err) return response.errorInternalError(req, res) res.end() }) } else { @@ -173,16 +173,16 @@ function historyDelete (req, res) { var noteId = req.params.noteId if (!noteId) { setHistory(req.user.id, [], function (err, count) { - if (err) return response.errorInternalError(res) + if (err) return response.errorInternalError(req, res) res.end() }) } else { getHistory(req.user.id, function (err, history) { - if (err) return response.errorInternalError(res) + if (err) return response.errorInternalError(req, res) if (!history) return response.errorNotFound(req, res) delete history[noteId] setHistory(req.user.id, history, function (err, count) { - if (err) return response.errorInternalError(res) + if (err) return response.errorInternalError(req, res) res.end() }) }) diff --git a/lib/note/noteActions.js b/lib/note/noteActions.js index 19ea3b65..90f23f69 100644 --- a/lib/note/noteActions.js +++ b/lib/note/noteActions.js @@ -84,7 +84,7 @@ function actionPDF (req, res, note) { markdownpdf(markdownpdfOptions).from.string(content).to(pdfPath, function () { if (!fs.existsSync(pdfPath)) { logger.error('PDF seems to not be generated as expected. File doesn\'t exist: ' + pdfPath) - return errorInternalError(res) + return errorInternalError(req, res) } const stream = fs.createReadStream(pdfPath) let filename = title @@ -178,7 +178,7 @@ function actionRevision (req, res, note) { Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) { if (err) { logger.error(err) - return errorInternalError(res) + return errorInternalError(req, res) } if (!content) { return errorNotFound(req, res) @@ -196,7 +196,7 @@ function actionRevision (req, res, note) { Revision.getNoteRevisions(note, function (err, data) { if (err) { logger.error(err) - return errorInternalError(res) + return errorInternalError(req, res) } const result = { revision: data diff --git a/lib/response.js b/lib/response.js index 1e681558..3e9cb891 100644 --- a/lib/response.js +++ b/lib/response.js @@ -49,7 +49,7 @@ function errorTooLong (req, res) { responseError(res, '413', 'Payload Too Large', 'Shorten your note!') } -function errorInternalError (res) { +function errorInternalError (req, res) { responseError(res, '500', 'Internal Error', 'wtf.') } @@ -113,7 +113,7 @@ function newNote (req, res, next) { return res.redirect(config.serverURL + '/' + models.Note.encodeNoteId(note.id)) }).catch(function (err) { logger.error(err) - return errorInternalError(res) + return errorInternalError(req, res) }) } @@ -143,7 +143,7 @@ function findNote (req, res, callback, include) { models.Note.parseNoteId(id, function (err, _id) { if (err) { logger.error(err) - return errorInternalError(res) + return errorInternalError(req, res) } models.Note.findOne({ where: { @@ -166,7 +166,7 @@ function findNote (req, res, callback, include) { } }).catch(function (err) { logger.error(err) - return errorInternalError(res) + return errorInternalError(req, res) }) }) } @@ -330,7 +330,7 @@ function gitlabActionProjects (req, res, note) { ) }).catch(function (err) { logger.error('gitlab action projects failed: ' + err) - return errorInternalError(res) + return errorInternalError(req, res) }) } else { return errorForbidden(req, res) @@ -385,7 +385,7 @@ function showPublishSlide (req, res, next) { res.render('slide.ejs', data) }).catch(function (err) { logger.error(err) - return errorInternalError(res) + return errorInternalError(req, res) }) }, include) } diff --git a/lib/user/index.js b/lib/user/index.js index 979be7af..c0687282 100644 --- a/lib/user/index.js +++ b/lib/user/index.js @@ -72,7 +72,7 @@ exports.exportMyData = (req, res) => { archive.pipe(res) archive.on('error', function (err) { logger.error('export user data failed: ' + err) - return response.errorInternalError(res) + return response.errorInternalError(req, res) }) models.User.findOne({ @@ -102,7 +102,7 @@ exports.exportMyData = (req, res) => { callback(null, null) }, function (err) { if (err) { - return response.errorInternalError(res) + return response.errorInternalError(req, res) } archive.finalize() @@ -110,7 +110,7 @@ exports.exportMyData = (req, res) => { }) }).catch(function (err) { logger.error('export user data failed: ' + err) - return response.errorInternalError(res) + return response.errorInternalError(req, res) }) } From da3fd00577db6ca53151a6cbf2e4a44e32a73d7b Mon Sep 17 00:00:00 2001 From: BoHong Li Date: Wed, 26 Feb 2020 11:26:56 +0800 Subject: [PATCH 8/8] refactor: change errorServiceUnavailable function signature to avoid parameter passing error Signed-off-by: BoHong Li --- lib/middleware/tooBusy.js | 2 +- lib/response.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/middleware/tooBusy.js b/lib/middleware/tooBusy.js index 13642c6c..65fa4b7d 100644 --- a/lib/middleware/tooBusy.js +++ b/lib/middleware/tooBusy.js @@ -9,7 +9,7 @@ toobusy.maxLag(config.responseMaxLag) module.exports = function (req, res, next) { if (toobusy()) { - response.errorServiceUnavailable(res) + response.errorServiceUnavailable(req, res) } else { next() } diff --git a/lib/response.js b/lib/response.js index 3e9cb891..0de8f9bc 100644 --- a/lib/response.js +++ b/lib/response.js @@ -53,7 +53,7 @@ function errorInternalError (req, res) { responseError(res, '500', 'Internal Error', 'wtf.') } -function errorServiceUnavailable (res) { +function errorServiceUnavailable (req, res) { res.status(503).send('I\'m busy right now, try again later.') }