Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit c6831eb

Browse files
authored
refactor error message of luis build and qnamaker build (#1025)
1 parent 091464d commit c6831eb

File tree

9 files changed

+191
-142
lines changed

9 files changed

+191
-142
lines changed

packages/lu/src/parser/lubuild/builder.ts

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -207,71 +207,75 @@ export class Builder {
207207
const filesSectionEmptyStatus = fileHelper.filesSectionEmptyStatus(clonedLuContents)
208208
let isAllLuEmpty = [...filesSectionEmptyStatus.values()].every((isEmpty) => isEmpty)
209209

210-
if (!isAllLuEmpty) {
211-
const luBuildCore = new LuBuildCore(authoringKey, endpoint, retryCount, retryDuration)
212-
const apps = await luBuildCore.getApplicationList()
213-
214-
let settings: any
215-
216-
// here we do a while loop to make full use of luis tps capacity
217-
while (clonedLuContents.length > 0) {
218-
// get a number(set by luisApiTps) of contents for each loop
219-
const subLuContents = clonedLuContents.splice(0, luisAPITPS)
220-
221-
// concurrently handle applications
222-
await Promise.all(subLuContents.map(async content => {
223-
if (!filesSectionEmptyStatus.get(content.path)) {
224-
// init current application object from lu content
225-
let currentApp = await this.initApplicationFromLuContent(content, botName, suffix)
226-
227-
// init recognizer asset
228-
const dialogFile = path.join(path.dirname(content.path), `${content.name}.dialog`)
229-
let recognizer = new Recognizer(content.path, content.name, dialogFile, schema)
230-
231-
// find if there is a matched name with current app under current authoring key
232-
if (!recognizer.getAppId()) {
233-
for (let app of apps) {
234-
if (app.name === currentApp.name) {
235-
recognizer.setAppId(app.id)
236-
break
210+
try {
211+
if (!isAllLuEmpty) {
212+
const luBuildCore = new LuBuildCore(authoringKey, endpoint, retryCount, retryDuration)
213+
const apps = await luBuildCore.getApplicationList()
214+
215+
let settings: any
216+
217+
// here we do a while loop to make full use of luis tps capacity
218+
while (clonedLuContents.length > 0) {
219+
// get a number(set by luisApiTps) of contents for each loop
220+
const subLuContents = clonedLuContents.splice(0, luisAPITPS)
221+
222+
// concurrently handle applications
223+
await Promise.all(subLuContents.map(async content => {
224+
if (!filesSectionEmptyStatus.get(content.path)) {
225+
// init current application object from lu content
226+
let currentApp = await this.initApplicationFromLuContent(content, botName, suffix)
227+
228+
// init recognizer asset
229+
const dialogFile = path.join(path.dirname(content.path), `${content.name}.dialog`)
230+
let recognizer = new Recognizer(content.path, content.name, dialogFile, schema)
231+
232+
// find if there is a matched name with current app under current authoring key
233+
if (!recognizer.getAppId()) {
234+
for (let app of apps) {
235+
if (app.name === currentApp.name) {
236+
recognizer.setAppId(app.id)
237+
break
238+
}
237239
}
238240
}
239-
}
240241

241-
let needTrainAndPublish = false
242+
let needTrainAndPublish = false
242243

243-
// compare models to update the model if a match found
244-
// otherwise create a new application
245-
if (recognizer.getAppId() && recognizer.getAppId() !== '') {
246-
// To see if need update the model
247-
needTrainAndPublish = await this.updateApplication(currentApp, luBuildCore, recognizer, timeBucketOfRequests, keptVersionCount)
248-
} else {
249-
// create a new application
250-
needTrainAndPublish = await this.createApplication(currentApp, luBuildCore, recognizer, timeBucketOfRequests)
251-
}
244+
// compare models to update the model if a match found
245+
// otherwise create a new application
246+
if (recognizer.getAppId() && recognizer.getAppId() !== '') {
247+
// To see if need update the model
248+
needTrainAndPublish = await this.updateApplication(currentApp, luBuildCore, recognizer, timeBucketOfRequests, keptVersionCount)
249+
} else {
250+
// create a new application
251+
needTrainAndPublish = await this.createApplication(currentApp, luBuildCore, recognizer, timeBucketOfRequests)
252+
}
252253

253-
if (needTrainAndPublish) {
254-
// train and publish application
255-
await this.trainAndPublishApplication(luBuildCore, recognizer, timeBucketOfRequests, isStaging)
256-
}
254+
if (needTrainAndPublish) {
255+
// train and publish application
256+
await this.trainAndPublishApplication(luBuildCore, recognizer, timeBucketOfRequests, isStaging)
257+
}
257258

258-
// init settings asset
259-
if (settings === undefined) {
260-
const settingsPath = path.join(path.dirname(content.path), `luis.settings.${suffix}.${region}.json`)
261-
settings = new Settings(settingsPath, {})
262-
}
259+
// init settings asset
260+
if (settings === undefined) {
261+
const settingsPath = path.join(path.dirname(content.path), `luis.settings.${suffix}.${region}.json`)
262+
settings = new Settings(settingsPath, {})
263+
}
263264

264-
// update settings asset
265-
settings.luis[content.name.split('.').join('_').replace(/-/g, '_')] = {
266-
"appId": recognizer.getAppId(),
267-
"version": recognizer.versionId
265+
// update settings asset
266+
settings.luis[content.name.split('.').join('_').replace(/-/g, '_')] = {
267+
"appId": recognizer.getAppId(),
268+
"version": recognizer.versionId
269+
}
268270
}
269-
}
270-
}))
271-
}
271+
}))
272+
}
272273

273-
// write dialog assets
274-
settingsAssets.push(settings)
274+
// write dialog assets
275+
settingsAssets.push(settings)
276+
}
277+
} catch (error) {
278+
throw(new exception(retCode.errorCode.LUIS_BUILD_FAILED, `Luis build failed: ${error.message}`))
275279
}
276280

277281
const settingsContent = this.generateDeclarativeAssets(settingsAssets)

packages/lu/src/parser/lubuild/core.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import {LUISAuthoringClient} from '@azure/cognitiveservices-luis-authoring'
88
import fetch from 'node-fetch'
99

1010
const delay = require('delay')
11-
const retCode = require('./../utils/enums/CLI-errors')
12-
const exception = require('./../utils/exception')
1311
const Luis = require('./../luis/luis')
1412

1513
const rateLimitErrorCode = 429
@@ -30,7 +28,7 @@ export class LuBuildCore {
3028

3129
// check endpoint is absolute or not
3230
if (!absoluteUrlPattern.test(endpoint)) {
33-
throw (new exception(retCode.errorCode.INVALID_URI, `Only absolute URLs are supported. "${endpoint}" is not an absolute LUIS endpoint URL.`))
31+
throw new Error(`Only absolute URLs are supported. "${endpoint}" is not an absolute LUIS endpoint URL.`)
3432
}
3533

3634
// new luis api client
@@ -114,12 +112,12 @@ export class LuBuildCore {
114112
retryCount--
115113
if (retryCount > 0) await delay(this.retryDuration)
116114
} else {
117-
throw (new exception(retCode.errorCode.LUIS_API_CALL_FAILED, error.message))
115+
throw error
118116
}
119117
}
120118

121119
if (retryCount === 0) {
122-
throw (new exception(retCode.errorCode.LUIS_API_CALL_FAILED, error.message))
120+
throw error
123121
}
124122

125123
return messageData
@@ -156,7 +154,7 @@ export class LuBuildCore {
156154
}
157155

158156
if (messageData.error) {
159-
throw (new exception(retCode.errorCode.LUIS_API_CALL_FAILED, messageData.error.message))
157+
throw new Error(messageData.error.message)
160158
}
161159

162160
return messageData
@@ -236,12 +234,12 @@ export class LuBuildCore {
236234
retryCount--
237235
if (retryCount > 0) await delay(this.retryDuration)
238236
} else {
239-
throw (new exception(retCode.errorCode.LUIS_API_CALL_FAILED, error.message))
237+
throw error
240238
}
241239
}
242240

243241
if (retryCount === 0) {
244-
throw (new exception(retCode.errorCode.LUIS_API_CALL_FAILED, error.message))
242+
throw error
245243
}
246244

247245
return messageData

packages/lu/src/parser/qnabuild/builder.ts

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -189,73 +189,77 @@ export class Builder {
189189
})
190190
}
191191

192-
const qnaBuildCore = new QnaBuildCore(subscriptionkey, endpoint)
193-
const kbs = (await qnaBuildCore.getKBList()).knowledgebases
194-
195192
let settingsPerCulture = new Map<string, any>()
196193

197-
// here we do a while loop to make full use of qna tps capacity
198-
while (mergedContents.length > 0) {
199-
// get a number(set by qnaApiTps) of contents for each loop
200-
const subContents = mergedContents.splice(0, qnaAPITPS)
201-
202-
// concurrently handle applications
203-
await Promise.all(subContents.map(async content => {
204-
let qnamakerContent = content.qnamakerContent
205-
if (!fileHelper.isFileSectionEmpty(qnamakerContent)) {
206-
let currentQna = content.qnamakerObject
207-
208-
// set kb name
209-
if (!currentQna.kb.name) currentQna.kb.name = `${botName}(${suffix}).${qnamakerContent.language}.qna`
210-
211-
let currentKB = currentQna.kb
212-
let currentAlt = currentQna.alterations
213-
let hostName = ''
214-
let kbId = ''
215-
216-
// find if there is a matched name with current kb under current key
217-
for (let kb of kbs) {
218-
if (kb.name === currentKB.name) {
219-
kbId = kb.id
220-
hostName = kb.hostName
221-
break
194+
try {
195+
const qnaBuildCore = new QnaBuildCore(subscriptionkey, endpoint)
196+
const kbs = (await qnaBuildCore.getKBList()).knowledgebases
197+
198+
// here we do a while loop to make full use of qna tps capacity
199+
while (mergedContents.length > 0) {
200+
// get a number(set by qnaApiTps) of contents for each loop
201+
const subContents = mergedContents.splice(0, qnaAPITPS)
202+
203+
// concurrently handle applications
204+
await Promise.all(subContents.map(async content => {
205+
let qnamakerContent = content.qnamakerContent
206+
if (!fileHelper.isFileSectionEmpty(qnamakerContent)) {
207+
let currentQna = content.qnamakerObject
208+
209+
// set kb name
210+
if (!currentQna.kb.name) currentQna.kb.name = `${botName}(${suffix}).${qnamakerContent.language}.qna`
211+
212+
let currentKB = currentQna.kb
213+
let currentAlt = currentQna.alterations
214+
let hostName = ''
215+
let kbId = ''
216+
217+
// find if there is a matched name with current kb under current key
218+
for (let kb of kbs) {
219+
if (kb.name === currentKB.name) {
220+
kbId = kb.id
221+
hostName = kb.hostName
222+
break
223+
}
222224
}
223-
}
224225

225-
let needPublish = false
226-
227-
// compare models to update the model if a match found
228-
// otherwise create a new kb
229-
if (kbId !== '') {
230-
// To see if need update the model
231-
needPublish = await this.updateKB(currentKB, qnaBuildCore, kbId, timeBucketOfRequests)
232-
} else {
233-
// create a new kb
234-
kbId = await this.createKB(currentKB, qnaBuildCore, timeBucketOfRequests)
235-
needPublish = true
236-
}
226+
let needPublish = false
227+
228+
// compare models to update the model if a match found
229+
// otherwise create a new kb
230+
if (kbId !== '') {
231+
// To see if need update the model
232+
needPublish = await this.updateKB(currentKB, qnaBuildCore, kbId, timeBucketOfRequests)
233+
} else {
234+
// create a new kb
235+
kbId = await this.createKB(currentKB, qnaBuildCore, timeBucketOfRequests)
236+
needPublish = true
237+
}
237238

238-
if (needPublish) {
239-
// train and publish kb
240-
await this.publishKB(qnaBuildCore, kbId, currentKB.name, timeBucketOfRequests)
241-
}
239+
if (needPublish) {
240+
// train and publish kb
241+
await this.publishKB(qnaBuildCore, kbId, currentKB.name, timeBucketOfRequests)
242+
}
242243

243-
if (hostName === '') hostName = (await qnaBuildCore.getKB(kbId)).hostName
244+
if (hostName === '') hostName = (await qnaBuildCore.getKB(kbId)).hostName
244245

245-
hostName += '/qnamaker'
246+
hostName += '/qnamaker'
246247

247-
// update alterations if there are
248-
if (currentAlt.wordAlterations && currentAlt.wordAlterations.length > 0) {
249-
this.handler('Replacing alterations...\n')
250-
await qnaBuildCore.replaceAlt(currentAlt)
251-
}
248+
// update alterations if there are
249+
if (currentAlt.wordAlterations && currentAlt.wordAlterations.length > 0) {
250+
this.handler('Replacing alterations...\n')
251+
await qnaBuildCore.replaceAlt(currentAlt)
252+
}
252253

253-
settingsPerCulture.set(qnamakerContent.language, {
254-
kbId,
255-
hostName
256-
})
257-
}
258-
}))
254+
settingsPerCulture.set(qnamakerContent.language, {
255+
kbId,
256+
hostName
257+
})
258+
}
259+
}))
260+
}
261+
} catch (error) {
262+
throw(new exception(retCode.errorCode.QNAMAKER_BUILD_FAILED, `Qnamaker build failed: ${error.message}`))
259263
}
260264

261265
let settings: any

0 commit comments

Comments
 (0)