diff --git a/tailbone/views/products.py b/tailbone/views/products.py index d929a589..2642b4db 100644 --- a/tailbone/views/products.py +++ b/tailbone/views/products.py @@ -40,7 +40,7 @@ from rattail.db import model, api, auth, Session as RattailSession from rattail.gpc import GPC from rattail.threads import Thread from rattail.exceptions import LabelPrintingError -from rattail.util import load_object, pretty_quantity, OrderedDict +from rattail.util import load_object, pretty_quantity, OrderedDict, simple_error from rattail.batch import get_batch_handler from rattail.time import localtime, make_utc @@ -1693,19 +1693,34 @@ class ProductView(MasterView): user = session.query(model.User).get(user_uuid) assert user params['created_by'] = user - batch = handler.make_batch(session, **params) - batch.products = products.with_session(session).all() - handler.do_populate(batch, user, progress=progress) + try: + batch = handler.make_batch(session, **params) + batch.products = products.with_session(session).all() + handler.do_populate(batch, user, progress=progress) - session.commit() - session.refresh(batch) - session.close() + except Exception as error: + session.rollback() + log.exception("failed to make '%s' batch with params: %s", + handler.batch_key, params) + session.close() + if progress: + progress.session.load() + progress.session['error'] = True + progress.session['error_msg'] = "Failed to make '{}' batch: {}".format( + handler.batch_key, simple_error(error)) + progress.session.save() - progress.session.load() - progress.session['complete'] = True - progress.session['success_url'] = self.get_batch_view_url(batch) - progress.session['success_msg'] = 'Batch has been created: {}'.format(batch) - progress.session.save() + else: + session.commit() + session.refresh(batch) + session.close() + + if progress: + progress.session.load() + progress.session['complete'] = True + progress.session['success_url'] = self.get_batch_view_url(batch) + progress.session['success_msg'] = 'Batch has been created: {}'.format(batch) + progress.session.save() def get_batch_view_url(self, batch): if batch.batch_key == 'labels':