const unknownError = { error : 'UNKNOWN_ERROR' } const unknownResponseFormat = 'UNKNOWN_RESPONSE_FORMAT'; function handlingError (funct, success, error) { try { success(funct()); } catch (e) { // console.log('error', e); error(e.name === 'ApiError' ? { error : e.message } : unknownError); } } module.exports = async (method, req, res) => { if (req.query.response_format === 'json') { handlingError( () => ({ response : method(req, res) }), (...args) => res.send(...args), (...data) => res.status(400).json(...data) ); } else { res.status(400).send(unknownResponseFormat); } }