【问题标题】:Django getting error exceptions must derive from BaseExceptionDjango 获取错误异常必须从 BaseException 派生
【发布时间】:2022-10-20 15:17:38
【问题描述】:

信息: 我想在前端使用Uppydjango-tus 作为文件处理的后端来上传文件。我收到错误TypeError: exceptions must derive from BaseException

追溯

Internal Server Error: /tus/upload/6393bfe5-277e-4c68-b9af-c0394be796b9
Traceback (most recent call last):
  File "/django_tus/tusfile.py", line 75, in get_tusfile_or_404
    raise TusResponse(status=404)
TypeError: exceptions must derive from BaseException
[06/Aug/2022 14:36:42] "HEAD /tus/upload/6393bfe5-277e-4c68-b9af-c0394be796b9 HTTP/1.1" 500 103054
[06/Aug/2022 14:36:42,624] - Broken pipe from ('127.0.0.1', 35814)

[06/Aug/2022 14:36:42] "POST /tus/upload/ HTTP/1.1" 201 0
[06/Aug/2022 14:36:42] "PATCH /tus/upload/8295bef4-c94a-4ab7-9c75-2635c74428d8 HTTP/1.1" 204 0

https://github.com/alican/django-tus/blob/master/django_tus/tusfile.py

class TusUpload(View):

    def head(self, request, resource_id):
        tus_file = TusFile.get_tusfile_or_404(str(resource_id))

        return TusResponse(
            status=200,
            extra_headers={
                'Upload-Offset': tus_file.offset,
                'Upload-Length': tus_file.file_size,
            })
def create_initial_file(metadata, file_size: int):
        resource_id = str(uuid.uuid4())
        cache.add("tus-uploads/{}/filename".format(resource_id), "{}".format(metadata.get("filename")), settings.TUS_TIMEOUT)
        cache.add("tus-uploads/{}/file_size".format(resource_id), file_size, settings.TUS_TIMEOUT)
        cache.add("tus-uploads/{}/offset".format(resource_id), 0, settings.TUS_TIMEOUT)
        cache.add("tus-uploads/{}/metadata".format(resource_id), metadata, settings.TUS_TIMEOUT)

        tus_file = TusFile(resource_id)
        tus_file.write_init_file()
        return tus_file

【问题讨论】:

  • 不要仅仅因为您使用的是 GitHub 上的 Git 存储库而使用 gitgithub 标签。这是一个基本的 Python 编程错误:您试图创建一个异常,但没有使用 BaseException 类作为您的基类。 (旧版本的 Python 没有强制执行此约束。)

标签: python python-3.x django uppy


【解决方案1】:

django-tus 尝试在 404 错误的情况下引发 in line 60 TusResponse 但不会从异常继承。

这是图书馆的错误。它在 2 年内没有更新,并且根据 @torek BaseException 继承以前没有强制执行。您可以尝试使用 TypeError 将其包装在 try-except 中,但在我的实现中这不起作用。

您可以尝试在您的 django-tus 版本中将 BaseException 添加到 TusResponse 的继承类中。

class TusResponse(HttpResponse, BaseException):

这应该消除错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 2021-10-31
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多