feat: improve parsing quest error message

This commit is contained in:
Lin, Hung-Shin committed 2023-11-26 18:31:45 +08:00
1 parent bb968706ad
commit fe69841622
2 files changed
+17 -3

No files matched your search

+5
View File
@@ -38,7 +38,12 @@ async function main() {
await setupTest() await setupTest()
} }
try {
await loadAndUpdateQuests() await loadAndUpdateQuests()
} catch (err) {
logger.error('Failed to load quests', err)
return
}
expiryRemovalScheduler() expiryRemovalScheduler()
const file = await fs.readFile('./swagger.yaml', 'utf8') const file = await fs.readFile('./swagger.yaml', 'utf8')
+12 -3
View File
@@ -7,6 +7,15 @@ import logger from './logger.js'
const questDirectory = path.join(process.cwd(), '..', 'quests') const questDirectory = path.join(process.cwd(), '..', 'quests')
class QuestValidationError extends Error {
constructor(message, questId, error) {
super(message)
this.name = 'QuestValidationError'
this.questId = questId
this.error = error
}
}
async function mapQuests(callback) { async function mapQuests(callback) {
const questNames = await fs.readdir(questDirectory, { const questNames = await fs.readdir(questDirectory, {
withFileTypes: true, withFileTypes: true,
@@ -28,7 +37,7 @@ export async function loadAndUpdateQuests() {
return mapQuests(async (id, questPath) => { return mapQuests(async (id, questPath) => {
const files = await fs.readdir(questPath) const files = await fs.readdir(questPath)
if (!files.includes('game.yml')) { if (!files.includes('game.yml')) {
throw new Error(`Quest ${id} is missing game.yml`) throw new QuestValidationError(`Missing game.yml`, id)
} }
try { try {
@@ -46,8 +55,8 @@ export async function loadAndUpdateQuests() {
}) })
await quest.save() await quest.save()
} catch (error) { } catch (error) {
logger.error(`Error parsing quest ${id}:`, error) logger.error(`Error parsing quest`, { id, error })
throw error throw new QuestValidationError(`Error parsing quest`, id, error)
} }
}) })
} }