From 8364b429308c0a44667b7429fb8b25cb2ba84a6e Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Wed, 13 Mar 2024 12:27:09 +0100 Subject: [PATCH] product_print_zpl_barcode: add timeout and improve error message --- .../wizard/product_print_zpl_barcode.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/product_print_zpl_barcode/wizard/product_print_zpl_barcode.py b/product_print_zpl_barcode/wizard/product_print_zpl_barcode.py index 4c2f053..11d81a3 100644 --- a/product_print_zpl_barcode/wizard/product_print_zpl_barcode.py +++ b/product_print_zpl_barcode/wizard/product_print_zpl_barcode.py @@ -13,6 +13,8 @@ import ipaddress import logging logger = logging.getLogger(__name__) +TIMEOUT = 5 +PRINTER_PORT = 9100 class ProductPrintZplBarcode(models.TransientModel): @@ -184,10 +186,13 @@ class ProductPrintZplBarcode(models.TransientModel): else: # IPv4 socket_inet = socket.AF_INET with socket.socket(socket_inet, socket.SOCK_STREAM) as s: + s.settimeout(TIMEOUT) try: - s.connect((str(ip), 9100)) + s.connect((str(ip), PRINTER_PORT)) except Exception as e: - raise UserError(str(e)) + raise UserError(_( + "Cannot connect to ZPL printer on %(ip)s. Error: %(error)s", + ip=ip, error=e)) zpl_file_bytes = base64.decodebytes(self.zpl_file) s.send(zpl_file_bytes) s.close()