WIP some work on making errors less spammy

This commit is contained in:
Henry Jameson 2021-03-08 22:24:39 +02:00
parent 2e7bd99444
commit 90afcd3420
4 changed files with 43 additions and 32 deletions

View file

@ -664,8 +664,7 @@
"no_more_statuses": "No more statuses", "no_more_statuses": "No more statuses",
"no_statuses": "No statuses", "no_statuses": "No statuses",
"socket_reconnected": "Realtime connection established", "socket_reconnected": "Realtime connection established",
"socket_broke": "Realtime connection lost: CloseEvent code {0}", "socket_broke": "Realtime connection lost: CloseEvent code {0}"
"socket_closed": "No realtime connection, updates can arrive with a delaye until connection is re-established"
}, },
"status": { "status": {
"favorites": "Favorites", "favorites": "Favorites",

View file

@ -5,6 +5,8 @@ import { Socket } from 'phoenix'
const api = { const api = {
state: { state: {
connectionBroken: false,
retryMultiplier: 1,
backendInteractor: backendInteractorService(), backendInteractor: backendInteractorService(),
fetchers: {}, fetchers: {},
socket: null, socket: null,
@ -34,12 +36,18 @@ const api = {
}, },
setMastoUserSocketStatus (state, value) { setMastoUserSocketStatus (state, value) {
state.mastoUserSocketStatus = value state.mastoUserSocketStatus = value
},
recoverConnection (state) {
state.connectionBroken = false
},
breakConnection (state) {
state.connectionBroken = true
} }
}, },
actions: { actions: {
// Global MastoAPI socket control, in future should disable ALL sockets/(re)start relevant sockets // Global MastoAPI socket control, in future should disable ALL sockets/(re)start relevant sockets
enableMastoSockets (store) { enableMastoSockets (store) {
const { state, dispatch } = store const { state, dispatch, commit } = store
// Do not initialize unless nonexistent or closed // Do not initialize unless nonexistent or closed
if ( if (
state.mastoUserSocket && state.mastoUserSocket &&
@ -50,11 +58,13 @@ const api = {
) { ) {
return return
} }
commit('recoverConnection')
return dispatch('startMastoUserSocket') return dispatch('startMastoUserSocket')
}, },
disableMastoSockets (store) { disableMastoSockets (store) {
const { state, dispatch } = store const { state, dispatch, commit } = store
if (!state.mastoUserSocket) return if (!state.mastoUserSocket) return
commit('recoverConnection')
return dispatch('stopMastoUserSocket') return dispatch('stopMastoUserSocket')
}, },
@ -102,6 +112,7 @@ const api = {
state.mastoUserSocket.addEventListener('open', () => { state.mastoUserSocket.addEventListener('open', () => {
// Do not show notification when we just opened up the page // Do not show notification when we just opened up the page
if (state.mastoUserSocketStatus !== null) { if (state.mastoUserSocketStatus !== null) {
commit('recoverConnection')
dispatch('pushGlobalNotice', { dispatch('pushGlobalNotice', {
level: 'success', level: 'success',
messageKey: 'timeline.socket_reconnected', messageKey: 'timeline.socket_reconnected',
@ -114,15 +125,6 @@ const api = {
console.error('Error in MastoAPI websocket:', error) console.error('Error in MastoAPI websocket:', error)
commit('setMastoUserSocketStatus', WSConnectionStatus.ERROR) commit('setMastoUserSocketStatus', WSConnectionStatus.ERROR)
dispatch('clearOpenedChats') dispatch('clearOpenedChats')
/* Since data in WS event for error is useless it's better to show
* generic warning instead of in "close" which actually has some
* useful data
*/
dispatch('pushGlobalNotice', {
level: 'error',
messageKey: 'timeline.socket_closed',
timeout: 5000
})
}) })
state.mastoUserSocket.addEventListener('close', ({ detail: closeEvent }) => { state.mastoUserSocket.addEventListener('close', ({ detail: closeEvent }) => {
const ignoreCodes = new Set([ const ignoreCodes = new Set([
@ -137,13 +139,19 @@ const api = {
dispatch('startFetchingTimeline', { timeline: 'friends' }) dispatch('startFetchingTimeline', { timeline: 'friends' })
dispatch('startFetchingNotifications') dispatch('startFetchingNotifications')
dispatch('startFetchingChats') dispatch('startFetchingChats')
dispatch('restartMastoUserSocket') setTimeout(() => {
dispatch('pushGlobalNotice', { console.log('TEST')
level: 'error', dispatch('restartMastoUserSocket')
messageKey: 'timeline.socket_broke', }, 1000)
messageArgs: [code], if (!state.connectionBroken) {
timeout: 5000 dispatch('pushGlobalNotice', {
}) level: 'error',
messageKey: 'timeline.socket_broke',
messageArgs: [code],
timeout: 5000
})
}
commit('breakConnection')
} }
commit('setMastoUserSocketStatus', WSConnectionStatus.CLOSED) commit('setMastoUserSocketStatus', WSConnectionStatus.CLOSED)
dispatch('clearOpenedChats') dispatch('clearOpenedChats')

View file

@ -57,12 +57,14 @@ const fetchNotifications = ({ store, args, older }) => {
return notifications return notifications
}) })
.catch((error) => { .catch((error) => {
store.dispatch('pushGlobalNotice', { if (!store.rootState.api.connectionBroken) {
level: 'error', store.dispatch('pushGlobalNotice', {
messageKey: 'notifications.error', level: 'error',
messageArgs: [error.message], messageKey: 'notifications.error',
timeout: 5000 messageArgs: [error.message],
}) timeout: 5000
})
}
}) })
} }

View file

@ -66,12 +66,14 @@ const fetchAndUpdate = ({
return { statuses, pagination } return { statuses, pagination }
}) })
.catch((error) => { .catch((error) => {
store.dispatch('pushGlobalNotice', { if (!store.rootState.api.connectionBroken) {
level: 'error', store.dispatch('pushGlobalNotice', {
messageKey: 'timeline.error', level: 'error',
messageArgs: [error.message], messageKey: 'timeline.error',
timeout: 5000 messageArgs: [error.message],
}) timeout: 5000
})
}
}) })
} }