improved logging and error handling
This commit is contained in:
@@ -21,7 +21,7 @@ var catalogueHost = process.env.CATALOGUE_HOST || 'catalogue'
|
||||
|
||||
const logger = pino({
|
||||
level: 'info',
|
||||
prettyPrint: false,
|
||||
prettyPrint: true,
|
||||
useLevelLabels: true
|
||||
});
|
||||
const expLogger = expPino({
|
||||
@@ -94,8 +94,12 @@ app.get('/rename/:from/:to', (req, res) => {
|
||||
res.status(404).send('cart not found');
|
||||
} else {
|
||||
var cart = JSON.parse(data);
|
||||
saveCart(req.params.to, cart);
|
||||
saveCart(req.params.to, cart).then((data) => {
|
||||
res.json(cart);
|
||||
}).catch((err) => {
|
||||
req.log.error(err);
|
||||
res.status(500).send(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -154,8 +158,12 @@ app.get('/add/:id/:sku/:qty', (req, res) => {
|
||||
cart.tax = calcTax(cart.total);
|
||||
|
||||
// save the new cart
|
||||
saveCart(req.params.id, cart);
|
||||
saveCart(req.params.id, cart).then((data) => {
|
||||
res.json(cart);
|
||||
}).catch((err) => {
|
||||
req.log.error(err);
|
||||
res.status(500).send(err);
|
||||
});
|
||||
}
|
||||
});
|
||||
}).catch((err) => {
|
||||
@@ -206,8 +214,12 @@ app.get('/update/:id/:sku/:qty', (req, res) => {
|
||||
cart.total = calcTotal(cart.items);
|
||||
// work out tax
|
||||
cart.tax = calcTax(cart.total);
|
||||
saveCart(req.params.id, cart);
|
||||
saveCart(req.params.id, cart).then((data) => {
|
||||
res.json(cart);
|
||||
}).catch((err) => {
|
||||
req.log.error(err);
|
||||
res.status(500).send(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -258,8 +270,12 @@ app.post('/shipping/:id', (req, res) => {
|
||||
cart.tax = calcTax(cart.total);
|
||||
|
||||
// save the updated cart
|
||||
saveCart(req.params.id, cart);
|
||||
saveCart(req.params.id, cart).then((data) => {
|
||||
res.json(cart);
|
||||
}).catch((err) => {
|
||||
req.log.error(err);
|
||||
res.status(500).send(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -319,11 +335,15 @@ function getProduct(sku) {
|
||||
|
||||
function saveCart(id, cart) {
|
||||
logger.info('saving cart', cart);
|
||||
return new Promise((resolve, reject) => {
|
||||
redisClient.setex(id, 3600, JSON.stringify(cart), (err, data) => {
|
||||
if(err) {
|
||||
logger.error('saveCart ERROR', err);
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// connect to Redis
|
||||
|
@@ -28,23 +28,60 @@ def pay(id):
|
||||
cart = request.get_json()
|
||||
app.logger.info(cart)
|
||||
|
||||
# dummy call to Paypal, hope they dont object
|
||||
anonymous_user = True
|
||||
|
||||
# check user exists
|
||||
try:
|
||||
req = requests.get('http://{user}:8080/check/{id}'.format(user=USER, id=id))
|
||||
except requests.exceptions.RequestException as err:
|
||||
app.logger.error(err)
|
||||
return str(err), 500
|
||||
if req.status_code == 200:
|
||||
anonymous_user = False
|
||||
|
||||
# check that the cart is valid
|
||||
has_shipping = False
|
||||
for item in cart.get('items'):
|
||||
if item.get('sku') == 'SHIP':
|
||||
has_shipping = True
|
||||
|
||||
if int(cart.get('total')) == 0 or has_shipping == False:
|
||||
return 'cart not valid', 400
|
||||
|
||||
# dummy call to payment gateway, hope they dont object
|
||||
try:
|
||||
req = requests.get(PAYMENT_GATEWAY)
|
||||
app.logger.info('{} returned {}'.format(PAYMENT_GATEWAY, req.status_code))
|
||||
except requests.exceptions.RequestException as err:
|
||||
app.logger.error(err)
|
||||
return str(err), 500
|
||||
if req.status_code != 200:
|
||||
return 'payment error', req.status_code
|
||||
|
||||
# Generate order id
|
||||
orderid = str(uuid.uuid4())
|
||||
queueOrder({ 'orderid': orderid, 'user': id, 'cart': cart })
|
||||
|
||||
# add to history
|
||||
# add to order history
|
||||
if not anonymous_user:
|
||||
try:
|
||||
req = requests.post('http://{user}:8080/order/{id}'.format(user=USER, id=id),
|
||||
data=json.dumps({'orderid': orderid, 'cart': cart}),
|
||||
headers={'Content-Type': 'application/json'})
|
||||
app.logger.info('order history returned {}'.format(req.status_code))
|
||||
except requests.exceptions.RequestException as err:
|
||||
app.logger.error(err)
|
||||
return str(err), 500
|
||||
|
||||
# delete cart
|
||||
try:
|
||||
req = requests.delete('http://{cart}:8080/cart/{id}'.format(cart=CART, id=id));
|
||||
app.logger.info('cart delete returned {}'.format(req.status_code))
|
||||
except requests.exceptions.RequestException as err:
|
||||
app.logger.error(err)
|
||||
return str(err), 500
|
||||
if req.status_code != 200:
|
||||
return 'order history update error', req.status_code
|
||||
|
||||
return jsonify({ 'orderid': orderid })
|
||||
|
||||
@@ -75,6 +112,7 @@ def queueOrder(order):
|
||||
|
||||
publisher.publish(order, headers)
|
||||
|
||||
|
||||
# RabbitMQ
|
||||
publisher = Publisher(app.logger)
|
||||
|
||||
@@ -85,4 +123,5 @@ if __name__ == "__main__":
|
||||
app.logger.setLevel(logging.INFO)
|
||||
app.logger.info('Payment gateway {}'.format(PAYMENT_GATEWAY))
|
||||
port = int(os.getenv("SHOP_PAYMENT_PORT", "8080"))
|
||||
app.logger.info('Starting on port {}'.format(port))
|
||||
app.run(host='0.0.0.0', port=port)
|
||||
|
@@ -54,6 +54,7 @@ app.get('/health', (req, res) => {
|
||||
|
||||
// use REDIS INCR to track anonymous users
|
||||
app.get('/uniqueid', (req, res) => {
|
||||
req.log.error('Unique ID test');
|
||||
// get number from Redis
|
||||
redisClient.incr('anonymous-counter', (err, r) => {
|
||||
if(!err) {
|
||||
@@ -67,6 +68,25 @@ app.get('/uniqueid', (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
// check user exists
|
||||
app.get('/check/:id', (req, res) => {
|
||||
if(mongoConnected) {
|
||||
usersCollection.findOne({name: req.params.id}).then((user) => {
|
||||
if(user) {
|
||||
res.send('OK');
|
||||
} else {
|
||||
res.status(404).send('user not found');
|
||||
}
|
||||
}).catch((e) => {
|
||||
req.log.error(e);
|
||||
res.send(500).send(e);
|
||||
});
|
||||
} else {
|
||||
req.log.error('database not available');
|
||||
res.status(500).send('database not available');
|
||||
}
|
||||
});
|
||||
|
||||
// return all users for debugging only
|
||||
app.get('/users', (req, res) => {
|
||||
if(mongoConnected) {
|
||||
|
Reference in New Issue
Block a user