mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +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 })
|
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')
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
Reference in new issue
Block a user