Gracefully catch IntegrityError when a database constraint is violated

This commit is contained in:
BBaoVanC 2020-10-08 10:10:11 -05:00
parent 91c2017759
commit ba9bef5c8f
No known key found for this signature in database
GPG Key ID: 6D74C8B0E7D791C2

View File

@ -10,6 +10,7 @@ from flask import Flask, jsonify, request
from flask_api import status
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.exc import IntegrityError
from libdb import Session, Location, ProductQuantity, Product, Brand, Unit, Purchase, Use
@ -258,10 +259,13 @@ def api_create_location():
newlocation = Location(name=locname, description=locdesc)
session.add(newlocation)
print("added newlocation")
session.commit()
print("committed")
return jsonify({'api_endpoints': f'/api/v1/location/name/{locname}'})
try:
session.commit()
print("committed")
return jsonify({'api_endpoints': f'/api/v1/location/name/{locname}'})
except IntegrityError:
session.rollback()
return jsonify({'error': 'INTEGRITY_ERROR'})
@app.route("/api/v1/create_brand", methods=["POST"])
@ -280,10 +284,13 @@ def api_create_brand():
newbrand = Brand(name=brandname, description=branddesc)
session.add(newbrand)
print("added newbrand")
session.commit()
print("committed")
return jsonify({'api_endpoints': [f'/api/v1/brand/name/{brandname}']})
try:
session.commit()
print("committed")
return jsonify({'api_endpoints': [f'/api/v1/brand/name/{brandname}']})
except IntegrityError:
session.rollback()
return jsonify({'error': 'INTEGRITY_ERROR'})
@app.route("/api/v1/create_product", methods=["POST"])
@ -305,11 +312,14 @@ def api_create_product():
description=desc)
session.add(newproduct)
print("added newproduct")
session.commit()
print("committed")
return jsonify({'api_endpoints': [f'/api/v1/product/upc/{request.form["upc"]}',
f'/api/v1/product/name/{request.form["name"]}']})
try:
session.commit()
print("committed")
return jsonify({'api_endpoints': [f'/api/v1/product/upc/{request.form["upc"]}',
f'/api/v1/product/name/{request.form["name"]}']})
except IntegrityError:
session.rollback()
return jsonify({'error': 'INTEGRITY_ERROR'})
@app.route("/api/v1/create_unit", methods=["POST"])
@ -328,10 +338,13 @@ def api_create_unit():
newunit = Unit(name=unitname, description=unitdesc)
session.add(newunit)
print("added newunit")
session.commit()
print("committed")
return jsonify({'api_endpoints': [f'/api/v1/unit/name/{unitname}']})
try:
session.commit()
print("committed")
return jsonify({'api_endpoints': [f'/api/v1/unit/name/{unitname}']})
except IntegrityError:
session.rollback()
return jsonify({'error': 'INTEGRITY_ERROR'})
@app.route("/api/v1/create_purchase", methods=["POST"])
@ -347,10 +360,13 @@ def api_create_purchase():
date=request.form['date'], location=request.form['location'])
session.add(newpurchase)
print("added newpurchase")
session.commit()
print("committed")
return jsonify({'api_endpoints': [f'/api/v1/purchase/id/{newpurchase.id}']})
try:
session.commit()
print("committed")
return jsonify({'api_endpoints': [f'/api/v1/purchase/id/{newpurchase.id}']})
except IntegrityError:
session.rollback()
return jsonify({'error': 'INTEGRITY_ERROR'})
@app.route("/api/v1/create_use", methods=["POST"])
@ -366,10 +382,13 @@ def api_create_use():
date=request.form['date'], location=request.form['location'])
session.add(newuse)
print("added newuse")
session.commit()
print("committed")
return jsonify({'api_endpoints': [f'/api/v1/use/id/{newuse.id}']})
try:
session.commit()
print("committed")
return jsonify({'api_endpoints': [f'/api/v1/use/id/{newuse.id}']})
except IntegrityError:
session.rollback()
return jsonify({'error': 'INTEGRITY_ERROR'})
if __name__ == "__main__":