export const errorHandler = (req: any, res: any, next: any) => { const errors = validationResult(req); if (!errors.isEmpty()) { res.status(ERROR_CODES.ERROR_CODE_GENERIC).json({message: errors.array()[0].msg}); } else { next(); } };
export function handleValidationResult(req: Request, res: Response, next: NextFunction): void { const errors = validationResult(req); if (false === errors.isEmpty()) { res.status(422).json({ errors: errors.array() }); } else { next(); } };
function areValidationErrors (req: express.Request, res: express.Response) { const errors = validationResult(req) if (!errors.isEmpty()) { logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors.mapped() }) res.status(400).json({ errors: errors.mapped() }) return true } return false }
router.post('/:id', CREATE_ORDER_VALIDATIONS, async (req: Request, res: Response) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(422).json({ errors: errors.mapped() }); } // matchedData returns only the subset of data validated by the middleware const orderData = matchedData(req); let detailsObj = orderData.details || {}; let details: Array< Pick<OrderDetail, 'id' | 'productid' | 'quantity' | 'unitprice' | 'discount'> > = normalizeOrderDetails(detailsObj); try { let order = await updateOrder(req.params.id, orderData as any, details); // * update the data res.redirect(`/orders/${order.id}`); } catch (e) { res.status(500); res.send(e.toString()); } });