Repository: bookwyrm-social/bookwyrm

Issue:
Server Error when consulting two books
**Describe the bug**
I get a "Server Error" when I try to consult the following books, but other books from the same author are fine:
- [Making Friends](https://bookwyrm.social/book/95163/s/making-friends) by Veo Corva
- [Tinker & Terror](https://bookwyrm.social/book/95165/s/tinker-terror) by Veo Corva

I get the error on Desktop (Firefox and Brave on Windows 10) as well as Safari on iOS for instance.

**Instance**
bookwyrm.social


Discussion:

Resolving patch:
diff --git a/bookwyrm/isbn/isbn.py b/bookwyrm/isbn/isbn.py
--- a/bookwyrm/isbn/isbn.py
+++ b/bookwyrm/isbn/isbn.py
@@ -40,7 +40,12 @@ def hyphenate(self, isbn_13: Optional[str]) -> Optional[str]:
             self.__element_tree = ElementTree.parse(self.__range_file_path)
 
         gs1_prefix = isbn_13[:3]
-        reg_group = self.__find_reg_group(isbn_13, gs1_prefix)
+        try:
+            reg_group = self.__find_reg_group(isbn_13, gs1_prefix)
+        except ValueError:
+            # if the reg groups are invalid, just return the original isbn
+            return isbn_13
+
         if reg_group is None:
             return isbn_13  # failed to hyphenate
 
diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py
--- a/bookwyrm/models/book.py
+++ b/bookwyrm/models/book.py
@@ -366,9 +366,9 @@ def save(self, *args: Any, **kwargs: Any) -> None:
 
         # normalize isbn format
         if self.isbn_10:
-            self.isbn_10 = re.sub(r"[^0-9X]", "", self.isbn_10)
+            self.isbn_10 = normalize_isbn(self.isbn_10)
         if self.isbn_13:
-            self.isbn_13 = re.sub(r"[^0-9X]", "", self.isbn_13)
+            self.isbn_13 = normalize_isbn(self.isbn_13)
 
         # set rank
         self.edition_rank = self.get_rank()
@@ -463,6 +463,11 @@ def isbn_13_to_10(isbn_13):
     return converted + str(checkdigit)
 
 
+def normalize_isbn(isbn):
+    """Remove unexpected characters from ISBN 10 or 13"""
+    return re.sub(r"[^0-9X]", "", isbn)
+
+
 # pylint: disable=unused-argument
 @receiver(models.signals.post_save, sender=Edition)
 def preview_image(instance, *args, **kwargs):


Test patch:
diff --git a/bookwyrm/tests/models/test_book_model.py b/bookwyrm/tests/models/test_book_model.py
--- a/bookwyrm/tests/models/test_book_model.py
+++ b/bookwyrm/tests/models/test_book_model.py
@@ -11,7 +11,7 @@
 from django.utils import timezone
 
 from bookwyrm import models, settings
-from bookwyrm.models.book import isbn_10_to_13, isbn_13_to_10
+from bookwyrm.models.book import isbn_10_to_13, isbn_13_to_10, normalize_isbn
 from bookwyrm.settings import ENABLE_THUMBNAIL_GENERATION
 
 
@@ -72,6 +72,10 @@ def test_isbn_13_to_10(self):
         isbn_10 = isbn_13_to_10(isbn_13)
         self.assertEqual(isbn_10, "178816167X")
 
+    def test_normalize_isbn(self):
+        """Remove misc characters from ISBNs"""
+        self.assertEqual(normalize_isbn("978-0-4633461-1-2"), "9780463346112")
+
     def test_get_edition_info(self):
         """text slug about an edition"""
         book = models.Edition.objects.create(title="Test Edition")
diff --git a/bookwyrm/tests/test_isbn.py b/bookwyrm/tests/test_isbn.py
--- a/bookwyrm/tests/test_isbn.py
+++ b/bookwyrm/tests/test_isbn.py
@@ -29,3 +29,10 @@ def test_isbn_hyphenation(self):
         self.assertEqual(hyphenator.hyphenate("9786769533251"), "9786769533251")
         # 979-8 (United States) 2300000-3499999 (unassigned)
         self.assertEqual(hyphenator.hyphenate("9798311111111"), "9798311111111")
+
+    def test_isbn_hyphenation_invalid_data(self):
+        """Make sure not to throw an error when a bad ISBN is found"""
+        # no action taken
+        self.assertEqual(hyphenator.hyphenate("978-0-4633461-1-2"), "978-0-4633461-1-2")
+        self.assertEqual(hyphenator.hyphenate("9-0-4633461-1-2"), "9-0-4633461-1-2")
+        self.assertEqual(hyphenator.hyphenate("90463346112"), "90463346112")