Skip to content

Instantly share code, notes, and snippets.

@taqiabdulaziz
Created July 3, 2019 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taqiabdulaziz/21a6901845675f8c797f25148814bb11 to your computer and use it in GitHub Desktop.
Save taqiabdulaziz/21a6901845675f8c797f25148814bb11 to your computer and use it in GitHub Desktop.
Bring your error to client
const logger = require('./logger');
const { httpStatus } = require('../configs/codes');
const { errorResponse } = require('./response');
const wrap = fn => (req, res) => {
const idRequest = req.request_id;
const {
baseUrl,
originalUrl,
method,
query,
params,
body,
headers
} = req;
const data = {
headers,
query,
params,
body
};
delete data.headers.cookie;
delete data.headers.accept;
delete data.headers.host;
delete data.headers.connection;
delete data.headers.authorization;
if (Object.entries(query).length === 0 && query.constructor === Object) delete data.query;
if (Object.entries(params).length === 0 && params.constructor === Object) delete data.params;
if (Object.entries(body).length === 0 && body.constructor === Object) delete data.body;
logger.info({
requestId: idRequest,
data,
mep: 'REQUEST',
description: `${method} REQUEST TO: ${originalUrl || baseUrl}`,
toService: 'BOOST-SVC-MERCHANT'
});
const result = fn(req, res).catch((error) => {
const idRequest = req.request_id;
return res.status(error.code || httpStatus.badRequest).json(errorResponse(idRequest, error)).end();
});
return result;
}
module.exports = {
wrap
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment