Created
July 3, 2019 03:46
-
-
Save taqiabdulaziz/21a6901845675f8c797f25148814bb11 to your computer and use it in GitHub Desktop.
Bring your error to client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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