fix: enhance error handling in frontend

This commit is contained in:
Lin, Hung-Shin committed 2023-11-17 11:18:14 +08:00
1 parent 1f77328362
commit d0c01fffa8
3 files changed
+41 -8

No files matched your search

+15 -3
View File
@@ -4,6 +4,7 @@ import { FileGraph } from '@linux-odyssey/file-graph'
import api from '../utils/api'
import Socket from '../utils/socket'
import SocketTerminal from '../utils/terminal'
import { LoadQuestError, LoadSessionError } from '../utils/errors'
const toast = useToast()
@@ -32,7 +33,7 @@ const socket = new Socket()
const term = new SocketTerminal(40, 80)
function setQuest(questId) {
api.get(`/quests/${questId}`).then((res) => {
return api.get(`/quests/${questId}`).then((res) => {
store.questId = res.data._id
store.quest = res.data
})
@@ -108,9 +109,20 @@ export function reset() {
}
export async function init(questId) {
if (!questId) throw new Error('No quest ID provided')
reset()
await setQuest(questId)
await getActiveSession(questId)
try {
await setQuest(questId)
} catch (err) {
console.error(err)
throw new LoadQuestError('Failed to load quest', questId)
}
try {
await getActiveSession(questId)
} catch (err) {
console.error(err)
throw new LoadSessionError('Failed to load session', questId)
}
}
function setup() {
+14 -4
View File
@@ -20,8 +20,18 @@ export class UnauthorizedError extends Error {
}
}
export default {
TooManyRequestsError,
ValidationError,
UnauthorizedError,
export class LoadQuestError extends Error {
constructor(message, questId) {
super(message)
this.name = 'LoadQuestError'
this.questId = questId
}
}
export class LoadSessionError extends Error {
constructor(message, questId) {
super(message)
this.name = 'LoadSessionError'
this.questId = questId
}
}
+12 -1
View File
@@ -10,6 +10,7 @@ import TerminalPart from '../components/TerminalPart.vue'
import VisualizationPart from '../components/VisualizationPart.vue'
import ControlPalette from '../components/ControlPalette.vue'
import CompleteModal from '../components/CompleteModal.vue'
import { LoadQuestError, LoadSessionError } from '../utils/errors'
const completed = computed(() => {
return sessionStore.session.status === 'finished'
@@ -28,8 +29,18 @@ onMounted(async () => {
try {
await init(props.questId)
} catch (err) {
if (err instanceof LoadQuestError) {
toast.error(`Failed to load quest: ${err.questId}. Please check the URL.`)
return
}
if (err instanceof LoadSessionError) {
toast.error(
`Failed to create session for quest: ${err.questId}. Please re-login and try again.`
)
return
}
console.error(err)
toast.error('Failed to initialize game session')
toast.error(err.message)
}
})
</script>