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:
@@ -82,7 +82,7 @@ instance.interceptors.response.use(
|
|||||||
(response) => {
|
(response) => {
|
||||||
// 根据业务状态码处理逻辑
|
// 根据业务状态码处理逻辑
|
||||||
const { status, data } = response;
|
const { status, data } = response;
|
||||||
if (status === 200 || status === 201) {
|
if (status >= 200 && status < 300) {
|
||||||
return data;
|
return data;
|
||||||
} else {
|
} else {
|
||||||
return Promise.reject(data);
|
return Promise.reject(data);
|
||||||
@@ -190,9 +190,18 @@ instance.interceptors.response.use(
|
|||||||
// 将HTTP状态码一并抛出,方便上层判断401等场景
|
// 将HTTP状态码一并抛出,方便上层判断401等场景
|
||||||
// 后端返回格式: { success: false, error: { code, message, details } }
|
// 后端返回格式: { success: false, error: { code, message, details } }
|
||||||
// 提取 error.message 作为顶层 message,方便前端使用 error?.message 获取
|
// 提取 error.message 作为顶层 message,方便前端使用 error?.message 获取
|
||||||
const errorMessage = typeof data === 'object' && data?.error?.message
|
let errorMessage: string | undefined;
|
||||||
? data.error.message
|
if (typeof data === 'object') {
|
||||||
: (typeof data === 'object' ? data?.message : data);
|
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({
|
return Promise.reject({
|
||||||
status,
|
status,
|
||||||
message: errorMessage,
|
message: errorMessage,
|
||||||
|
|||||||
@@ -207,7 +207,7 @@ async function testConnection() {
|
|||||||
MessagePlugin.success(t('datasource.testSuccess'))
|
MessagePlugin.success(t('datasource.testSuccess'))
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
testResult.value = 'error'
|
testResult.value = 'error'
|
||||||
testErrorMsg.value = e?.response?.data?.error || e?.message || ''
|
testErrorMsg.value = e?.message || e?.error || ''
|
||||||
MessagePlugin.error(t('datasource.testFailed'))
|
MessagePlugin.error(t('datasource.testFailed'))
|
||||||
}
|
}
|
||||||
testing.value = false
|
testing.value = false
|
||||||
@@ -234,8 +234,8 @@ async function loadResources() {
|
|||||||
|
|
||||||
const res = await listResources(tempDsId.value)
|
const res = await listResources(tempDsId.value)
|
||||||
resources.value = res?.data || res || []
|
resources.value = res?.data || res || []
|
||||||
} catch {
|
} catch (e: any) {
|
||||||
MessagePlugin.error(t('datasource.resourceLoadFailed'))
|
MessagePlugin.error(e?.message || e?.error || t('datasource.resourceLoadFailed'))
|
||||||
}
|
}
|
||||||
loadingResources.value = false
|
loadingResources.value = false
|
||||||
}
|
}
|
||||||
@@ -300,7 +300,7 @@ async function handleSubmit() {
|
|||||||
emit('saved')
|
emit('saved')
|
||||||
visible.value = false
|
visible.value = false
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
MessagePlugin.error(e?.message || t('datasource.saveFailed'))
|
MessagePlugin.error(e?.message || e?.error || t('datasource.saveFailed'))
|
||||||
}
|
}
|
||||||
submitting.value = false
|
submitting.value = false
|
||||||
}
|
}
|
||||||
@@ -478,12 +478,18 @@ const stepTitles = computed(() => [
|
|||||||
<t-button variant="outline" :loading="testing" @click="testConnection">
|
<t-button variant="outline" :loading="testing" @click="testConnection">
|
||||||
{{ t('datasource.testConnection') }}
|
{{ t('datasource.testConnection') }}
|
||||||
</t-button>
|
</t-button>
|
||||||
<span v-if="testResult === 'success'" class="test-ok">{{ t('datasource.connected') }}</span>
|
<span v-if="testResult === 'success'" class="test-ok">
|
||||||
<span v-if="testResult === 'error'" class="test-fail">
|
<t-icon name="check-circle-filled" size="14px" />
|
||||||
{{ t('datasource.connectionFailed') }}
|
{{ t('datasource.connected') }}
|
||||||
<span v-if="testErrorMsg" class="test-error-detail">- {{ testErrorMsg }}</span>
|
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</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">
|
<div class="ds-dialog-footer">
|
||||||
<t-button variant="outline" @click="step = 0" v-if="!isEdit">{{ t('datasource.back') }}</t-button>
|
<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-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-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; }
|
.form-actions { display: flex; align-items: center; gap: 8px; margin-top: 12px; }
|
||||||
.test-ok { color: #00a870; font-size: 13px; }
|
.test-ok { color: var(--td-success-color); font-size: 13px; display: flex; align-items: center; gap: 4px; }
|
||||||
.test-fail { color: #e34d59; font-size: 13px; }
|
|
||||||
.test-error-detail { font-size: 11px; color: var(--td-text-color-placeholder); }
|
.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); }
|
.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'))
|
MessagePlugin.success(t('datasource.deleteSuccess'))
|
||||||
await loadList()
|
await loadList()
|
||||||
confirmDialog.hide()
|
confirmDialog.hide()
|
||||||
} catch {
|
} catch (e: any) {
|
||||||
MessagePlugin.error(t('datasource.deleteFailed'))
|
MessagePlugin.error(e?.message || e?.error || t('datasource.deleteFailed'))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -79,8 +79,8 @@ async function handleSync(ds: DataSource) {
|
|||||||
await triggerSync(ds.id)
|
await triggerSync(ds.id)
|
||||||
MessagePlugin.success(t('datasource.syncTriggered'))
|
MessagePlugin.success(t('datasource.syncTriggered'))
|
||||||
loadList()
|
loadList()
|
||||||
} catch {
|
} catch (e: any) {
|
||||||
MessagePlugin.error(t('datasource.syncFailed'))
|
MessagePlugin.error(e?.message || e?.error || t('datasource.syncFailed'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,8 +89,8 @@ async function handlePause(ds: DataSource) {
|
|||||||
await pauseDataSource(ds.id)
|
await pauseDataSource(ds.id)
|
||||||
MessagePlugin.success(t('datasource.paused'))
|
MessagePlugin.success(t('datasource.paused'))
|
||||||
loadList()
|
loadList()
|
||||||
} catch {
|
} catch (e: any) {
|
||||||
MessagePlugin.error(t('datasource.pauseFailed'))
|
MessagePlugin.error(e?.message || e?.error || t('datasource.pauseFailed'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,8 +99,8 @@ async function handleResume(ds: DataSource) {
|
|||||||
await resumeDataSource(ds.id)
|
await resumeDataSource(ds.id)
|
||||||
MessagePlugin.success(t('datasource.resumed'))
|
MessagePlugin.success(t('datasource.resumed'))
|
||||||
loadList()
|
loadList()
|
||||||
} catch {
|
} catch (e: any) {
|
||||||
MessagePlugin.error(t('datasource.resumeFailed'))
|
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"))
|
_, err = s.taskEnqueuer.Enqueue(task, asynq.Queue("default"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf(ctx, "failed to enqueue sync task: %v", err)
|
logger.Errorf(ctx, "failed to enqueue sync task: %v", err)
|
||||||
// Update sync log as failed
|
|
||||||
syncLog.Status = types.SyncLogStatusFailed
|
syncLog.Status = types.SyncLogStatusFailed
|
||||||
syncLog.FinishedAt = timePtr(time.Now())
|
syncLog.FinishedAt = timePtr(time.Now())
|
||||||
syncLog.ErrorMessage = err.Error()
|
syncLog.ErrorMessage = err.Error()
|
||||||
_ = s.syncLogRepo.Update(ctx, syncLog)
|
_ = 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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -386,6 +388,9 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
|
|||||||
syncLog.FinishedAt = timePtr(time.Now())
|
syncLog.FinishedAt = timePtr(time.Now())
|
||||||
syncLog.ErrorMessage = fmt.Sprintf("Connector not found: %s", ds.Type)
|
syncLog.ErrorMessage = fmt.Sprintf("Connector not found: %s", ds.Type)
|
||||||
_ = s.syncLogRepo.Update(ctx, syncLog)
|
_ = s.syncLogRepo.Update(ctx, syncLog)
|
||||||
|
ds.Status = types.DataSourceStatusError
|
||||||
|
ds.ErrorMessage = syncLog.ErrorMessage
|
||||||
|
_ = s.dsRepo.Update(ctx, ds)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -397,6 +402,9 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
|
|||||||
syncLog.FinishedAt = timePtr(time.Now())
|
syncLog.FinishedAt = timePtr(time.Now())
|
||||||
syncLog.ErrorMessage = fmt.Sprintf("Invalid configuration: %v", err)
|
syncLog.ErrorMessage = fmt.Sprintf("Invalid configuration: %v", err)
|
||||||
_ = s.syncLogRepo.Update(ctx, syncLog)
|
_ = s.syncLogRepo.Update(ctx, syncLog)
|
||||||
|
ds.Status = types.DataSourceStatusError
|
||||||
|
ds.ErrorMessage = syncLog.ErrorMessage
|
||||||
|
_ = s.dsRepo.Update(ctx, ds)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -422,6 +430,9 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
|
|||||||
syncLog.FinishedAt = timePtr(time.Now())
|
syncLog.FinishedAt = timePtr(time.Now())
|
||||||
syncLog.ErrorMessage = fmt.Sprintf("Fetch failed: %v", fetchErr)
|
syncLog.ErrorMessage = fmt.Sprintf("Fetch failed: %v", fetchErr)
|
||||||
_ = s.syncLogRepo.Update(ctx, syncLog)
|
_ = s.syncLogRepo.Update(ctx, syncLog)
|
||||||
|
ds.Status = types.DataSourceStatusError
|
||||||
|
ds.ErrorMessage = syncLog.ErrorMessage
|
||||||
|
_ = s.dsRepo.Update(ctx, ds)
|
||||||
return fetchErr
|
return fetchErr
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -440,6 +451,9 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
|
|||||||
syncLog.FinishedAt = timePtr(time.Now())
|
syncLog.FinishedAt = timePtr(time.Now())
|
||||||
syncLog.ErrorMessage = fmt.Sprintf("Failed to get tenant info: %v", err)
|
syncLog.ErrorMessage = fmt.Sprintf("Failed to get tenant info: %v", err)
|
||||||
_ = s.syncLogRepo.Update(ctx, syncLog)
|
_ = s.syncLogRepo.Update(ctx, syncLog)
|
||||||
|
ds.Status = types.DataSourceStatusError
|
||||||
|
ds.ErrorMessage = syncLog.ErrorMessage
|
||||||
|
_ = s.dsRepo.Update(ctx, ds)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ctx = context.WithValue(ctx, types.TenantInfoContextKey, tenant)
|
ctx = context.WithValue(ctx, types.TenantInfoContextKey, tenant)
|
||||||
|
|||||||
Reference in New Issue
Block a user