fix: add error handling

This commit is contained in:
Lin, Hung-Shin committed 2023-11-24 12:52:01 +08:00
1 parent eb9118d5b1
commit c7f52b1003
1 file changed
+50 -32
+50 -32
View File
@@ -50,9 +50,18 @@ export default class CommandHandler extends SessionHandler {
} }
async discoverHandler() { async discoverHandler() {
const graph = new FileGraph(this.session.graph) try {
graph.discover(this.params.discover) const graph = new FileGraph(this.session.graph)
this.session.graph = graph graph.discover(this.params.discover)
this.session.graph = graph
} catch (e) {
logger.error('Update file graph failed', {
error: e,
session: this.session.id,
graph: this.session.graph,
discover: this.params.discover,
})
}
} }
async isMatch(condition) { async isMatch(condition) {
@@ -111,37 +120,46 @@ export default class CommandHandler extends SessionHandler {
} }
async run() { async run() {
this.quest = await Quest.findById(this.session.quest) try {
const stages = this.getStages() this.quest = await Quest.findById(this.session.quest)
if (stages.length === 0) { const stages = this.getStages()
logger.warn('stage not found', this.session.tasks) if (stages.length === 0) {
return {} logger.warn('stage not found', this.session.tasks)
} return {}
this.handleEvent()
this.handleCommand()
const matches = await Promise.all(
stages.map((stage) => this.isMatch(stage.condition))
)
const stage = stages.find((_, i) => matches[i])
if (!stage) {
for (const s of stages) {
// eslint-disable-next-line no-await-in-loop
const exception = await this.checkException(s)
if (exception) {
return this.executeException(exception)
}
} }
return {}
}
const response = this.execute(stage)
return { this.handleEvent()
stage: stage.id, this.handleCommand()
end: this.session.status === 'finished',
...response, const matches = await Promise.all(
stages.map((stage) => this.isMatch(stage.condition))
)
const stage = stages.find((_, i) => matches[i])
if (!stage) {
for (const s of stages) {
// eslint-disable-next-line no-await-in-loop
const exception = await this.checkException(s)
if (exception) {
return this.executeException(exception)
}
}
return {}
}
const response = this.execute(stage)
return {
stage: stage.id,
end: this.session.status === 'finished',
...response,
}
} catch (err) {
logger.error('CommandHandler error', {
error: err,
session: this.session.id,
command: this.commandInput,
})
return {}
} }
} }
} }