refactor: improve messaging about websocket errors

This commit is contained in:
arabcoders 2025-11-12 18:08:43 +03:00
parent ad3a3ddb72
commit 70ff5e79c5
2 changed files with 23 additions and 4 deletions

View file

@ -154,13 +154,19 @@
<div> <div>
<NuxtLoadingIndicator /> <NuxtLoadingIndicator />
<NuxtPage v-if="config.is_loaded" :isLoading="loadingImage" @reload_bg="() => loadImage(true)" /> <NuxtPage v-if="config.is_loaded" :isLoading="loadingImage" @reload_bg="() => loadImage(true)" />
<Message v-if="!config.is_loaded" class="has-background-info-90 has-text-dark mt-5" <Message v-if="!config.is_loaded" class="mt-5" :newStyle="true"
title="Loading Configuration" icon="fas fa-spinner fa-spin"> title="Loading Configuration" icon="fas fa-spinner fa-spin">
<p>Loading application configuration. This usually takes less than a second.</p> <p>Loading application configuration. This usually takes less than a second.</p>
<p v-if="!socket.isConnected" class="mt-2"> <p v-if="!socket.isConnected" class="mt-2">
If this is taking too long, please check that the backend server is running and that the WebSocket If this is taking too long, please check that the backend server is running and that the WebSocket
connection is functional. connection is functional.
</p> </p>
<p v-if="socket.error" class="has-text-danger">
<span class="icon-text">
<span class="icon"><i class="fas fa-triangle-exclamation" /></span>
{{ socket.error }}
</span>
</p>
</Message> </Message>
<Markdown @closeModel="() => doc.file = ''" :file="doc.file" v-if="doc.file" /> <Markdown @closeModel="() => doc.file = ''" :file="doc.file" v-if="doc.file" />
<ClientOnly> <ClientOnly>
@ -182,8 +188,8 @@
- <NuxtLink @click="doc.file = '/api/docs/README.md'">README</NuxtLink> - <NuxtLink @click="doc.file = '/api/docs/README.md'">README</NuxtLink>
- <NuxtLink @click="doc.file = '/api/docs/API.md'">API</NuxtLink> - <NuxtLink @click="doc.file = '/api/docs/API.md'">API</NuxtLink>
- <NuxtLink @click="scrollToTop"> - <NuxtLink @click="scrollToTop">
<span class="icon"><i class="fas fa-arrow-up" /></span> <span class="icon"><i class="fas fa-arrow-up" /></span>
<span>Top</span> <span>Top</span>
</NuxtLink> </NuxtLink>
</div> </div>
</div> </div>

View file

@ -13,6 +13,7 @@ export const useSocketStore = defineStore('socket', () => {
const socket = ref<IOSocket | null>(null) const socket = ref<IOSocket | null>(null)
const isConnected = ref<boolean>(false) const isConnected = ref<boolean>(false)
const connectionStatus = ref<connectionStatus>('disconnected') const connectionStatus = ref<connectionStatus>('disconnected')
const error = ref<string | null>(null)
const emit = (event: string, data?: any): any => socket.value?.emit(event, data) const emit = (event: string, data?: any): any => socket.value?.emit(event, data)
const on = (event: string | string[], callback: (...args: any[]) => void, withEvent: boolean = false) => { const on = (event: string | string[], callback: (...args: any[]) => void, withEvent: boolean = false) => {
@ -72,16 +73,26 @@ export const useSocketStore = defineStore('socket', () => {
connectionStatus.value = 'connecting'; connectionStatus.value = 'connecting';
socket.value = io(url, opts) socket.value = io(url, opts)
on("connect_error", (e: any) => console.error("Socket connection error:", e)); on("connect_error", (e: any) => {
isConnected.value = false
if (null === e || undefined === e) {
error.value = 'Connection error: Unknown error';
return;
}
error.value = `Connection error: ${e.type || 'Unknown'}: ${e.message || 'Unknown error'}`;
});
on('connect', () => { on('connect', () => {
isConnected.value = true isConnected.value = true
connectionStatus.value = 'connected'; connectionStatus.value = 'connected';
error.value = null;
}); });
on('disconnect', () => { on('disconnect', () => {
isConnected.value = false isConnected.value = false
connectionStatus.value = 'disconnected'; connectionStatus.value = 'disconnected';
error.value = 'Disconnected from server.';
}); });
on('configuration', stream => { on('configuration', stream => {
@ -100,6 +111,7 @@ export const useSocketStore = defineStore('socket', () => {
config.add('folders', json.data.folders) config.add('folders', json.data.folders)
stateStore.addAll('queue', json.data.queue || {}) stateStore.addAll('queue', json.data.queue || {})
stateStore.addAll('history', json.data.done || {}) stateStore.addAll('history', json.data.done || {})
error.value = null;
}) })
on('item_added', stream => { on('item_added', stream => {
@ -215,5 +227,6 @@ export const useSocketStore = defineStore('socket', () => {
socket, isConnected, socket, isConnected,
getSessionId, getSessionId,
connectionStatus: readonly(connectionStatus) as Readonly<Ref<connectionStatus>>, connectionStatus: readonly(connectionStatus) as Readonly<Ref<connectionStatus>>,
error: readonly(error) as Readonly<Ref<string | null>>,
}; };
}); });