mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
fix: update frontend error handling
This commit is contained in:
1 parent
3af560bf7a
commit
14bde27127
2 files changed
+11
-2
No files matched your search
@@ -17,6 +17,7 @@ export const login = async (username, password) => {
|
||||
await api.post('/auth/login', { username, password })
|
||||
return true
|
||||
} catch (err) {
|
||||
if (!err.response) throw err
|
||||
const { status } = err.response
|
||||
switch (status) {
|
||||
case 401:
|
||||
@@ -43,6 +44,7 @@ export const register = async (username, email, password) => {
|
||||
})
|
||||
return true
|
||||
} catch (err) {
|
||||
if (!err.response) throw err
|
||||
const { status } = err.response
|
||||
if (status === 429) {
|
||||
throw new TooManyRequestsError(err.response.data.message)
|
||||
@@ -78,6 +80,7 @@ export const checkUsername = async (username) => {
|
||||
})
|
||||
return true
|
||||
} catch (err) {
|
||||
if (!err.response) throw err
|
||||
const { status } = err.response
|
||||
if (status === 429) {
|
||||
throw new TooManyRequestsError('Too many requests')
|
||||
@@ -93,6 +96,7 @@ export const chooseUsername = async (username) => {
|
||||
try {
|
||||
await api.post('/auth/register-from-session', { username })
|
||||
} catch (err) {
|
||||
if (!err.response) throw err
|
||||
switch (err.response?.status) {
|
||||
case 400:
|
||||
throw new ValidationError('Username is already taken')
|
||||
|
||||
@@ -29,6 +29,7 @@ function handleRegister({ username, email, password, success, error }) {
|
||||
return
|
||||
}
|
||||
console.error(err)
|
||||
error('Something went wrong.')
|
||||
})
|
||||
}
|
||||
|
||||
@@ -53,8 +54,12 @@ async function check({ username, email, password, error }) {
|
||||
try {
|
||||
await checkUsername(username)
|
||||
} catch (err) {
|
||||
if (err.response?.status === 400) {
|
||||
error('Invalid username.')
|
||||
if (err instanceof ValidationError) {
|
||||
error(err.message)
|
||||
return
|
||||
}
|
||||
if (err instanceof TooManyRequestsError) {
|
||||
error('Too many requests. Try again in 2 minutes.')
|
||||
return
|
||||
}
|
||||
console.error(err)
|
||||
|
||||
Reference in new issue
Block a user