From 68a24253e9f26bc6a638e8c5a8c6243a9fe7c84e Mon Sep 17 00:00:00 2001 From: LiuJiaNan <15703339975@163.com> Date: Sat, 20 Dec 2025 16:42:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=89=80=E6=9C=89throw=20new=20Error?= =?UTF-8?q?=E6=94=B9=E6=88=90console.error=E5=8A=A0return=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/Cascader/Dictionary/index.js | 6 ++- components/LeftTree/Dictionary/index.js | 6 ++- components/Select/Dictionary/index.js | 6 ++- components/SelectTree/Dictionary/index.js | 6 ++- hooks/useDeleteFile/index.js | 18 ++++++--- hooks/useDictionary/index.js | 18 ++++++--- hooks/useDownloadBlob/index.js | 3 +- hooks/useDownloadFile/index.js | 6 ++- hooks/useGetFile/index.js | 24 ++++++++---- hooks/useTable/index.js | 6 ++- hooks/useUploadFile/index.js | 48 +++++++++++++++-------- 11 files changed, 98 insertions(+), 49 deletions(-) diff --git a/components/Cascader/Dictionary/index.js b/components/Cascader/Dictionary/index.js index 989f391..8472e5f 100644 --- a/components/Cascader/Dictionary/index.js +++ b/components/Cascader/Dictionary/index.js @@ -18,8 +18,10 @@ function DictionaryCascader(props) { const [treeData, setTreeData] = useState([]); const getData = async () => { - if (!Object.values(DICTIONARY_APP_KEY_ENUM).includes(appKey)) - throw new Error("传入的 appKey 不在 DICTIONARY_APP_KEY_ENUM 中"); + if (!Object.values(DICTIONARY_APP_KEY_ENUM).includes(appKey)) { + console.error("传入的 appKey 不在 DICTIONARY_APP_KEY_ENUM 中"); + return; + } setTreeData([]); const { data } = await request("/config/dict-trees/list/by/dictValues", "get", { appKey, dictValue }); diff --git a/components/LeftTree/Dictionary/index.js b/components/LeftTree/Dictionary/index.js index 91720e5..e28a357 100644 --- a/components/LeftTree/Dictionary/index.js +++ b/components/LeftTree/Dictionary/index.js @@ -18,8 +18,10 @@ function DictionaryLeftTree(props) { const [treeData, setTreeData] = useState([]); const getData = async () => { - if (!Object.values(DICTIONARY_APP_KEY_ENUM).includes(appKey)) - throw new Error("传入的 appKey 不在 DICTIONARY_APP_KEY_ENUM 中"); + if (!Object.values(DICTIONARY_APP_KEY_ENUM).includes(appKey)) { + console.error("传入的 appKey 不在 DICTIONARY_APP_KEY_ENUM 中"); + return; + } setTreeData([]); const { data } = await request("/config/dict-trees/list/by/dictValues", "get", { appKey, dictValue }); diff --git a/components/Select/Dictionary/index.js b/components/Select/Dictionary/index.js index 0f96fce..d2521c2 100644 --- a/components/Select/Dictionary/index.js +++ b/components/Select/Dictionary/index.js @@ -18,8 +18,10 @@ function DictionarySelect(props) { const [data, setData] = useState([]); const getData = async () => { - if (!Object.values(DICTIONARY_APP_KEY_ENUM).includes(appKey)) - throw new Error("传入的 appKey 不在 DICTIONARY_APP_KEY_ENUM 中"); + if (!Object.values(DICTIONARY_APP_KEY_ENUM).includes(appKey)) { + console.error("传入的 appKey 不在 DICTIONARY_APP_KEY_ENUM 中"); + return; + } setData([]); const { data } = await request("/config/dict-trees/list/by/dictValues", "get", { appKey, dictValue }); diff --git a/components/SelectTree/Dictionary/index.js b/components/SelectTree/Dictionary/index.js index 289c11a..027ec8a 100644 --- a/components/SelectTree/Dictionary/index.js +++ b/components/SelectTree/Dictionary/index.js @@ -18,8 +18,10 @@ function DictionarySelectTree(props) { const [treeData, setTreeData] = useState([]); const getData = async () => { - if (!Object.values(DICTIONARY_APP_KEY_ENUM).includes(appKey)) - throw new Error("传入的 appKey 不在 DICTIONARY_APP_KEY_ENUM 中"); + if (!Object.values(DICTIONARY_APP_KEY_ENUM).includes(appKey)) { + console.error("传入的 appKey 不在 DICTIONARY_APP_KEY_ENUM 中"); + return; + } setTreeData([]); const { data } = await request("/config/dict-trees/list/by/dictValues", "get", { appKey, dictValue }); diff --git a/hooks/useDeleteFile/index.js b/hooks/useDeleteFile/index.js index 1f3f350..71ff626 100644 --- a/hooks/useDeleteFile/index.js +++ b/hooks/useDeleteFile/index.js @@ -12,8 +12,10 @@ function useDeleteFile(returnType = "object") { // 删除文件 const deleteFile = (options) => { - if (!options) - throw new Error("请传入 options"); + if (!options) { + console.error("请传入 options"); + return; + } // 增加请求数量并设置loading状态 requestCount++; @@ -24,10 +26,14 @@ function useDeleteFile(returnType = "object") { return new Promise((resolve, reject) => { const { files = [], single = true } = options; - if (!files) - throw new Error("请传入 files"); - if (!Array.isArray(files)) - throw new Error("请传入有效的 files"); + if (!files) { + console.error("请传入 files"); + return; + } + if (!Array.isArray(files)) { + console.error("请传入有效的 files"); + return; + } // 如果没有文件则直接返回 if (files.length === 0) { diff --git a/hooks/useDictionary/index.js b/hooks/useDictionary/index.js index d7c586f..7385c13 100644 --- a/hooks/useDictionary/index.js +++ b/hooks/useDictionary/index.js @@ -13,8 +13,10 @@ function useDictionary(returnType = "object") { // 获取数据字典 const getDictionary = (options) => { - if (!options) - throw new Error("请传入 options"); + if (!options) { + console.error("请传入 options"); + return; + } // 增加请求数量并设置loading状态 requestCount++; @@ -25,10 +27,14 @@ function useDictionary(returnType = "object") { return new Promise((resolve, reject) => { const { appKey = DICTIONARY_APP_KEY_ENUM.DEFAULT, dictValue } = options; - if (!Object.values(DICTIONARY_APP_KEY_ENUM).includes(appKey)) - throw new Error("传入的 options.appKey 不在 DICTIONARY_APP_KEY_ENUM 中"); - if (!dictValue) - throw new Error("请传入 options.dictValue"); + if (!Object.values(DICTIONARY_APP_KEY_ENUM).includes(appKey)) { + console.error("传入的 options.appKey 不在 DICTIONARY_APP_KEY_ENUM 中"); + return; + } + if (!dictValue) { + console.error("请传入 options.dictValue"); + return; + } // 发送请求 request( diff --git a/hooks/useDownloadBlob/index.js b/hooks/useDownloadBlob/index.js index a556368..41010a9 100644 --- a/hooks/useDownloadBlob/index.js +++ b/hooks/useDownloadBlob/index.js @@ -38,7 +38,8 @@ export default function useDownloadBlob(returnType = "object") { }) .then((response) => { if (!response.ok) { - throw new Error("Network response was not ok"); + console.error("网络响应异常"); + return; } return response.blob(); }) diff --git a/hooks/useDownloadFile/index.js b/hooks/useDownloadFile/index.js index 47744d6..9989638 100644 --- a/hooks/useDownloadFile/index.js +++ b/hooks/useDownloadFile/index.js @@ -20,8 +20,10 @@ export default function useDownloadFile(returnType = "object") { } const { url, name: fileName } = options; - if (!url) - throw new Error("请传入 url"); + if (!url) { + console.error("请传入 url"); + return; + } Modal.confirm({ title: "提示", diff --git a/hooks/useGetFile/index.js b/hooks/useGetFile/index.js index c3e6553..8fa9617 100644 --- a/hooks/useGetFile/index.js +++ b/hooks/useGetFile/index.js @@ -14,8 +14,10 @@ function useGetFile(returnType = "object") { // 获取文件 const getFile = (options) => { - if (!options) - throw new Error("请传入 options"); + if (!options) { + console.error("请传入 options"); + return; + } // 增加请求数量并设置loading状态 requestCount++; @@ -26,15 +28,21 @@ function useGetFile(returnType = "object") { return new Promise((resolve, reject) => { const { eqType, eqForeignKey } = options; - if (!eqType) - throw new Error("请传入 options.eqType"); + if (!eqType) { + console.error("请传入 options.eqType"); + return; + } // 检查eqType是否在UPLOAD_FILE_TYPE_ENUM中 - if (!Object.values(UPLOAD_FILE_TYPE_ENUM).includes(eqType)) - throw new Error("传入的 eqType 不在 UPLOAD_FILE_TYPE_ENUM 中"); + if (!Object.values(UPLOAD_FILE_TYPE_ENUM).includes(eqType)) { + console.error("传入的 eqType 不在 UPLOAD_FILE_TYPE_ENUM 中"); + return; + } - if (eqForeignKey === undefined || eqForeignKey === null) - throw new Error("请传入 options.eqForeignKey"); + if (eqForeignKey === undefined || eqForeignKey === null) { + console.error("请传入 options.eqForeignKey"); + return; + } // 发送请求 request( diff --git a/hooks/useTable/index.js b/hooks/useTable/index.js index b8b148d..7d292ba 100644 --- a/hooks/useTable/index.js +++ b/hooks/useTable/index.js @@ -44,8 +44,10 @@ function getService(service, getExtraParams = {}, transform, usePermission) { * 自定义 useTable,继承 ahooks 的 useAntdTable,根据需求进行扩展 */ function useTable(service, options) { - if (!service) - throw new Error("请传入 service"); + if (!service) { + console.error("请传入 service") + return; + } // 获取额外参数和转换函数 const { params: extraParams, transform, ...restOptions } = options || {}; diff --git a/hooks/useUploadFile/index.js b/hooks/useUploadFile/index.js index 80ce9bd..5f6a513 100644 --- a/hooks/useUploadFile/index.js +++ b/hooks/useUploadFile/index.js @@ -13,8 +13,10 @@ function useUploadFile(returnType = "object") { // 上传文件 const uploadFile = (options) => { - if (!options) - throw new Error("请传入 options"); + if (!options) { + console.error("请传入 options"); + return; + } // 增加请求数量并设置loading状态 requestCount++; @@ -25,28 +27,42 @@ function useUploadFile(returnType = "object") { return new Promise((resolve, reject) => { const { files = [], single = true, params } = options; - if (!files) - throw new Error("请传入 files"); - if (!Array.isArray(files)) - throw new Error("请传入有效的 files"); - if (!params) - throw new Error("请传入 options.params"); - if (!params.type) - throw new Error("请传入 options.params.type"); + if (!files) { + console.error("请传入 files"); + return; + } + if (!Array.isArray(files)) { + console.error("请传入有效的 files"); + return; + } + if (!params) { + console.error("请传入 options.params"); + return; + } + if (!params.type) { + console.error("请传入 options.params.type"); + return; + } // 检查type是否在UPLOAD_FILE_TYPE_ENUM中 - if (!Object.values(UPLOAD_FILE_TYPE_ENUM).includes(params.type)) - throw new Error("传入的 type 不在 UPLOAD_FILE_TYPE_ENUM 中"); + if (!Object.values(UPLOAD_FILE_TYPE_ENUM).includes(params.type)) { + console.error("传入的 type 不在 UPLOAD_FILE_TYPE_ENUM 中"); + return; + } // 根据type获取对应的path const path = UPLOAD_FILE_PATH_ENUM[params.type]; - if (!path) - throw new Error(`未找到 type ${params.type} 对应的 path `); + if (!path) { + console.error(`未找到 type ${params.type} 对应的 path `); + return; + } // 当single为false时,foreignKey是必需的 if (!single) { - if (!params.hasOwnProperty("foreignKey")) - throw new Error("请传入 options.params.foreignKey"); + if (!params.hasOwnProperty("foreignKey")) { + console.error("请传入 options.params.foreignKey"); + return; + } // 如果 foreignKey 是 undefined,设置默认值为空字符串 if (params.foreignKey === undefined || params.foreignKey === null) params.foreignKey = "";