feat: improve error message for invalid API url
Resolves https://kolaente.dev/vikunja/frontend/issues/3680
This commit is contained in:
parent
44754fac0f
commit
725fd1ad46
@ -48,13 +48,14 @@ import Message from '@/components/misc/message.vue'
|
|||||||
import CustomTransition from '@/components/misc/CustomTransition.vue'
|
import CustomTransition from '@/components/misc/CustomTransition.vue'
|
||||||
import NoAuthWrapper from '@/components/misc/no-auth-wrapper.vue'
|
import NoAuthWrapper from '@/components/misc/no-auth-wrapper.vue'
|
||||||
|
|
||||||
import {ERROR_NO_API_URL} from '@/helpers/checkAndSetApiUrl'
|
import {ERROR_NO_API_URL, InvalidApiUrlProvidedError, NoApiUrlProvidedError} from '@/helpers/checkAndSetApiUrl'
|
||||||
import {useOnline} from '@/composables/useOnline'
|
import {useOnline} from '@/composables/useOnline'
|
||||||
|
|
||||||
import {getAuthForRoute} from '@/router'
|
import {getAuthForRoute} from '@/router'
|
||||||
|
|
||||||
import {useBaseStore} from '@/stores/base'
|
import {useBaseStore} from '@/stores/base'
|
||||||
import {useAuthStore} from '@/stores/auth'
|
import {useAuthStore} from '@/stores/auth'
|
||||||
|
import {useI18n} from 'vue-i18n'
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
@ -68,6 +69,8 @@ const online = useOnline()
|
|||||||
const error = ref('')
|
const error = ref('')
|
||||||
const showLoading = computed(() => !ready.value && error.value === '')
|
const showLoading = computed(() => !ready.value && error.value === '')
|
||||||
|
|
||||||
|
const {t} = useI18n()
|
||||||
|
|
||||||
async function load() {
|
async function load() {
|
||||||
try {
|
try {
|
||||||
await baseStore.loadApp()
|
await baseStore.loadApp()
|
||||||
@ -77,7 +80,15 @@ async function load() {
|
|||||||
await router.push(redirectTo)
|
await router.push(redirectTo)
|
||||||
}
|
}
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
error.value = String(e)
|
if (e instanceof NoApiUrlProvidedError) {
|
||||||
|
error.value = ERROR_NO_API_URL
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (e instanceof InvalidApiUrlProvidedError) {
|
||||||
|
error.value = t('apiConfig.error')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
error.value = String(e.message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,8 +4,27 @@ const API_DEFAULT_PORT = '3456'
|
|||||||
|
|
||||||
export const ERROR_NO_API_URL = 'noApiUrlProvided'
|
export const ERROR_NO_API_URL = 'noApiUrlProvided'
|
||||||
|
|
||||||
|
export class NoApiUrlProvidedError extends Error {
|
||||||
|
constructor() {
|
||||||
|
super()
|
||||||
|
this.message = 'No API URL provided'
|
||||||
|
this.name = 'NoApiUrlProvidedError'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class InvalidApiUrlProvidedError extends Error {
|
||||||
|
constructor() {
|
||||||
|
super()
|
||||||
|
this.message = 'The provided API URL is invalid.'
|
||||||
|
this.name = 'InvalidApiUrlProvidedError'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const checkAndSetApiUrl = (url: string | undefined | null): Promise<string> => {
|
||||||
|
if (url === '' || url === null || typeof url === 'undefined') {
|
||||||
|
throw new NoApiUrlProvidedError()
|
||||||
|
}
|
||||||
|
|
||||||
export const checkAndSetApiUrl = (url: string): Promise<string> => {
|
|
||||||
if (url.startsWith('/')) {
|
if (url.startsWith('/')) {
|
||||||
url = window.location.host + url
|
url = window.location.host + url
|
||||||
}
|
}
|
||||||
@ -17,8 +36,14 @@ export const checkAndSetApiUrl = (url: string): Promise<string> => {
|
|||||||
) {
|
) {
|
||||||
url = `${window.location.protocol}//${url}`
|
url = `${window.location.protocol}//${url}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let urlToCheck: URL
|
||||||
|
try {
|
||||||
|
urlToCheck = new URL(url)
|
||||||
|
} catch (e) {
|
||||||
|
throw new InvalidApiUrlProvidedError()
|
||||||
|
}
|
||||||
|
|
||||||
const urlToCheck: URL = new URL(url)
|
|
||||||
const origUrlToCheck = urlToCheck
|
const origUrlToCheck = urlToCheck
|
||||||
|
|
||||||
const oldUrl = window.API_URL
|
const oldUrl = window.API_URL
|
||||||
@ -86,6 +111,6 @@ export const checkAndSetApiUrl = (url: string): Promise<string> => {
|
|||||||
return window.API_URL
|
return window.API_URL
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(ERROR_NO_API_URL)
|
throw new InvalidApiUrlProvidedError()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -916,7 +916,7 @@
|
|||||||
"urlPlaceholder": "eg. https://localhost:3456",
|
"urlPlaceholder": "eg. https://localhost:3456",
|
||||||
"change": "change",
|
"change": "change",
|
||||||
"use": "Using Vikunja installation at {0}",
|
"use": "Using Vikunja installation at {0}",
|
||||||
"error": "Could not find or use Vikunja installation at \"{domain}\". Please try a different url.",
|
"error": "Could not find or use Vikunja installation at \"{domain}\". Please check if the url has the correct format and you can reach it when accessing it directly and try again.",
|
||||||
"success": "Using Vikunja installation at \"{domain}\".",
|
"success": "Using Vikunja installation at \"{domain}\".",
|
||||||
"urlRequired": "A url is required."
|
"urlRequired": "A url is required."
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user