From 336ca5770b941dce05efce32bd110bd586e61860 Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Thu, 17 Apr 2025 14:50:59 +0800 Subject: [PATCH] fix: improve error handling and enhance setting management - Updated error handling in GetSetting to use errors.Is for better clarity when checking for no documents. - Refactored setting retrieval in the Vuex store to ensure a default value is set if the retrieved setting is empty. - Adjusted API calls in the store to wrap payloads in a data object for consistency. - Cleaned up SystemDetail.vue by removing unnecessary navigation actions for a more streamlined UI. - Enhanced conditional rendering in SystemDetailTabDependency to ensure form value checks are safe. - Replaced el-input with cl-edit-input in SystemDetailTabCustomize for improved input handling and added change event for saving. --- core/controllers/setting.go | 3 ++- frontend/crawlab-ui/src/store/modules/system.ts | 12 ++++++++---- .../src/views/system/detail/SystemDetail.vue | 7 ------- .../system/detail/tabs/SystemDetailTabCustomize.vue | 3 ++- .../system/detail/tabs/SystemDetailTabDependency.vue | 2 +- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/core/controllers/setting.go b/core/controllers/setting.go index a29ce468..18955e8c 100644 --- a/core/controllers/setting.go +++ b/core/controllers/setting.go @@ -1,6 +1,7 @@ package controllers import ( + "errors" "github.com/crawlab-team/crawlab/core/models/models" "github.com/crawlab-team/crawlab/core/models/service" "github.com/gin-gonic/gin" @@ -16,7 +17,7 @@ func GetSetting(_ *gin.Context, params *GetSettingParams) (response *Response[mo // setting s, err := service.NewModelService[models.Setting]().GetOne(bson.M{"key": params.Key}, nil) if err != nil { - if err == mongo.ErrNoDocuments { + if errors.Is(err, mongo.ErrNoDocuments) { return GetDataResponse(models.Setting{}) } return GetErrorResponse[models.Setting](err) diff --git a/frontend/crawlab-ui/src/store/modules/system.ts b/frontend/crawlab-ui/src/store/modules/system.ts index cdea5b25..d1b6b19e 100644 --- a/frontend/crawlab-ui/src/store/modules/system.ts +++ b/frontend/crawlab-ui/src/store/modules/system.ts @@ -34,8 +34,12 @@ const actions = { { key }: { key: string } ) => { const res = await get(`/settings/${key}`); - const resData = res.data || getDefaultSetting(key); - commit('setSetting', { key, value: resData }); + const setting = res.data || getDefaultSetting(key); + if (!setting.value) { + setting.value = {}; + } + console.debug(setting) + commit('setSetting', { key, value: setting }); }, saveSetting: async ( _: StoreActionContext, @@ -48,9 +52,9 @@ const actions = { } ) => { if (!value._id) { - await post(`/settings/${key}`, value); + await post(`/settings/${key}`, {data: value}); } else { - await put(`/settings/${key}`, value); + await put(`/settings/${key}`, {data: value}); } }, } as SystemStoreActions; diff --git a/frontend/crawlab-ui/src/views/system/detail/SystemDetail.vue b/frontend/crawlab-ui/src/views/system/detail/SystemDetail.vue index 542e0f71..18affb8a 100644 --- a/frontend/crawlab-ui/src/views/system/detail/SystemDetail.vue +++ b/frontend/crawlab-ui/src/views/system/detail/SystemDetail.vue @@ -59,13 +59,6 @@ defineOptions({ name: 'ClSystemDetail' });