I have a nodejs server with a controller and a service. I am trying to propogate the error received from the server file and pass it to the frontend for to display. The issue is, I am unable to receive the error in the frontend and get an internal server error instead. I have tried various methods to correct this but it didnt work. Can someone explain what i am doing wrong?
my controller is:
function moveTable(req, res, next) {
const payload = jwtDecode(req.headers.authorization.split(' ')[1]);
authService.getAuthToken(payload.sub)
.then(token => cloverOrderService.moveTable(req.body, token.merchantId,token.cloverToken))
.then(data => res.send(data))
.catch(err => next(err));
}
my service code is:
async function moveTable(data, merchantId, cloverToken) {
let newMain = await retryOrderWithDifferentName(data.updatedTableName, merchantId, cloverToken, images, true);
if(!newMain.order) {
return {
updatedMain: main.order ? await updateOrder(main, merchantId, cloverToken) : '';
}
} else {
throw new Error('Order Already Exists For Table ' + data.updatedTableName);
}
}
in the .catch in the controller to send a custom error code and response as below but the response code sends but I don’t receive the custom message:
.catch(err => res.status(404).json({error :'No such user exists' +err}));