Repository: hydroshare/hydroshare

Issue:
Working image types, outside of the allowlisted file extensions, throws a generic error
**Description of the bug**
When using an image that appears to work, but falls outside the allowlisted file extensions (e.g., .webp), the error handling is unusual.  The error thrown is "Error! Metadata failed to update "Could not parse the error message"", rather than the usual "Error! Failed to update metadata element 'toolicon'. ['Supported icon image types are png, gif, jpeg, ico, and svg']." thrown (e.g., by icon URL "google.com/index.html").  

Steps to reproduce the bug:
1. Create a resource of type Web App Connector
2. Set "Icon URL or File" to "https://upload.wikimedia.org/wikipedia/commons/a/a1/Johnrogershousemay2020.webp"
3. Save and observe the generic error

**Expected behavior**
A more actionable and descriptive error is thrown, like the "Error! Failed to update metadata element 'toolicon'. ['Supported icon image types are png, gif, jpeg, ico, and svg']." thrown in other cases.

https://user-images.githubusercontent.com/43424907/178643646-f4740b26-6bba-4788-a623-2549b8658084.mov





Discussion:
Looks like this issue has been resolved
![image](https://user-images.githubusercontent.com/17934193/201951654-89d4b029-28c8-4c65-8959-91d929b0b810.png)

reopening this issue.
rc2.1 (89dc601dead967653ac54ddc9160e1140e800f1e) local dev env gives the correct error message:
![image](https://user-images.githubusercontent.com/17934193/204823245-2b6d814a-ebf0-4316-b02e-70fc54d36898.png)

But rc2.1 deployed to beta however gives:
![image](https://user-images.githubusercontent.com/17934193/204823515-2e08e770-3f5a-47b0-91e2-c31a79fb9974.png)

re-opening, needs another look

Resolving patch:
diff --git a/hs_tools_resource/middleware.py b/hs_tools_resource/middleware.py
--- a/hs_tools_resource/middleware.py
+++ b/hs_tools_resource/middleware.py
@@ -1,7 +1,8 @@
-from django.http import JsonResponse, HttpResponse
+from django.http import JsonResponse
 from django.core.exceptions import RequestDataTooBig
 from django.conf import settings
 from hs_tools_resource.utils import convert_size
+from django.urls import resolve
 
 
 class CheckRequest():
@@ -17,25 +18,8 @@ def __call__(self, request):
         try:
             _ = request.body
         except RequestDataTooBig:
-            limit = convert_size(settings.DATA_UPLOAD_MAX_MEMORY_SIZE)
-            return JsonResponse({"message": f"The upload was too big. Limit: {limit}"})
-
-        response = self.get_response(request)
-        return response
-
-    def process_request(self, request):
-        try:
-            request.body
-        except RequestDataTooBig as e:
-            response = self.process_exception(request, e)
-            if response is not None:
-                return response
-            else:
-                pass
-
-        return None
-
-    def process_exception(self, request, exception):
-        if isinstance(exception, RequestDataTooBig):
-            return HttpResponse(status=413)
-        return None
+            view_name = resolve(request.path).view_name
+            if view_name == "update_metadata_element":
+                limit = convert_size(settings.DATA_UPLOAD_MAX_MEMORY_SIZE)
+                return JsonResponse({"message": f"The upload was too big. Limit: {limit}"})
+        return self.get_response(request)
diff --git a/theme/views.py b/theme/views.py
--- a/theme/views.py
+++ b/theme/views.py
@@ -116,6 +116,7 @@ def get_context_data(self, **kwargs):
             "resources": resources,
             "quota_message": get_quota_message(u),
             "group_membership_requests": group_membership_requests,
+            "data_upload_max": settings.DATA_UPLOAD_MAX_MEMORY_SIZE
         }
 
 


Test patch: