diff --git a/src/App.vue b/src/App.vue index cb1ffbe897..612e094235 100644 --- a/src/App.vue +++ b/src/App.vue @@ -32,19 +32,18 @@ + + diff --git a/src/eventBus.ts b/src/eventBus.ts index b0230b55b6..c1fbc0effa 100644 --- a/src/eventBus.ts +++ b/src/eventBus.ts @@ -1,2 +1,30 @@ import Vue from 'vue' -export default new Vue() + +export const EventBus = { + bus: new Vue(), + $emit: (text?: string, type?: FlashMessageTypes, timeout?: number): void => { + const opts: FlashMessage = { + open: true + } + if (text) opts.text = text + if (type) opts.type = type + if (timeout) opts.timeout = timeout + + EventBus.bus.$emit('flashMessage', opts) // custom message + } +} + +export interface FlashMessage { + type?: FlashMessageTypes; + open: boolean; + text?: string; + timeout?: number; +} + +export enum FlashMessageTypes { + success = 'success', + error = 'error', + warning = 'warning', + primary = 'primary', + secondary = 'secondary' +} diff --git a/src/mixins/files.ts b/src/mixins/files.ts index f994f684ed..ad1858af85 100644 --- a/src/mixins/files.ts +++ b/src/mixins/files.ts @@ -3,7 +3,7 @@ import Vue from 'vue' import { Component } from 'vue-property-decorator' import { getThumb } from '@/store/helpers' import { AxiosRequestConfig } from 'axios' -import EventBus from '@/eventBus' +// import { EventBus, FlashMessageTypes } from '@/eventBus' @Component export default class FilesMixin extends Vue { @@ -72,10 +72,15 @@ export default class FilesMixin extends Vue { encodeURI(this.apiUrl + '/server/files/' + filepath + '?date=' + new Date().getTime()), o ) - .then((response) => { - this.$store.dispatch('files/removeFileDownload') + .then(response => { return response }) + .catch(e => { + return e + }) + .finally(() => { + this.$store.dispatch('files/removeFileDownload') + }) } /** @@ -155,9 +160,14 @@ export default class FilesMixin extends Vue { } ) .then((response) => { - this.$store.dispatch('files/removeFileUpload', filepath) return response }) + .catch(e => { + return e + }) + .finally(() => { + this.$store.dispatch('files/removeFileUpload', filepath) + }) } // Upload some files. @@ -186,7 +196,8 @@ export default class FilesMixin extends Vue { try { await this.uploadFile(file, path, root, andPrint) } catch (e) { - EventBus.$emit('flashMessage', { type: 'error', text: `Error uploading ${file.name}
${e}` }) + return e + // EventBus.$emit('flashMessage', { type: 'error', text: `Error uploading ${file.name}
${e}` }) } } } diff --git a/src/plugins/axios.ts b/src/plugins/axios.ts index 132354355b..0fb3480c83 100644 --- a/src/plugins/axios.ts +++ b/src/plugins/axios.ts @@ -1,8 +1,45 @@ import _Vue from 'vue' -import Axios, { AxiosStatic } from 'axios' +import Axios, { AxiosResponse, AxiosStatic } from 'axios' +import { EventBus, FlashMessageTypes } from '@/eventBus' export function AxiosPlugin (Vue: typeof _Vue): void { - // do stuff with options + const responseInterceptor = (response: AxiosResponse) => { + switch (response.status) { + case 200: + // Works! + break + default: + } + return response + } + + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ + const errorInterceptor = (error: any) => { + // Check if its a network / server error. + if (!error.response) { + // Network / Server Error. + console.error('Network error') + return Promise.reject(error) + } + + // All other errors + let message: string | undefined + if (error.response.data) message = error.response.data + switch (error.response.status) { + case 400: + console.error(error.response.status, error.message, message) + EventBus.$emit(message || 'Server error', FlashMessageTypes.error, 5000) + break + default: + console.error(error.response.status, error.message) + EventBus.$emit(message || 'Server error', FlashMessageTypes.error, -1) + } + + return Promise.reject(error) + } + + Axios.interceptors.response.use(responseInterceptor, errorInterceptor) + Vue.prototype.$http = Axios Vue.$http = Axios } diff --git a/src/store/socket/actions.ts b/src/store/socket/actions.ts index 56a779a6ac..43d1b27da8 100644 --- a/src/store/socket/actions.ts +++ b/src/store/socket/actions.ts @@ -5,7 +5,7 @@ import { SocketState } from './types' import { RootState } from '../types' import { Globals } from '@/globals' import { SocketActions } from '@/socketActions' -import EventBus from '@/eventBus' +import { EventBus, FlashMessageTypes } from '@/eventBus' let retryTimeout: number @@ -82,7 +82,7 @@ export const actions: ActionTree = { message = payload.message } - EventBus.$emit('flashMessage', { type: 'error', text: message }) + EventBus.$emit(message, FlashMessageTypes.error, 5000) } if (payload.code === 503) { // This indicates klippy is non-responsive, or there's a configuration error