瀏覽代碼

[api] Improve error handling for non-decodable files in /display filebrowser API (#4130)

Instead of displaying raw binary content when file decoding fails, returns a user-friendly error message suggesting file download as an alternative.

Adds error logging to help diagnose encoding issues in server logs.
Harsh Gupta 7 月之前
父節點
當前提交
2c6299e67f
共有 1 個文件被更改,包括 2 次插入1 次删除
  1. 2 1
      apps/filebrowser/src/filebrowser/api.py

+ 2 - 1
apps/filebrowser/src/filebrowser/api.py

@@ -410,7 +410,8 @@ def display(request):
       file_contents = contents.decode(encoding)
       mode = 'text'
     except UnicodeDecodeError:
-      file_contents = contents
+      LOG.error("Cannot decode file contents with encoding: %s." % encoding)
+      return HttpResponse("Cannot display file content. Please download the file instead.", status=422)
 
   data = {
     'contents': file_contents,