fix(datasource): improve error handling and user feedback in DataSource components

- Updated error handling in DataSourceSettings and DataSourceEditorDialog to provide more informative messages.
- Enhanced the UI to display error details more clearly, improving user experience during connection tests and resource loading.
- Adjusted HTTP response status checks in request utility for better accuracy in handling success responses.
This commit is contained in:
wizardchen
2026-03-30 15:08:57 +08:00
committed by lyingbug
parent 8021647929
commit 36dac0120a
4 changed files with 82 additions and 24 deletions
+13 -4
View File
@@ -82,7 +82,7 @@ instance.interceptors.response.use(
(response) => {
// 根据业务状态码处理逻辑
const { status, data } = response;
if (status === 200 || status === 201) {
if (status >= 200 && status < 300) {
return data;
} else {
return Promise.reject(data);
@@ -190,9 +190,18 @@ instance.interceptors.response.use(
// 将HTTP状态码一并抛出,方便上层判断401等场景
// 后端返回格式: { success: false, error: { code, message, details } }
// 提取 error.message 作为顶层 message,方便前端使用 error?.message 获取
const errorMessage = typeof data === 'object' && data?.error?.message
? data.error.message
: (typeof data === 'object' ? data?.message : data);
let errorMessage: string | undefined;
if (typeof data === 'object') {
if (typeof data?.error === 'string') {
errorMessage = data.error;
} else if (data?.error?.message) {
errorMessage = data.error.message;
} else {
errorMessage = data?.message;
}
} else if (typeof data === 'string') {
errorMessage = data;
}
return Promise.reject({
status,
message: errorMessage,
@@ -207,7 +207,7 @@ async function testConnection() {
MessagePlugin.success(t('datasource.testSuccess'))
} catch (e: any) {
testResult.value = 'error'
testErrorMsg.value = e?.response?.data?.error || e?.message || ''
testErrorMsg.value = e?.message || e?.error || ''
MessagePlugin.error(t('datasource.testFailed'))
}
testing.value = false
@@ -234,8 +234,8 @@ async function loadResources() {
const res = await listResources(tempDsId.value)
resources.value = res?.data || res || []
} catch {
MessagePlugin.error(t('datasource.resourceLoadFailed'))
} catch (e: any) {
MessagePlugin.error(e?.message || e?.error || t('datasource.resourceLoadFailed'))
}
loadingResources.value = false
}
@@ -300,7 +300,7 @@ async function handleSubmit() {
emit('saved')
visible.value = false
} catch (e: any) {
MessagePlugin.error(e?.message || t('datasource.saveFailed'))
MessagePlugin.error(e?.message || e?.error || t('datasource.saveFailed'))
}
submitting.value = false
}
@@ -478,12 +478,18 @@ const stepTitles = computed(() => [
<t-button variant="outline" :loading="testing" @click="testConnection">
{{ t('datasource.testConnection') }}
</t-button>
<span v-if="testResult === 'success'" class="test-ok">{{ t('datasource.connected') }}</span>
<span v-if="testResult === 'error'" class="test-fail">
{{ t('datasource.connectionFailed') }}
<span v-if="testErrorMsg" class="test-error-detail">- {{ testErrorMsg }}</span>
<span v-if="testResult === 'success'" class="test-ok">
<t-icon name="check-circle-filled" size="14px" />
{{ t('datasource.connected') }}
</span>
</div>
<div v-if="testResult === 'error'" class="test-error-box">
<t-icon name="error-circle-filled" size="16px" />
<div class="test-error-content">
<span class="test-error-title">{{ t('datasource.connectionFailed') }}</span>
<span v-if="testErrorMsg" class="test-error-detail">{{ testErrorMsg }}</span>
</div>
</div>
<div class="ds-dialog-footer">
<t-button variant="outline" @click="step = 0" v-if="!isEdit">{{ t('datasource.back') }}</t-button>
@@ -799,9 +805,38 @@ const stepTitles = computed(() => [
.form-label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; color: var(--td-text-color-primary); }
.form-tip { font-size: 12px; color: var(--td-text-color-placeholder); margin: 4px 0 12px; }
.form-actions { display: flex; align-items: center; gap: 8px; margin-top: 12px; }
.test-ok { color: #00a870; font-size: 13px; }
.test-fail { color: #e34d59; font-size: 13px; }
.test-error-detail { font-size: 11px; color: var(--td-text-color-placeholder); }
.test-ok { color: var(--td-success-color); font-size: 13px; display: flex; align-items: center; gap: 4px; }
.test-error-box {
display: flex;
align-items: flex-start;
gap: 8px;
margin-top: 10px;
padding: 10px 14px;
border-radius: 8px;
background: var(--td-error-color-1);
color: var(--td-error-color);
font-size: 13px;
line-height: 20px;
}
.test-error-content {
display: flex;
flex-direction: column;
gap: 2px;
min-width: 0;
}
.test-error-title {
font-weight: 500;
}
.test-error-detail {
font-size: 12px;
color: var(--td-error-color);
opacity: 0.8;
word-break: break-word;
}
.ds-dialog-footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--td-border-level-2-color); }
@@ -67,8 +67,8 @@ function handleDelete(ds: DataSource) {
MessagePlugin.success(t('datasource.deleteSuccess'))
await loadList()
confirmDialog.hide()
} catch {
MessagePlugin.error(t('datasource.deleteFailed'))
} catch (e: any) {
MessagePlugin.error(e?.message || e?.error || t('datasource.deleteFailed'))
}
},
})
@@ -79,8 +79,8 @@ async function handleSync(ds: DataSource) {
await triggerSync(ds.id)
MessagePlugin.success(t('datasource.syncTriggered'))
loadList()
} catch {
MessagePlugin.error(t('datasource.syncFailed'))
} catch (e: any) {
MessagePlugin.error(e?.message || e?.error || t('datasource.syncFailed'))
}
}
@@ -89,8 +89,8 @@ async function handlePause(ds: DataSource) {
await pauseDataSource(ds.id)
MessagePlugin.success(t('datasource.paused'))
loadList()
} catch {
MessagePlugin.error(t('datasource.pauseFailed'))
} catch (e: any) {
MessagePlugin.error(e?.message || e?.error || t('datasource.pauseFailed'))
}
}
@@ -99,8 +99,8 @@ async function handleResume(ds: DataSource) {
await resumeDataSource(ds.id)
MessagePlugin.success(t('datasource.resumed'))
loadList()
} catch {
MessagePlugin.error(t('datasource.resumeFailed'))
} catch (e: any) {
MessagePlugin.error(e?.message || e?.error || t('datasource.resumeFailed'))
}
}
@@ -281,11 +281,13 @@ func (s *DataSourceService) ManualSync(ctx context.Context, dsID string) (*types
_, err = s.taskEnqueuer.Enqueue(task, asynq.Queue("default"))
if err != nil {
logger.Errorf(ctx, "failed to enqueue sync task: %v", err)
// Update sync log as failed
syncLog.Status = types.SyncLogStatusFailed
syncLog.FinishedAt = timePtr(time.Now())
syncLog.ErrorMessage = err.Error()
_ = s.syncLogRepo.Update(ctx, syncLog)
ds.Status = types.DataSourceStatusError
ds.ErrorMessage = fmt.Sprintf("Failed to enqueue sync: %v", err)
_ = s.dsRepo.Update(ctx, ds)
return nil, err
}
@@ -386,6 +388,9 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
syncLog.FinishedAt = timePtr(time.Now())
syncLog.ErrorMessage = fmt.Sprintf("Connector not found: %s", ds.Type)
_ = s.syncLogRepo.Update(ctx, syncLog)
ds.Status = types.DataSourceStatusError
ds.ErrorMessage = syncLog.ErrorMessage
_ = s.dsRepo.Update(ctx, ds)
return err
}
@@ -397,6 +402,9 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
syncLog.FinishedAt = timePtr(time.Now())
syncLog.ErrorMessage = fmt.Sprintf("Invalid configuration: %v", err)
_ = s.syncLogRepo.Update(ctx, syncLog)
ds.Status = types.DataSourceStatusError
ds.ErrorMessage = syncLog.ErrorMessage
_ = s.dsRepo.Update(ctx, ds)
return err
}
@@ -422,6 +430,9 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
syncLog.FinishedAt = timePtr(time.Now())
syncLog.ErrorMessage = fmt.Sprintf("Fetch failed: %v", fetchErr)
_ = s.syncLogRepo.Update(ctx, syncLog)
ds.Status = types.DataSourceStatusError
ds.ErrorMessage = syncLog.ErrorMessage
_ = s.dsRepo.Update(ctx, ds)
return fetchErr
}
@@ -440,6 +451,9 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
syncLog.FinishedAt = timePtr(time.Now())
syncLog.ErrorMessage = fmt.Sprintf("Failed to get tenant info: %v", err)
_ = s.syncLogRepo.Update(ctx, syncLog)
ds.Status = types.DataSourceStatusError
ds.ErrorMessage = syncLog.ErrorMessage
_ = s.dsRepo.Update(ctx, ds)
return err
}
ctx = context.WithValue(ctx, types.TenantInfoContextKey, tenant)