Skip to content

Commit

Permalink
feat(core): enhance domain error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 7, 2021
1 parent c689385 commit e2bc856
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
6 changes: 6 additions & 0 deletions packages/koishi-core/src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ template.set('internal', {
'invalid-option': '选项 {0} 输入无效,{1}',
'check-syntax': '输入帮助以查看用法。',

// parser
'invalid-number': '请提供一个数字。',
'invalid-integer': '请提供一个整数。',
'invalid-user': '请指定正确的用户。',
'invalid-channel': '请指定正确的频道。',

// suggest
'suggestion': '您要找的是不是{0}?',
'command-suggestion-prefix': '',
Expand Down
18 changes: 15 additions & 3 deletions packages/koishi-core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Domain {
text: string
user: string
channel: string
integer: number
}

export namespace Domain {
Expand Down Expand Up @@ -75,9 +76,20 @@ export namespace Domain {
}

create('string', source => source)
create('number', source => +source)
create('text', source => source)

create('number', (source) => {
const value = +source
if (value * 0 === 0) return value
throw new Error(template('internal.invalid-number'))
})

create('integer', (source) => {
const value = +source
if (value * 0 === 0 && Math.floor(value) === value) return value
throw new Error(template('interval.invalid-integer'))
})

create('user', (source, session) => {
if (source.startsWith('@')) {
source = source.slice(1)
Expand All @@ -88,7 +100,7 @@ export namespace Domain {
if (code && code.type === 'at') {
return `${session.platform}:${code.data.id}`
}
throw new Error('请指定正确的目标。')
throw new Error(template('internal.invalid-user'))
})

create('channel', (source, session) => {
Expand All @@ -101,7 +113,7 @@ export namespace Domain {
if (code && code.type === 'sharp') {
return `${session.platform}:${code.data.id}`
}
throw new Error('请指定正确的目标。')
throw new Error(template('internal.invalid-channel'))
})

const BRACKET_REGEXP = /<[^>]+>|\[[^\]]+\]/g
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-common/tests/admin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ before(async () => {

describe('Admin Commands', () => {
it('user/authorize', async () => {
await session.shouldReply('authorize -t nan', '选项 target 输入无效,请指定正确的目标。')
await session.shouldReply('authorize -t nan', '选项 target 输入无效,请指定正确的用户。')
await session.shouldReply('authorize -t @321', '未找到指定的用户。')
await session.shouldReply('authorize -t @789', '权限不足。')
await session.shouldReply('authorize -t @456 -1', '参数错误。')
Expand Down Expand Up @@ -89,10 +89,10 @@ describe('Admin Commands', () => {

it('channel/assign', async () => {
await app.session('123').shouldReply('assign', '当前不在群组上下文中,请使用 -t 参数指定目标频道。')
await session.shouldReply('assign -t nan', '选项 target 输入无效,请指定正确的目标。')
await session.shouldReply('assign -t nan', '选项 target 输入无效,请指定正确的频道。')
await session.shouldReply('assign -t #123', '未找到指定的频道。')
await session.shouldReply('assign -t #321', '频道数据未改动。')
await session.shouldReply('assign -t #321 nan', '参数 bot 输入无效,请指定正确的目标。')
await session.shouldReply('assign -t #321 nan', '参数 bot 输入无效,请指定正确的用户。')
})

it('channel/switch', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-teach/tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe('Teach Plugin', () => {

it('modify writer', async () => {
await u2.shouldReply('#1 -W', '问答 1 因权限过低无法修改。')
await u4g2.shouldReply('#1 -w foo', '选项 writer 输入无效,请指定正确的目标。')
await u4g2.shouldReply('#1 -w foo', '选项 writer 输入无效,请指定正确的用户。')
await u4g2.shouldReply('#1 -w [CQ:at,id=500]', '指定的目标用户不存在。')
await u4g2.shouldReply('#1 -w [CQ:at,id=200]', '问答 1 已成功修改。')

Expand Down

0 comments on commit e2bc856

Please sign in to comment.