mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-07 19:10:12 +08:00
fix: enhance error handling in frontend
This commit is contained in:
1 parent
1f77328362
commit
d0c01fffa8
3 files changed
+41
-8
No files matched your search
@@ -4,6 +4,7 @@ import { FileGraph } from '@linux-odyssey/file-graph'
|
|||||||
import api from '../utils/api'
|
import api from '../utils/api'
|
||||||
import Socket from '../utils/socket'
|
import Socket from '../utils/socket'
|
||||||
import SocketTerminal from '../utils/terminal'
|
import SocketTerminal from '../utils/terminal'
|
||||||
|
import { LoadQuestError, LoadSessionError } from '../utils/errors'
|
||||||
|
|
||||||
const toast = useToast()
|
const toast = useToast()
|
||||||
|
|
||||||
@@ -32,7 +33,7 @@ const socket = new Socket()
|
|||||||
const term = new SocketTerminal(40, 80)
|
const term = new SocketTerminal(40, 80)
|
||||||
|
|
||||||
function setQuest(questId) {
|
function setQuest(questId) {
|
||||||
api.get(`/quests/${questId}`).then((res) => {
|
return api.get(`/quests/${questId}`).then((res) => {
|
||||||
store.questId = res.data._id
|
store.questId = res.data._id
|
||||||
store.quest = res.data
|
store.quest = res.data
|
||||||
})
|
})
|
||||||
@@ -108,9 +109,20 @@ export function reset() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function init(questId) {
|
export async function init(questId) {
|
||||||
|
if (!questId) throw new Error('No quest ID provided')
|
||||||
reset()
|
reset()
|
||||||
await setQuest(questId)
|
try {
|
||||||
await getActiveSession(questId)
|
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() {
|
function setup() {
|
||||||
|
|||||||
+14
-4
@@ -20,8 +20,18 @@ export class UnauthorizedError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export class LoadQuestError extends Error {
|
||||||
TooManyRequestsError,
|
constructor(message, questId) {
|
||||||
ValidationError,
|
super(message)
|
||||||
UnauthorizedError,
|
this.name = 'LoadQuestError'
|
||||||
|
this.questId = questId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class LoadSessionError extends Error {
|
||||||
|
constructor(message, questId) {
|
||||||
|
super(message)
|
||||||
|
this.name = 'LoadSessionError'
|
||||||
|
this.questId = questId
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,7 @@ import TerminalPart from '../components/TerminalPart.vue'
|
|||||||
import VisualizationPart from '../components/VisualizationPart.vue'
|
import VisualizationPart from '../components/VisualizationPart.vue'
|
||||||
import ControlPalette from '../components/ControlPalette.vue'
|
import ControlPalette from '../components/ControlPalette.vue'
|
||||||
import CompleteModal from '../components/CompleteModal.vue'
|
import CompleteModal from '../components/CompleteModal.vue'
|
||||||
|
import { LoadQuestError, LoadSessionError } from '../utils/errors'
|
||||||
|
|
||||||
const completed = computed(() => {
|
const completed = computed(() => {
|
||||||
return sessionStore.session.status === 'finished'
|
return sessionStore.session.status === 'finished'
|
||||||
@@ -28,8 +29,18 @@ onMounted(async () => {
|
|||||||
try {
|
try {
|
||||||
await init(props.questId)
|
await init(props.questId)
|
||||||
} catch (err) {
|
} 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)
|
console.error(err)
|
||||||
toast.error('Failed to initialize game session')
|
toast.error(err.message)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in new issue
Block a user