diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js index 86aee6ea47..62ee51106c 100644 --- a/src/controllers/helpers.js +++ b/src/controllers/helpers.js @@ -1,5 +1,6 @@ 'use strict'; +const colors = require('colors/safe'); const nconf = require('nconf'); const validator = require('validator'); const querystring = require('querystring'); @@ -361,21 +362,9 @@ helpers.formatApiResponse = async (statusCode, res, payload) => { // Update status code based on some common error codes switch (payload.message) { - case '[[error:user-banned]]': { - const [reason, expiry] = await Promise.all([ - user.bans.getReason(res.req.uid), - user.getUserField(res.req.uid, 'banned:expire'), - ]); - - response.reason = reason; - if (expiry) { - Object.assign(response, { - expiry, - expiryISO: new Date(expiry).toISOString(), - expiryLocaleString: new Date(expiry).toLocaleString(), - }); - } - } // intentional fall through + case '[[error:user-banned]]': + Object.assign(response, await generateBannedResponse(res)); + // intentional fall through case '[[error:no-privileges]]': statusCode = 403; @@ -391,6 +380,7 @@ helpers.formatApiResponse = async (statusCode, res, payload) => { if (global.env === 'development') { returnPayload.stack = payload.stack; + process.stdout.write(`[${colors.yellow('api')}] Exception caught, error with stack trace follows:\n`); process.stdout.write(payload.stack); } res.status(statusCode).json(returnPayload); @@ -400,6 +390,25 @@ helpers.formatApiResponse = async (statusCode, res, payload) => { } }; +async function generateBannedResponse(res) { + const response = {}; + const [reason, expiry] = await Promise.all([ + user.bans.getReason(res.req.uid), + user.getUserField(res.req.uid, 'banned:expire'), + ]); + + response.reason = reason; + if (expiry) { + Object.assign(response, { + expiry, + expiryISO: new Date(expiry).toISOString(), + expiryLocaleString: new Date(expiry).toLocaleString(), + }); + } + + return response; +} + helpers.generateError = (statusCode, message) => { var payload = { status: {