Some error handling

This commit is contained in:
Henry Jameson 2019-12-26 14:12:35 +02:00
parent b619477573
commit 43197c4243
2 changed files with 51 additions and 40 deletions

View file

@ -103,6 +103,10 @@ const settings = {
promise.then(() => { promise.then(() => {
this.$store.dispatch('setOption', { name: 'useStreamingApi', value }) this.$store.dispatch('setOption', { name: 'useStreamingApi', value })
}).catch((e) => {
console.error('Failed starting MastoAPI Streaming socket', e)
this.$store.dispatch('disableMastoSockets')
this.$store.dispatch('setOption', { name: 'useStreamingApi', value: false })
}) })
} }
} }

View file

@ -35,60 +35,67 @@ const api = {
enableMastoSockets (store) { enableMastoSockets (store) {
const { state, dispatch } = store const { state, dispatch } = store
if (state.mastoUserSocket) return if (state.mastoUserSocket) return
dispatch('startMastoUserSocket') return dispatch('startMastoUserSocket')
}, },
disableMastoSockets (store) { disableMastoSockets (store) {
const { state, dispatch } = store const { state, dispatch } = store
if (!state.mastoUserSocket) return if (!state.mastoUserSocket) return
dispatch('stopMastoUserSocket') return dispatch('stopMastoUserSocket')
}, },
// MastoAPI 'User' sockets // MastoAPI 'User' sockets
startMastoUserSocket (store) { startMastoUserSocket (store) {
const { state, dispatch } = store return new Promise((resolve, reject) => {
state.mastoUserSocket = state.backendInteractor.startUserSocket({ store }) try {
state.mastoUserSocket.addEventListener( const { state, dispatch } = store
'message', state.mastoUserSocket = state.backendInteractor.startUserSocket({ store })
({ detail: message }) => { state.mastoUserSocket.addEventListener(
if (!message) return // pings 'message',
if (message.event === 'notification') { ({ detail: message }) => {
dispatch('addNewNotifications', { if (!message) return // pings
notifications: [message.notification], if (message.event === 'notification') {
older: false dispatch('addNewNotifications', {
}) notifications: [message.notification],
} else if (message.event === 'update') { older: false
dispatch('addNewStatuses', { })
statuses: [message.status], } else if (message.event === 'update') {
userId: false, dispatch('addNewStatuses', {
showImmediately: false, statuses: [message.status],
timeline: 'friends' userId: false,
}) showImmediately: false,
} timeline: 'friends'
} })
) }
state.mastoUserSocket.addEventListener('error', ({ detail: error }) => { }
console.error('Error in MastoAPI websocket:', error) )
}) state.mastoUserSocket.addEventListener('error', ({ detail: error }) => {
state.mastoUserSocket.addEventListener('close', ({ detail: closeEvent }) => { console.error('Error in MastoAPI websocket:', error)
const ignoreCodes = new Set([ })
1000, // Normal (intended) closure state.mastoUserSocket.addEventListener('close', ({ detail: closeEvent }) => {
1001 // Going away const ignoreCodes = new Set([
]) 1000, // Normal (intended) closure
const { code } = closeEvent 1001 // Going away
if (ignoreCodes.has(code)) { ])
console.debug(`Not restarting socket becasue of closure code ${code} is in ignore list`) const { code } = closeEvent
} else { if (ignoreCodes.has(code)) {
console.warn(`MastoAPI websocket disconnected, restarting. CloseEvent code: ${code}`) console.debug(`Not restarting socket becasue of closure code ${code} is in ignore list`)
dispatch('startFetchingTimeline', { timeline: 'friends' }) } else {
dispatch('startFetchingNotifications') console.warn(`MastoAPI websocket disconnected, restarting. CloseEvent code: ${code}`)
dispatch('restartMastoUserSocket') dispatch('startFetchingTimeline', { timeline: 'friends' })
dispatch('startFetchingNotifications')
dispatch('restartMastoUserSocket')
}
})
resolve()
} catch (e) {
reject(e)
} }
}) })
}, },
restartMastoUserSocket ({ dispatch }) { restartMastoUserSocket ({ dispatch }) {
// This basically starts MastoAPI user socket and stops conventional // This basically starts MastoAPI user socket and stops conventional
// fetchers when connection reestablished // fetchers when connection reestablished
dispatch('startMastoUserSocket').then(() => { return dispatch('startMastoUserSocket').then(() => {
dispatch('stopFetchingTimeline', { timeline: 'friends' }) dispatch('stopFetchingTimeline', { timeline: 'friends' })
dispatch('stopFetchingNotifications') dispatch('stopFetchingNotifications')
}) })