Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

Restructure the File errors in controller (#23801)

Yongtao Huangbf2f03f9116a
diff --git a/api/controllers/common/errors.py b/api/controllers/common/errors.py
index 9f762b3135e..6e2ea952fca 100644
--- a/api/controllers/common/errors.py
+++ b/api/controllers/common/errors.py
@@ -1,5 +1,7 @@
from werkzeug.exceptions import HTTPException
+from libs.exception import BaseHTTPException
+
class FilenameNotExistsError(HTTPException):
code = 400
@@ -9,3 +11,27 @@ class FilenameNotExistsError(HTTPException):
class RemoteFileUploadError(HTTPException):
code = 400
description = "Error uploading remote file."
+
+
+class FileTooLargeError(BaseHTTPException):
+ error_code = "file_too_large"
+ description = "File size exceeded. {message}"
+ code = 413
+
+
+class UnsupportedFileTypeError(BaseHTTPException):
+ error_code = "unsupported_file_type"
+ description = "File type not allowed."
+ code = 415
+
+
+class TooManyFilesError(BaseHTTPException):
+ error_code = "too_many_files"
+ description = "Only one file is allowed."
+ code = 400
+
+
+class NoFileUploadedError(BaseHTTPException):
+ error_code = "no_file_uploaded"
+ description = "Please upload your file."
+ code = 400
diff --git a/api/controllers/console/app/annotation.py b/api/controllers/console/app/annotation.py
index ee6011cd651..493a9a52e27 100644
--- a/api/controllers/console/app/annotation.py
+++ b/api/controllers/console/app/annotation.py
@@ -3,9 +3,8 @@ from flask_login import current_user
from flask_restful import Resource, marshal, marshal_with, reqparse
from werkzeug.exceptions import Forbidden
+from controllers.common.errors import NoFileUploadedError, TooManyFilesError
from controllers.console import api
-from controllers.console.app.error import NoFileUploadedError
-from controllers.console.datasets.error import TooManyFilesError
from controllers.console.wraps import (
account_initialization_required,
cloud_edition_billing_resource_check,
diff --git a/api/controllers/console/app/error.py b/api/controllers/console/app/error.py
index 1559f82d6ea..fbd7901646d 100644
--- a/api/controllers/console/app/error.py
+++ b/api/controllers/console/app/error.py
@@ -79,18 +79,6 @@ class ProviderNotSupportSpeechToTextError(BaseHTTPException):
code = 400
-class NoFileUploadedError(BaseHTTPException):
- error_code = "no_file_uploaded"
- description = "Please upload your file."
- code = 400
-
-
-class TooManyFilesError(BaseHTTPException):
- error_code = "too_many_files"
- description = "Only one file is allowed."
- code = 400
-
-
class DraftWorkflowNotExist(BaseHTTPException):
error_code = "draft_workflow_not_exist"
description = "Draft workflow need to be initialized."
diff --git a/api/controllers/console/datasets/error.py b/api/controllers/console/datasets/error.py
index cb68bb5e812..a43843b551d 100644
--- a/api/controllers/console/datasets/error.py
+++ b/api/controllers/console/datasets/error.py
@@ -1,30 +1,6 @@
from libs.exception import BaseHTTPException
-class NoFileUploadedError(BaseHTTPException):
- error_code = "no_file_uploaded"
- description = "Please upload your file."
- code = 400
-
-
-class TooManyFilesError(BaseHTTPException):
- error_code = "too_many_files"
- description = "Only one file is allowed."
- code = 400
-
-
-class FileTooLargeError(BaseHTTPException):
- error_code = "file_too_large"
- description = "File size exceeded. {message}"
- code = 413
-
-
-class UnsupportedFileTypeError(BaseHTTPException):
- error_code = "unsupported_file_type"
- description = "File type not allowed."
- code = 415
-
-
class DatasetNotInitializedError(BaseHTTPException):
error_code = "dataset_not_initialized"
description = "The dataset is still being initialized or indexing. Please wait a moment."
diff --git a/api/controllers/console/error.py b/api/controllers/console/error.py
index 0a4dfe1c102..0645d63be5f 100644
--- a/api/controllers/console/error.py
+++ b/api/controllers/console/error.py
@@ -76,30 +76,6 @@ class EmailSendIpLimitError(BaseHTTPException):
code = 429
-class FileTooLargeError(BaseHTTPException):
- error_code = "file_too_large"
- description = "File size exceeded. {message}"
- code = 413
-
-
-class UnsupportedFileTypeError(BaseHTTPException):
- error_code = "unsupported_file_type"
- description = "File type not allowed."
- code = 415
-
-
-class TooManyFilesError(BaseHTTPException):
- error_code = "too_many_files"
- description = "Only one file is allowed."
- code = 400
-
-
-class NoFileUploadedError(BaseHTTPException):
- error_code = "no_file_uploaded"
- description = "Please upload your file."
- code = 400
-
-
class UnauthorizedAndForceLogout(BaseHTTPException):
error_code = "unauthorized_and_force_logout"
description = "Unauthorized and force logout."
diff --git a/api/controllers/console/files.py b/api/controllers/console/files.py
index 256ff24b3b3..a87d270e9c9 100644
--- a/api/controllers/console/files.py
+++ b/api/controllers/console/files.py
@@ -8,7 +8,13 @@ from werkzeug.exceptions import Forbidden
import services
from configs import dify_config
from constants import DOCUMENT_EXTENSIONS
-from controllers.common.errors import FilenameNotExistsError
+from controllers.common.errors import (
+ FilenameNotExistsError,
+ FileTooLargeError,
+ NoFileUploadedError,
+ TooManyFilesError,
+ UnsupportedFileTypeError,
+)
from controllers.console.wraps import (
account_initialization_required,
cloud_edition_billing_resource_check,
@@ -18,13 +24,6 @@ from fields.file_fields import file_fields, upload_config_fields
from libs.login import login_required
from services.file_service import FileService
-from .error import (
- FileTooLargeError,
- NoFileUploadedError,
- TooManyFilesError,
- UnsupportedFileTypeError,
-)
-
PREVIEW_WORDS_LIMIT = 3000
diff --git a/api/controllers/console/remote_files.py b/api/controllers/console/remote_files.py
index b8cf019e4f0..c356113c402 100644
--- a/api/controllers/console/remote_files.py
+++ b/api/controllers/console/remote_files.py
@@ -7,18 +7,17 @@ from flask_restful import Resource, marshal_with, reqparse
import services
from controllers.common import helpers
-from controllers.common.errors import RemoteFileUploadError
+from controllers.common.errors import (
+ FileTooLargeError,
+ RemoteFileUploadError,
+ UnsupportedFileTypeError,
+)
from core.file import helpers as file_helpers
from core.helper import ssrf_proxy
from fields.file_fields import file_fields_with_signed_url, remote_file_info_fields
from models.account import Account
from services.file_service import FileService
-from .error import (
- FileTooLargeError,
- UnsupportedFileTypeError,
-)
-
class RemoteFileInfoApi(Resource):
@marshal_with(remote_file_info_fields)
diff --git a/api/controllers/console/workspace/workspace.py b/api/controllers/console/workspace/workspace.py
index 6012c9ecc8a..f4f0078da7d 100644
--- a/api/controllers/console/workspace/workspace.py
+++ b/api/controllers/console/workspace/workspace.py
@@ -7,15 +7,15 @@ from sqlalchemy import select
from werkzeug.exceptions import Unauthorized
import services
-from controllers.common.errors import FilenameNotExistsError
-from controllers.console import api
-from controllers.console.admin import admin_required
-from controllers.console.datasets.error import (
+from controllers.common.errors import (
+ FilenameNotExistsError,
FileTooLargeError,
NoFileUploadedError,
TooManyFilesError,
UnsupportedFileTypeError,
)
+from controllers.console import api
+from controllers.console.admin import admin_required
from controllers.console.error import AccountNotLinkTenantError
from controllers.console.wraps import (
account_initialization_required,
diff --git a/api/controllers/files/error.py b/api/controllers/files/error.py
deleted file mode 100644
index a7ce4cd6f79..00000000000
--- a/api/controllers/files/error.py
+++ /dev/null
@@ -1,7 +0,0 @@
-from libs.exception import BaseHTTPException
-
-
-class UnsupportedFileTypeError(BaseHTTPException):
- error_code = "unsupported_file_type"
- description = "File type not allowed."
- code = 415
diff --git a/api/controllers/files/image_preview.py b/api/controllers/files/image_preview.py
index 46c19e1fbb3..91f7b27d1dd 100644
--- a/api/controllers/files/image_preview.py
+++ b/api/controllers/files/image_preview.py
@@ -5,8 +5,8 @@ from flask_restful import Resource, reqparse
from werkzeug.exceptions import NotFound
import services
+from controllers.common.errors import UnsupportedFileTypeError
from controllers.files import api
-from controllers.files.error import UnsupportedFileTypeError
from services.account_service import TenantService
from services.file_service import FileService
diff --git a/api/controllers/files/tool_files.py b/api/controllers/files/tool_files.py
index 1c3430ef4fd..d9c4e505118 100644
--- a/api/controllers/files/tool_files.py
+++ b/api/controllers/files/tool_files.py
@@ -4,8 +4,8 @@ from flask import Response
from flask_restful import Resource, reqparse
from werkzeug.exceptions import Forbidden, NotFound
+from controllers.common.errors import UnsupportedFileTypeError
from controllers.files import api
-from controllers.files.error import UnsupportedFileTypeError
from core.tools.signature import verify_tool_file_signature
from core.tools.tool_file_manager import ToolFileManager
from models import db as global_db
diff --git a/api/controllers/files/upload.py b/api/controllers/files/upload.py
index 15f93d2774a..bcc72d131c7 100644
--- a/api/controllers/files/upload.py
+++ b/api/controllers/files/upload.py
@@ -5,11 +5,13 @@ from flask_restful import Resource, marshal_with
from werkzeug.exceptions import Forbidden
import services
+from controllers.common.errors import (
+ FileTooLargeError,
+ UnsupportedFileTypeError,
+)
from controllers.console.wraps import setup_required
from controllers.files import api
-from controllers.files.error import UnsupportedFileTypeError
from controllers.inner_api.plugin.wraps import get_user
-from controllers.service_api.app.error import FileTooLargeError
from core.file.helpers import verify_plugin_file_signature
from core.tools.tool_file_manager import ToolFileManager
from fields.file_fields import file_fields
diff --git a/api/controllers/service_api/app/error.py b/api/controllers/service_api/app/error.py
index ba705f71e27..0e04a04cb24 100644
--- a/api/controllers/service_api/app/error.py
+++ b/api/controllers/service_api/app/error.py
@@ -85,30 +85,6 @@ class ProviderNotSupportSpeechToTextError(BaseHTTPException):
code = 400
-class NoFileUploadedError(BaseHTTPException):
- error_code = "no_file_uploaded"
- description = "Please upload your file."
- code = 400
-
-
-class TooManyFilesError(BaseHTTPException):
- error_code = "too_many_files"
- description = "Only one file is allowed."
- code = 400
-
-
-class FileTooLargeError(BaseHTTPException):
- error_code = "file_too_large"
- description = "File size exceeded. {message}"
- code = 413
-
-
-class UnsupportedFileTypeError(BaseHTTPException):
- error_code = "unsupported_file_type"
- description = "File type not allowed."
- code = 415
-
-
class FileNotFoundError(BaseHTTPException):
error_code = "file_not_found"
description = "The requested file was not found."
diff --git a/api/controllers/service_api/app/file.py b/api/controllers/service_api/app/file.py
index f09d07bcb6b..37153ca5dbf 100644
--- a/api/controllers/service_api/app/file.py
+++ b/api/controllers/service_api/app/file.py
@@ -2,14 +2,14 @@ from flask import request
from flask_restful import Resource, marshal_with
import services
-from controllers.common.errors import FilenameNotExistsError
-from controllers.service_api import api
-from controllers.service_api.app.error import (
+from controllers.common.errors import (
+ FilenameNotExistsError,
FileTooLargeError,
NoFileUploadedError,
TooManyFilesError,
UnsupportedFileTypeError,
)
+from controllers.service_api import api
from controllers.service_api.wraps import FetchUserArg, WhereisUserArg, validate_app_token
from fields.file_fields import file_fields
from models.model import App, EndUser
diff --git a/api/controllers/service_api/dataset/document.py b/api/controllers/service_api/dataset/document.py
index 2955d5d20d4..d0354f7851a 100644
--- a/api/controllers/service_api/dataset/document.py
+++ b/api/controllers/service_api/dataset/document.py
@@ -6,15 +6,15 @@ from sqlalchemy import desc, select
from werkzeug.exceptions import Forbidden, NotFound
import services
-from controllers.common.errors import FilenameNotExistsError
-from controllers.service_api import api
-from controllers.service_api.app.error import (
+from controllers.common.errors import (
+ FilenameNotExistsError,
FileTooLargeError,
NoFileUploadedError,
- ProviderNotInitializeError,
TooManyFilesError,
UnsupportedFileTypeError,
)
+from controllers.service_api import api
+from controllers.service_api.app.error import ProviderNotInitializeError
from controllers.service_api.dataset.error import (
ArchivedDocumentImmutableError,
DocumentIndexingError,
diff --git a/api/controllers/service_api/dataset/error.py b/api/controllers/service_api/dataset/error.py
index ecc47b40a12..e4214a16ad9 100644
--- a/api/controllers/service_api/dataset/error.py
+++ b/api/controllers/service_api/dataset/error.py
@@ -1,30 +1,6 @@
from libs.exception import BaseHTTPException
-class NoFileUploadedError(BaseHTTPException):
- error_code = "no_file_uploaded"
- description = "Please upload your file."
- code = 400
-
-
-class TooManyFilesError(BaseHTTPException):
- error_code = "too_many_files"
- description = "Only one file is allowed."
- code = 400
-
-
-class FileTooLargeError(BaseHTTPException):
- error_code = "file_too_large"
- description = "File size exceeded. {message}"
- code = 413
-
-
-class UnsupportedFileTypeError(BaseHTTPException):
- error_code = "unsupported_file_type"
- description = "File type not allowed."
- code = 415
-
-
class DatasetNotInitializedError(BaseHTTPException):
error_code = "dataset_not_initialized"
description = "The dataset is still being initialized or indexing. Please wait a moment."
diff --git a/api/controllers/web/error.py b/api/controllers/web/error.py
index 036e11d5c55..196a27e348b 100644
--- a/api/controllers/web/error.py
+++ b/api/controllers/web/error.py
@@ -97,30 +97,6 @@ class ProviderNotSupportSpeechToTextError(BaseHTTPException):
code = 400
-class NoFileUploadedError(BaseHTTPException):
- error_code = "no_file_uploaded"
- description = "Please upload your file."
- code = 400
-
-
-class TooManyFilesError(BaseHTTPException):
- error_code = "too_many_files"
- description = "Only one file is allowed."
- code = 400
-
-
-class FileTooLargeError(BaseHTTPException):
- error_code = "file_too_large"
- description = "File size exceeded. {message}"
- code = 413
-
-
-class UnsupportedFileTypeError(BaseHTTPException):
- error_code = "unsupported_file_type"
- description = "File type not allowed."
- code = 415
-
-
class WebAppAuthRequiredError(BaseHTTPException):
error_code = "web_sso_auth_required"
description = "Web app authentication required."
diff --git a/api/controllers/web/files.py b/api/controllers/web/files.py
index 8e9317606e4..0c304358250 100644
--- a/api/controllers/web/files.py
+++ b/api/controllers/web/files.py
@@ -2,8 +2,13 @@ from flask import request
from flask_restful import marshal_with
import services
-from controllers.common.errors import FilenameNotExistsError
-from controllers.web.error import FileTooLargeError, NoFileUploadedError, TooManyFilesError, UnsupportedFileTypeError
+from controllers.common.errors import (
+ FilenameNotExistsError,
+ FileTooLargeError,
+ NoFileUploadedError,
+ TooManyFilesError,
+ UnsupportedFileTypeError,
+)
from controllers.web.wraps import WebApiResource
from fields.file_fields import file_fields
from services.file_service import FileService
diff --git a/api/controllers/web/remote_files.py b/api/controllers/web/remote_files.py
index ae68df6bdc4..4e19716c3df 100644
--- a/api/controllers/web/remote_files.py
+++ b/api/controllers/web/remote_files.py
@@ -5,15 +5,17 @@ from flask_restful import marshal_with, reqparse
import services
from controllers.common import helpers
-from controllers.common.errors import RemoteFileUploadError
+from controllers.common.errors import (
+ FileTooLargeError,
+ RemoteFileUploadError,
+ UnsupportedFileTypeError,
+)
from controllers.web.wraps import WebApiResource
from core.file import helpers as file_helpers
from core.helper import ssrf_proxy
from fields.file_fields import file_fields_with_signed_url, remote_file_info_fields
from services.file_service import FileService
-from .error import FileTooLargeError, UnsupportedFileTypeError
-
class RemoteFileInfoApi(WebApiResource):
@marshal_with(remote_file_info_fields)
diff --git a/api/tests/unit_tests/controllers/console/test_files_security.py b/api/tests/unit_tests/controllers/console/test_files_security.py
index cb5562d3451..2630fbcfd0c 100644
--- a/api/tests/unit_tests/controllers/console/test_files_security.py
+++ b/api/tests/unit_tests/controllers/console/test_files_security.py
@@ -4,8 +4,8 @@ from unittest.mock import patch
import pytest
from werkzeug.exceptions import Forbidden
-from controllers.common.errors import FilenameNotExistsError
-from controllers.console.error import (
+from controllers.common.errors import (
+ FilenameNotExistsError,
FileTooLargeError,
NoFileUploadedError,
TooManyFilesError,