From de8751b86c6dffb5c32ca9e01d01139ef8de18b6 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Thu, 11 Apr 2024 14:14:27 -0500 Subject: [PATCH] Try to return JSON error when receiving API call fails although in my testing, the error still got raised somehow in the tweens or something? client then sees it as a 500 response and gets no JSON --- tailbone/api/batch/receiving.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tailbone/api/batch/receiving.py b/tailbone/api/batch/receiving.py index f8ce4a33..daa4290f 100644 --- a/tailbone/api/batch/receiving.py +++ b/tailbone/api/batch/receiving.py @@ -2,7 +2,7 @@ ################################################################################ # # Rattail -- Retail Software Framework -# Copyright © 2010-2023 Lance Edgar +# Copyright © 2010-2024 Lance Edgar # # This file is part of Rattail. # @@ -27,6 +27,7 @@ Tailbone Web API - Receiving Batches import logging import humanize +import sqlalchemy as sa from rattail.db import model from rattail.util import pretty_quantity @@ -440,9 +441,17 @@ class ReceivingBatchRowViews(APIBatchRowView): # handler takes care of the row receiving logic for us kwargs = dict(form.validated) del kwargs['row'] - self.batch_handler.receive_row(row, **kwargs) + try: + self.batch_handler.receive_row(row, **kwargs) + self.Session.flush() + except Exception as error: + log.warning("receive() failed", exc_info=True) + if isinstance(error, sa.exc.DataError) and hasattr(error, 'orig'): + error = str(error.orig) + else: + error = str(error) + return {'error': error} - self.Session.flush() return self._get(obj=row) @classmethod