Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize api error code handler #804

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion frontend/src/components/navbar/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
import useFetchApi from '../../packs/useFetchApi.js'
import { mapState } from 'pinia'
import { useCookies } from "vue3-cookies"
import { ElMessage } from 'element-plus'

export default {
props: {
Expand Down Expand Up @@ -293,10 +294,12 @@
location.href = `/${locale}/settings/locale`
},
async fetchUser() {
const {data, _} = await useFetchApi(`/user/${this.uuid}?type=uuid`).json()
const {data, error} = await useFetchApi(`/user/${this.uuid}?type=uuid`).json()
if (data.value) {
this.userAvatar = data.value.data.avatar
this.userStore.initialize(data.value.data)
} else {
ElMessage.warning(error.value.msg)
}
},
clearCookies() {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/shared/BranchDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@

const { data, error } = await useFetchApi(url).json()

if (!error.value) {
if (data.value) {
branches.value = data.value.data
} else {
ElMessage({ message: error.value.msg, type: 'warning' })
ElMessage.warning(error.value.msg)
}
}

Expand Down
11 changes: 7 additions & 4 deletions frontend/src/components/shared/FileList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,16 @@
const url = `/${prefixPath}/${props.namespacePath}/tree?path=${props.currentPath}&ref=${props.currentBranch}`

try {
const { data, error } = await useFetchApi(url).json()
const { response, data, error } = await useFetchApi(url).json()

if (error.value) {
if (data.value) {
files.value = data.value.data
} else if (response.value.status === 403) {
location.href = '/errors/unauthorized'
} else if (response.value.status === 404) {
location.href = '/errors/not-found'
} else {
const json = data.value
files.value = json.data
ElMessage.warning(error.value.msg)
}
} catch (error) {
console.log(error)
Expand Down
9 changes: 3 additions & 6 deletions frontend/src/components/shared/RepoCards.vue
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,11 @@

async function loadRepos(url) {
const { error, data } = await useFetchApi(url).json()
if (!data.value) {
ElMessage({
message: error.value.msg || t('all.fetchError'),
type: 'warning'
})
} else {
if (data.value) {
reposData.value = data.value.data
totalRepos.value = data.value.total
} else {
ElMessage.warning(error.value.msg || t('all.fetchError'))
}
}

Expand Down
15 changes: 10 additions & 5 deletions frontend/src/components/shared/RepoDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import useRepoDetailStore from '../../stores/RepoDetailStore'
import { buildTags } from '../../packs/buildTags'
import { ElMessage } from 'element-plus'
import { usePermissionCheck } from '../../packs/usePermissionCheck'
import useFetchApi from '../../packs/useFetchApi'

const props = defineProps({
Expand Down Expand Up @@ -89,8 +88,11 @@
try {
const { response, data, error } = await useFetchApi(url).json()

const hasPermission = usePermissionCheck(response.value)
if (!hasPermission) return
// redirect unauthorized page
if (response.value.status === 403) {
window.location.href = '/errors/unauthorized'
return
}

const json = data.value
if (json) {
Expand All @@ -100,8 +102,11 @@
}
repoDetailStore.initialize(json.data)
ownerUrl.value = getOwnerUrl(json.data)
} else {
ElMessage({ message: error.value.msg, type: 'warning' })
} else if (response.value.status === 404 ) {
window.location.href = '/errors/not-found'
}
else {
ElMessage.warning(error.value.msg)
}
} catch (error) {
console.error(error)
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/shared/RepoSummary.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import TestEndpoint from '../endpoints/playground/TestEndpoint.vue'
import useFetchApi from '../../packs/useFetchApi'
import resolveContent from '../../packs/resolveContent'
import { ElMessage } from 'element-plus'

const props = defineProps({
namespacePath: String,
Expand Down Expand Up @@ -117,7 +118,7 @@
type: 'warning'
})
} else {
datasetInfo.value = data.value.data.dataset_info
datasetInfo.value = data.value.data.dataset_info
}
}

Expand Down
10 changes: 4 additions & 6 deletions frontend/src/components/user_settings/Profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,8 @@
const hasLastLoginTime = computed(() => isCurrentUser.value ? !!userStore.lastLoginTime : !!theLastLoginTime.value)
const hasOrg = computed(() => isCurrentUser.value ? !!userStore.orgs : !!userOrgs.value)
const fetchUserInfo = async () => {
const { data, error } = await useFetchApi(
`/user/${props.name}`
).json()
if (error.value) {
ElMessage({ message: error.value, type: 'warning' })
} else {
const { data, error } = await useFetchApi(`/user/${props.name}`).json()
if (data.value) {
const body = data.value
avatar.value = body.data.avatar
username.value = body.data.username
Expand All @@ -119,6 +115,8 @@
email.value = body.data.email
theLastLoginTime.value = body.data.last_login_at
userOrgs.value = body.data.orgs
} else {
ElMessage.warning(error.value.msg)
}
}
</script>
7 changes: 1 addition & 6 deletions frontend/src/packs/useFetchApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,9 @@ const useFetchApi = createFetch({
}
return { options }
},
onFetchError({ data, error, response }) {
onFetchError({ response }) {
if (response.status === 401) {
popupReloginDialog()
} else if (response.status === 404) {
window.location.href = "/errors/not-found"
} else {
console.log('Fetch Error:', data || error)
return { error: data || error }
}
}
}
Expand Down
7 changes: 0 additions & 7 deletions frontend/src/packs/usePermissionCheck.js

This file was deleted.

Loading