Repository: bookwyrm-social/bookwyrm

Issue:
Don't show a generic 413 server error upon submitting incompatible content
**Is your feature request related to a problem? Please describe.**
I added a new book to the database, filled out all the DB fields and uploaded an image that happened to be too big. Submitting led to a white 413 Content too large error page from the web server, so I didn't know what the problem was (could also have been a description text that was too long, or the ISBN field doesn't accept the hyphens I added, or...)

**Describe the solution you'd like**
If there's something wrong with the content I add, I'd like the system to tell me right away, i.e. on the same page at the field that contains the error. Failing that, I'd still like the system to tell me what went wrong upon submitting, i.e. instead of showing me a generic 413 server error, tell me what part of the content was wrong and how I should fix it.

**Describe alternatives you've considered**
the only alternative I saw was an educated guess

**Additional context**
See [this rant](https://chaos.social/@janeckhoff/109767477467168537) for more context.



Discussion:
It sounds like this is a [`SuspiciousOperation`](https://docs.djangoproject.com/en/3.2/ref/exceptions/#django.core.exceptions.SuspiciousOperation) `(RequestDataTooBig)` error, but I'm struggling to replicate the bug, even using an absurdly large image file. I wonder if something changed with image file handling since this issue was reported.

Resolving patch:
diff --git a/bookwyrm/middleware/__init__.py b/bookwyrm/middleware/__init__.py
--- a/bookwyrm/middleware/__init__.py
+++ b/bookwyrm/middleware/__init__.py
@@ -1,3 +1,4 @@
 """ look at all this nice middleware! """
 from .timezone_middleware import TimezoneMiddleware
 from .ip_middleware import IPBlocklistMiddleware
+from .file_too_big import FileTooBig
diff --git a/bookwyrm/middleware/file_too_big.py b/bookwyrm/middleware/file_too_big.py
new file mode 100644
--- /dev/null
+++ b/bookwyrm/middleware/file_too_big.py
@@ -0,0 +1,30 @@
+"""Middleware to display a custom 413 error page"""
+
+from django.http import HttpResponse
+from django.shortcuts import render
+from django.core.exceptions import RequestDataTooBig
+
+
+class FileTooBig:
+    """Middleware to display a custom page when a
+    RequestDataTooBig exception is thrown"""
+
+    def __init__(self, get_response):
+        """boilerplate __init__ from Django docs"""
+
+        self.get_response = get_response
+
+    def __call__(self, request):
+        """If RequestDataTooBig is thrown, render the 413 error page"""
+
+        try:
+            body = request.body  # pylint: disable=unused-variable
+
+        except RequestDataTooBig:
+
+            rendered = render(request, "413.html")
+            response = HttpResponse(rendered)
+            return response
+
+        response = self.get_response(request)
+        return response
diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py
--- a/bookwyrm/settings.py
+++ b/bookwyrm/settings.py
@@ -119,6 +119,7 @@
     "bookwyrm.middleware.IPBlocklistMiddleware",
     "django.contrib.messages.middleware.MessageMiddleware",
     "django.middleware.clickjacking.XFrameOptionsMiddleware",
+    "bookwyrm.middleware.FileTooBig",
 ]
 
 ROOT_URLCONF = "bookwyrm.urls"


Test patch: