refactor(ui): refine vue.js app init error message
[e-mobility-charging-stations-simulator.git] / ui / web / src / main.ts
1 import { createApp } from 'vue'
2 import ToastPlugin from 'vue-toast-notification'
3 import type { ConfigurationData, ResponsePayload } from '@/types'
4 import { router } from '@/router'
5 import { UIClient } from '@/composables'
6 import App from '@/App.vue'
7 import 'vue-toast-notification/dist/theme-bootstrap.css'
8
9 const initializeApp = (config: ConfigurationData) => {
10 const app = createApp(App)
11 app.config.errorHandler = (error, instance, info) => {
12 console.error('Error:', error)
13 console.info('Vue instance:', instance)
14 console.info('Error info:', info)
15 // TODO: add code for UI notifications or other error handling logic
16 }
17 app.config.globalProperties.$configuration = config
18 app.config.globalProperties.$chargingStations = []
19 app.config.globalProperties.$uiClient = UIClient.getInstance(
20 app.config.globalProperties.$configuration.uiServer
21 )
22 app.config.globalProperties.$uiClient.registerWSEventListener('open', () => {
23 app.config.globalProperties.$uiClient
24 .listChargingStations()
25 .then((response: ResponsePayload) => {
26 app.config.globalProperties.$chargingStations = response.chargingStations
27 })
28 .catch((error: Error) => {
29 // TODO: add code for UI notifications or other error handling logic
30 console.error('Error at fetching charging stations:', error)
31 })
32 .finally(() => {
33 app.use(router).use(ToastPlugin).mount('#app')
34 })
35 })
36 }
37
38 fetch('/config.json')
39 .then(response => {
40 if (!response.ok) {
41 // TODO: add code for UI notifications or other error handling logic
42 console.error('Failed to fetch app configuration')
43 return
44 }
45 response
46 .json()
47 .then(config => {
48 try {
49 initializeApp(config)
50 } catch (error) {
51 // TODO: add code for UI notifications or other error handling logic
52 console.error('Error at initializing app:', error)
53 }
54 })
55 .catch(error => {
56 // TODO: add code for UI notifications or other error handling logic
57 console.error('Error at deserializing JSON app configuration:', error)
58 })
59 })
60 .catch(error => {
61 // TODO: add code for UI notifications or other error handling logic
62 console.error('Error at fetching app configuration:', error)
63 })