feat: error handler

This commit is contained in:
KUN1007 2023-10-03 20:55:53 +08:00
parent 232dc1e0bf
commit c8bc852267
8 changed files with 86 additions and 13 deletions

View file

@ -1,11 +1,13 @@
<script setup lang="ts">
import { watch, ref } from 'vue'
import { Icon } from '@iconify/vue'
// store
import { useKUNGalgameSettingsStore } from '@/store/modules/settings'
import { storeToRefs } from 'pinia'
// 使 store
const settingsStore = useKUNGalgameSettingsStore()
const { showKUNGalgameLanguage } = storeToRefs(useKUNGalgameSettingsStore())
const props = defineProps<{
messageCN: string
@ -13,10 +15,10 @@ const props = defineProps<{
type: `warn` | `success` | `error` | `info`
}>()
const message =
settingsStore.showKUNGalgameLanguage === 'en'
? props.messageEN
: props.messageCN
const message = ref('')
//
message.value =
showKUNGalgameLanguage.value === 'en' ? props.messageEN : props.messageCN
const messageClass = (type: string): string => {
if (type === 'warn') {
@ -31,6 +33,15 @@ const messageClass = (type: string): string => {
return ''
}
}
// ,
watch(
() => useKUNGalgameSettingsStore().showKUNGalgameLanguage,
() => {
message.value =
showKUNGalgameLanguage.value === 'en' ? props.messageEN : props.messageCN
}
)
</script>
<template>

17
src/error/errorHandler.ts Normal file
View file

@ -0,0 +1,17 @@
// 全局消息组件(顶部)
import message from '@/components/alert/Message'
import { getErrorMessageEN, getErrorMessageCN } from './errorI18n'
const showMessage = (errorCode: number) => {
const messageType = 'error'
const messageTextEN = getErrorMessageEN(errorCode)
const messageTextCN = getErrorMessageCN(errorCode)
message(messageTextEN, messageTextCN, messageType)
}
/**
* KUNGalgame
*/
export const kungalgameErrorHandler = (errorNumber: number) => {
showMessage(errorNumber)
}

23
src/error/errorI18n.ts Normal file
View file

@ -0,0 +1,23 @@
const errorMessagesEN: Record<number, string> = {
10101: 'User not found',
10102: 'User password error',
10103: 'Email verification code error',
10104: 'Email is already registered, please change it',
10105: 'Username is already registered, please change it',
}
const errorMessagesCN: Record<number, string> = {
10101: '用户未找到',
10102: '用户密码错误',
10103: '邮箱验证码错误',
10104: '邮箱已被注册,请更改',
10105: '用户名已被注册,请修改',
}
export const getErrorMessageEN = (errorCode: number) => {
return errorMessagesEN[errorCode] || 'Unknown server error'
}
export const getErrorMessageCN = (errorCode: number) => {
return errorMessagesCN[errorCode] || '未知的服务器错误'
}

View file

@ -5,8 +5,20 @@ import { generateTokenByRefreshTokenApi } from '@/api'
import { useKUNGalgameUserStore } from '@/store/modules/kungalgamer'
// 导入路由
import router from '@/router'
// 导入已知错误处理函数
import { kungalgameErrorHandler } from './errorHandler'
interface ErrorResponseData {
code: number
message: string
}
/**
*
*
*/
export async function onRequestError(response: Response) {
// 根据状态码识别错误
if (response.status === 401) {
// 尝试根据 refresh token 获取新的 token
const accessTokenResponse = await generateTokenByRefreshTokenApi()
@ -26,6 +38,7 @@ export async function onRequestError(response: Response) {
useKUNGalgameUserStore().removeToken()
router.push('/login')
}
return
}
if (response.status === 404) {
@ -34,9 +47,11 @@ export async function onRequestError(response: Response) {
'资源未找到,请求地址出错',
'error'
)
return
}
if (response.status === 500) {
message('Internal Server Error', '服务器错误', 'error')
}
// 获取错误响应体数据
const data: ErrorResponseData = await response.json()
// 处理已知错误
kungalgameErrorHandler(data.code)
}

View file

@ -220,7 +220,6 @@ export default {
leave: 'Confirm leaving the page? Your changes will not be saved.',
},
login: {
success: 'Login Successfully! Welcome to KUN Visual Novel ~ ',
invalidUsername:
'Invalid username. Username should be 1 to 17 characters long and can include: Chinese characters, English letters, numbers, underscore, and tilde (~)',
invalidPassword:

View file

@ -218,7 +218,6 @@ export default {
leave: '确认离开界面吗?您的更改将不会保存',
},
login: {
login: '登陆成功!欢迎来到 鲲 Galgame ~ ',
invalidUsername:
'非法的用户名,用户名为 1 到 17 位,可以包含:中文、英文、数字、下划线、波浪线',
invalidPassword:

View file

@ -28,8 +28,12 @@ const kunFetchRequest = async <T>(
const response = await fetch(fullUrl, { ...options, headers })
// 处理错误token 过期
await onRequestError(response)
// 后端的一切正常响应都被设为 200 了
if (response.status != 200) {
// 处理错误token 过期
await onRequestError(response)
throw new Error('KUNGalgame Fetch Error occurred, but no problem')
}
const data: T = await response.json()
return data

View file

@ -89,7 +89,12 @@ const handleLogin = () => {
//
if (res.code === 200) {
router.push('/')
info.info(tm('AlertInfo.login.success'))
message(
'Login Successfully! Welcome to KUN Visual Novel ~ ',
'登陆成功!欢迎来到 鲲 Galgame ~ ',
'success',
5000
)
}
})
}