From b6d5fe7013ec1bdaff78840e33170347d3c39e29 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Wed, 4 Oct 2023 19:08:17 -0700 Subject: [PATCH] Improve rar error messages --- comicapi/archivers/rar.py | 41 ++++++++++++++++++++++++++------------- comicapi/comicarchive.py | 31 ++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/comicapi/archivers/rar.py b/comicapi/archivers/rar.py index 1dc2df8..4c947ed 100644 --- a/comicapi/archivers/rar.py +++ b/comicapi/archivers/rar.py @@ -64,18 +64,25 @@ class RarArchiver(Archiver): f"-z{tmp_file}", str(self.path), ] - subprocess.run( + result = subprocess.run( proc_args, startupinfo=self.startupinfo, - stdout=subprocess.DEVNULL, stdin=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - check=True, + capture_output=True, + encoding="utf-8", ) + if result.returncode != 0: + logger.error( + "Error writing comment to rar archive [exitcode: %d]: %s :: %s", + result.returncode, + self.path, + result.stderr, + ) + return False if platform.system() == "Darwin": time.sleep(1) - except (subprocess.CalledProcessError, OSError) as e: + except OSError as e: logger.exception("Error writing comment to rar archive [%s]: %s", e, self.path) return False else: @@ -134,9 +141,9 @@ class RarArchiver(Archiver): result = subprocess.run( [self.exe, "d", "-c-", self.path, archive_file], startupinfo=self.startupinfo, - stdout=subprocess.DEVNULL, stdin=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, + capture_output=True, + encoding="utf-8", ) if platform.system() == "Darwin": @@ -164,15 +171,18 @@ class RarArchiver(Archiver): [self.exe, "a", f"-si{archive_name}", f"-ap{archive_parent}", "-c-", "-ep", self.path], input=data, startupinfo=self.startupinfo, - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, + capture_output=True, ) if platform.system() == "Darwin": time.sleep(1) if result.returncode != 0: logger.error( - "Error writing rar archive [exitcode: %d]: %s :: %s", result.returncode, self.path, archive_file + "Error writing rar archive [exitcode: %d]: %s :: %s :: %s", + result.returncode, + self.path, + archive_file, + result.stderr, ) return False else: @@ -219,12 +229,17 @@ class RarArchiver(Archiver): [self.exe, "a", "-r", "-c-", str(rar_path.absolute()), "."], cwd=rar_cwd.absolute(), startupinfo=self.startupinfo, - stdout=subprocess.DEVNULL, stdin=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, + capture_output=True, + encoding="utf-8", ) if result.returncode != 0: - logger.error("Error while copying to rar archive [exitcode: %d]: %s", result.returncode, self.path) + logger.error( + "Error while copying to rar archive [exitcode: %d]: %s: %s", + result.returncode, + self.path, + result.stderr, + ) return False self.path.unlink(missing_ok=True) diff --git a/comicapi/comicarchive.py b/comicapi/comicarchive.py index 87a234d..2436491 100644 --- a/comicapi/comicarchive.py +++ b/comicapi/comicarchive.py @@ -20,6 +20,7 @@ import os import pathlib import shutil import sys +import traceback from typing import cast from comicapi import filenamelexer, filenameparser, utils @@ -194,8 +195,11 @@ class ComicArchive: if filename: try: image_data = self.archiver.read_file(filename) or b"" - except Exception: - logger.error("Error reading in page %d. Substituting logo page.", index) + except Exception as e: + tb = traceback.extract_tb(e.__traceback__) + logger.error( + "%s:%s: Error reading in page %d. Substituting logo page.", tb[1].filename, tb[1].lineno, index + ) image_data = ComicArchive.logo_data return image_data @@ -325,7 +329,8 @@ class ComicArchive: self.reset_cache() return write_success except Exception as e: - logger.error("Error saving CBI! for %s: %s", self.path, e) + tb = traceback.extract_tb(e.__traceback__) + logger.error("%s:%s: Error saving CBI! for %s: %s", tb[1].filename, tb[1].lineno, self.path, e) return False @@ -365,7 +370,8 @@ class ComicArchive: try: raw_cix = self.archiver.read_file(self.ci_xml_filename) or b"" except Exception as e: - logger.error("Error reading in raw CIX! for %s: %s", self.path, e) + tb = traceback.extract_tb(e.__traceback__) + logger.error("%s:%s: Error reading in raw CIX! for %s: %s", tb[1].filename, tb[1].lineno, self.path, e) raw_cix = b"" return raw_cix @@ -382,7 +388,8 @@ class ComicArchive: self.reset_cache() return write_success except Exception as e: - logger.error("Error saving CIX! for %s: %s", self.path, e) + tb = traceback.extract_tb(e.__traceback__) + logger.error("%s:%s: Error saving CIX! for %s: %s", tb[1].filename, tb[1].lineno, self.path, e) return False @@ -440,7 +447,10 @@ class ComicArchive: if raw_bytes: raw_comet = raw_bytes.decode("utf-8") except OSError as e: - logger.exception("Error reading in raw CoMet!: %s", e) + tb = traceback.extract_tb(e.__traceback__) + logger.error( + "%s:%s: Error reading in raw CoMet! for %s: %s", tb[1].filename, tb[1].lineno, self.path, e + ) return raw_comet def write_comet(self, metadata: GenericMetadata) -> bool: @@ -490,7 +500,14 @@ class ComicArchive: if d: data = d.decode("utf-8") except Exception as e: - logger.warning("Error reading in Comet XML for validation! from %s: %s", self.path, e) + tb = traceback.extract_tb(e.__traceback__) + logger.warning( + "%s:%s: Error reading in Comet XML for validation! from %s: %s", + tb[1].filename, + tb[1].lineno, + self.path, + e, + ) if CoMet().validate_string(data): # since we found it, save it! self.comet_filename = n