Label printing errors (#6450)

* Enhance error handling for label printing

* Better feedback in forms

* Raise error to user - no need to log
This commit is contained in:
Oliver 2024-02-08 11:37:06 +11:00 committed by GitHub
parent 4e58f0a3c7
commit 8b62f7b2c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 7 deletions

View File

@ -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.
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()

View File

@ -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
"""
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
"""
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,