fix: improve readability of error messages

This commit is contained in:
Crane.z
2026-03-23 18:47:09 +08:00
committed by wrd
parent 737ed99271
commit 479f2e7338
2 changed files with 22 additions and 6 deletions

View File

@@ -30,6 +30,7 @@ import DataForm from '../DataForm/index.vue'
import FormGroupHeader from '@/components/Form/FormGroupHeader/index.vue'
import { FormFieldGenerator } from '@/components/Form/AutoDataForm/utils'
import { UniqueCheck } from '@/components/Form/DataForm/rules'
import { joinErrorMessages } from '@/utils/common'
export default {
name: 'AutoDataForm',
@@ -225,7 +226,7 @@ export default {
let msg = v
console.log(k, v)
// v是数组并且数组都是字符串则拼接为字符串
if (Array.isArray(v) && v.every(item => typeof item === 'string')) msg = v.join('; ')
if (Array.isArray(v) && v.every(item => typeof item === 'string')) msg = joinErrorMessages(v)
// 处理 [{"port":["请确保该值小于或者等于 65535。"]},{},{}] 这种情况
else if (Array.isArray(v) && v.every(item => _.isPlainObject(item))) {
const subMsg = []
@@ -236,7 +237,7 @@ export default {
}
})
})
msg = subMsg.join(' ')
msg = joinErrorMessages(subMsg, ' ')
} else if (typeof v === 'object' && v !== null) msg = JSON.stringify(v)
mapped[k] = String(msg || '')
})

View File

@@ -4,6 +4,22 @@ import { scopedLocalStorage as localStorage } from '@/utils/storage'
const _ = require('lodash')
function trimTrailingPunctuation(message) {
return String(message || '').replace(/[;\s]*[。.]+$/g, '').trim()
}
export function joinErrorMessages(messages, separator = '; ') {
if (!Array.isArray(messages)) return ''
const normalized = messages
.map(item => String(item || '').trim())
.filter(Boolean)
return normalized
.map((item, index) => index === normalized.length - 1 ? item : trimTrailingPunctuation(item))
.join(separator)
}
export function getApiPath(that, objectId) {
let pagePath = that.$route.path
const pagePathArray = pagePath.split('/')
@@ -142,12 +158,11 @@ export function getErrorResponseMsg(error) {
} else if (data && data['non_field_errors']) {
msg = data['non_field_errors'].join(' ')
} else if (Array.isArray(data)) {
msg = data
msg = joinErrorMessages(data
.map((item, i) => {
return getErrorResponseMsg(item)
})
.filter(i => i)
.join('; ')
.filter(i => i))
} else if (typeof data === 'string') {
return data
} else if (_.isPlainObject(data)) {
@@ -155,7 +170,7 @@ export function getErrorResponseMsg(error) {
.map(item => getErrorResponseMsg(item))
.filter(i => i)
// 错误信息不要重复提示
return [...new Set(msg)].join('; ')
return joinErrorMessages([...new Set(msg)])
} else {
msg = error.toString()
}