Handle errors when printing reports (#5360)

- Re-throw as a ValidationError
- Results in a 400 error, not a 500
This commit is contained in:
Oliver 2023-07-28 13:47:46 +10:00 committed by GitHub
parent 7e7d4d01a2
commit 5f3d3b28b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,6 +18,7 @@ import InvenTree.helpers
import order.models
import part.models
from InvenTree.api import MetadataView
from InvenTree.exceptions import log_error
from InvenTree.filters import InvenTreeSearchFilter
from InvenTree.mixins import ListAPI, RetrieveAPI, RetrieveUpdateDestroyAPI
from stock.models import StockItem, StockItemAttachment, StockLocation
@ -183,6 +184,7 @@ class ReportPrintMixin:
# Start with a default report name
report_name = "report.pdf"
try:
# Merge one or more PDF files into a single download
for item in items_to_print:
report = self.get_object()
@ -256,6 +258,17 @@ class ReportPrintMixin:
inline=inline,
)
except Exception as exc:
# Log the exception to the database
log_error(request.path)
# Re-throw the exception to the client as a DRF exception
raise ValidationError({
'error': 'Report printing failed',
'detail': str(exc),
'path': request.path,
})
def get(self, request, *args, **kwargs):
"""Default implementation of GET for a print endpoint.