You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.2 KiB
51 lines
1.2 KiB
/**
|
|
* 验证异常类
|
|
* 处理数据验证失败的异常
|
|
*/
|
|
|
|
import BaseException from './BaseException.js'
|
|
|
|
export class ValidationException extends BaseException {
|
|
constructor(message = '数据验证失败', details = null) {
|
|
super(message, 400, 'VALIDATION_ERROR', details)
|
|
}
|
|
|
|
/**
|
|
* 创建字段验证失败异常
|
|
*/
|
|
static field(field, message) {
|
|
return new ValidationException(`字段验证失败: ${field}`, {
|
|
field,
|
|
message
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 创建多字段验证失败异常
|
|
*/
|
|
static fields(errors) {
|
|
return new ValidationException('数据验证失败', errors)
|
|
}
|
|
|
|
/**
|
|
* 创建必需字段异常
|
|
*/
|
|
static required(fields) {
|
|
const fieldList = Array.isArray(fields) ? fields.join(', ') : fields
|
|
return new ValidationException(`缺少必需字段: ${fieldList}`, {
|
|
missing: fields
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 创建格式错误异常
|
|
*/
|
|
static format(field, expectedFormat) {
|
|
return new ValidationException(`字段格式错误: ${field}`, {
|
|
field,
|
|
expectedFormat
|
|
})
|
|
}
|
|
}
|
|
|
|
export default ValidationException
|