From ee65d08d8111fbbce5e2b2a83fe5f458906fce41 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 19 Mar 2021 10:38:56 -0500 Subject: [PATCH] Catch and show error, if one happens when making batch from product query --- tailbone/views/products.py | 39 ++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) 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':