fix: update frontend error handling

This commit is contained in:
wancat committed 2023-11-01 23:30:28 +08:00
1 parent af672791a5
commit bcb9f1f803
2 files changed
+11 -2

No files matched your search

+4
View File
@@ -17,6 +17,7 @@ export const login = async (username, password) => {
await api.post('/auth/login', { username, password }) await api.post('/auth/login', { username, password })
return true return true
} catch (err) { } catch (err) {
if (!err.response) throw err
const { status } = err.response const { status } = err.response
switch (status) { switch (status) {
case 401: case 401:
@@ -43,6 +44,7 @@ export const register = async (username, email, password) => {
}) })
return true return true
} catch (err) { } catch (err) {
if (!err.response) throw err
const { status } = err.response const { status } = err.response
if (status === 429) { if (status === 429) {
throw new TooManyRequestsError(err.response.data.message) throw new TooManyRequestsError(err.response.data.message)
@@ -78,6 +80,7 @@ export const checkUsername = async (username) => {
}) })
return true return true
} catch (err) { } catch (err) {
if (!err.response) throw err
const { status } = err.response const { status } = err.response
if (status === 429) { if (status === 429) {
throw new TooManyRequestsError('Too many requests') throw new TooManyRequestsError('Too many requests')
@@ -93,6 +96,7 @@ export const chooseUsername = async (username) => {
try { try {
await api.post('/auth/register-from-session', { username }) await api.post('/auth/register-from-session', { username })
} catch (err) { } catch (err) {
if (!err.response) throw err
switch (err.response?.status) { switch (err.response?.status) {
case 400: case 400:
throw new ValidationError('Username is already taken') throw new ValidationError('Username is already taken')
+7 -2
View File
@@ -29,6 +29,7 @@ function handleRegister({ username, email, password, success, error }) {
return return
} }
console.error(err) console.error(err)
error('Something went wrong.')
}) })
} }
@@ -53,8 +54,12 @@ async function check({ username, email, password, error }) {
try { try {
await checkUsername(username) await checkUsername(username)
} catch (err) { } catch (err) {
if (err.response?.status === 400) { if (err instanceof ValidationError) {
error('Invalid username.') error(err.message)
return
}
if (err instanceof TooManyRequestsError) {
error('Too many requests. Try again in 2 minutes.')
return return
} }
console.error(err) console.error(err)