Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 635c96a

Browse files
committed
better exception handling
1 parent de08d17 commit 635c96a

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/documents/classifier.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class IncompatibleClassifierVersionError(Exception):
1313
pass
1414

1515

16+
class ClassifierModelCorruptError(Exception):
17+
pass
18+
19+
1620
logger = logging.getLogger("paperless.classifier")
1721

1822

@@ -34,9 +38,8 @@ def load_classifier():
3438
try:
3539
classifier.load()
3640

37-
except (EOFError,
38-
IncompatibleClassifierVersionError,
39-
pickle.UnpicklingError):
41+
except (ClassifierModelCorruptError,
42+
IncompatibleClassifierVersionError):
4043
# there's something wrong with the model file.
4144
logger.exception(
4245
f"Unrecoverable error while loading document "
@@ -46,7 +49,12 @@ def load_classifier():
4649
classifier = None
4750
except OSError:
4851
logger.exception(
49-
f"Error while loading document classification model"
52+
f"IO error while loading document classification model"
53+
)
54+
classifier = None
55+
except Exception:
56+
logger.exception(
57+
f"Unknown error while loading document classification model"
5058
)
5159
classifier = None
5260

@@ -76,13 +84,16 @@ def load(self):
7684
raise IncompatibleClassifierVersionError(
7785
"Cannor load classifier, incompatible versions.")
7886
else:
79-
self.data_hash = pickle.load(f)
80-
self.data_vectorizer = pickle.load(f)
81-
self.tags_binarizer = pickle.load(f)
82-
83-
self.tags_classifier = pickle.load(f)
84-
self.correspondent_classifier = pickle.load(f)
85-
self.document_type_classifier = pickle.load(f)
87+
try:
88+
self.data_hash = pickle.load(f)
89+
self.data_vectorizer = pickle.load(f)
90+
self.tags_binarizer = pickle.load(f)
91+
92+
self.tags_classifier = pickle.load(f)
93+
self.correspondent_classifier = pickle.load(f)
94+
self.document_type_classifier = pickle.load(f)
95+
except Exception:
96+
raise ClassifierModelCorruptError()
8697

8798
def save(self):
8899
with open(settings.MODEL_FILE, "wb") as f:

0 commit comments

Comments
 (0)