From 8b62f7b2c09958c7e8d4eabcaadae3afc5e5dd44 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 8 Feb 2024 11:37:06 +1100 Subject: [PATCH] Label printing errors (#6450) * Enhance error handling for label printing * Better feedback in forms * Raise error to user - no need to log --- InvenTree/label/api.py | 13 ++++++++++--- InvenTree/plugin/base/label/mixins.py | 22 ++++++++++++++++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/InvenTree/label/api.py b/InvenTree/label/api.py index 694e3b9d90..9c009eac62 100644 --- a/InvenTree/label/api.py +++ b/InvenTree/label/api.py @@ -4,6 +4,7 @@ from django.core.exceptions import FieldError, ValidationError from django.http import JsonResponse from django.urls import include, path, re_path from django.utils.decorators import method_decorator +from django.utils.translation import gettext_lazy as _ from django.views.decorators.cache import cache_page, never_cache from django_filters.rest_framework import DjangoFilterBackend @@ -13,6 +14,7 @@ from rest_framework.request import clone_request import build.models import common.models +import InvenTree.exceptions import InvenTree.helpers import label.models import label.serializers @@ -232,9 +234,14 @@ class LabelPrintMixin(LabelFilterMixin): # At this point, we offload the label(s) to the selected plugin. # The plugin is responsible for handling the request and returning a response. - result = plugin.print_labels( - label, items_to_print, request, printing_options=request.data - ) + try: + result = plugin.print_labels( + label, items_to_print, request, printing_options=request.data + ) + except ValidationError as e: + raise (e) + except Exception as e: + raise ValidationError([_('Error printing label'), str(e)]) if isinstance(result, JsonResponse): result['plugin'] = plugin.plugin_slug() diff --git a/InvenTree/plugin/base/label/mixins.py b/InvenTree/plugin/base/label/mixins.py index 975cf66985..91d7d4c832 100644 --- a/InvenTree/plugin/base/label/mixins.py +++ b/InvenTree/plugin/base/label/mixins.py @@ -2,13 +2,16 @@ from typing import Union +from django.core.exceptions import ValidationError from django.http import JsonResponse +from django.utils.translation import gettext_lazy as _ import pdf2image from rest_framework import serializers from rest_framework.request import Request from common.models import InvenTreeSetting +from InvenTree.exceptions import log_error from InvenTree.tasks import offload_task from label.models import LabelTemplate from plugin.base.label import label as plugin_label @@ -47,7 +50,11 @@ class LabelPrintingMixin: label: The LabelTemplate object to render request: The HTTP request object which triggered this print job """ - return label.render(request) + try: + return label.render(request) + except Exception as e: + log_error('label.render_to_pdf') + raise ValidationError(_('Error rendering label to PDF')) def render_to_html(self, label: LabelTemplate, request, **kwargs): """Render this label to HTML format. @@ -56,7 +63,11 @@ class LabelPrintingMixin: label: The LabelTemplate object to render request: The HTTP request object which triggered this print job """ - return label.render_as_string(request) + try: + return label.render_as_string(request) + except Exception as e: + log_error('label.render_to_html') + raise ValidationError(_('Error rendering label to HTML')) def render_to_png(self, label: LabelTemplate, request=None, **kwargs): """Render this label to PNG format.""" @@ -71,8 +82,11 @@ class LabelPrintingMixin: dpi = kwargs.get('dpi', InvenTreeSetting.get_setting('LABEL_DPI', 300)) # Convert to png data - png = pdf2image.convert_from_bytes(pdf_data, dpi=dpi)[0] - return png + try: + return pdf2image.convert_from_bytes(pdf_data, dpi=dpi)[0] + except Exception as e: + log_error('label.render_to_png') + raise ValidationError(_('Error rendering label to PNG')) def print_labels( self,