mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 11:30:13 +08:00
feat: add error handling for choose username
This commit is contained in:
1 parent
f65b72c5b4
commit
bcfdf15559
4 files changed
+81
-48
No files matched your search
+49
-11
@@ -17,16 +17,20 @@ export const login = async (username, password) => {
|
||||
await api.post('/auth/login', { username, password })
|
||||
return true
|
||||
} catch (err) {
|
||||
if (err.response?.status === 401) {
|
||||
throw new UnauthorizedError(err.response.data.message)
|
||||
const { status } = err.response
|
||||
switch (status) {
|
||||
case 401:
|
||||
throw new UnauthorizedError(err.response.data.message)
|
||||
|
||||
case 429:
|
||||
throw new TooManyRequestsError(err.response.data.message)
|
||||
|
||||
case 400:
|
||||
throw new ValidationError(err.response.data.message)
|
||||
|
||||
default:
|
||||
throw err
|
||||
}
|
||||
if (err.response?.status === 429) {
|
||||
throw new TooManyRequestsError(err.response.data.message)
|
||||
}
|
||||
if (err.response?.status === 400) {
|
||||
throw new ValidationError(err.response.data.message)
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +43,7 @@ export const register = async (username, email, password) => {
|
||||
})
|
||||
return true
|
||||
} catch (err) {
|
||||
const status = err.response?.status
|
||||
const { status } = err.response
|
||||
if (status === 429) {
|
||||
throw new TooManyRequestsError(err.response.data.message)
|
||||
}
|
||||
@@ -48,7 +52,7 @@ export const register = async (username, email, password) => {
|
||||
}
|
||||
if (status === 400) {
|
||||
const { errors } = err.response.data
|
||||
if (errors?.length > 0) {
|
||||
if (Array.isArray(errors) && errors.length > 0) {
|
||||
const { path, msg } = errors[0]
|
||||
switch (path) {
|
||||
case 'username':
|
||||
@@ -61,11 +65,45 @@ export const register = async (username, email, password) => {
|
||||
throw new ValidationError(msg)
|
||||
}
|
||||
}
|
||||
throw new ValidationError(err.response.data.message)
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
export const checkUsername = async (username) => {
|
||||
try {
|
||||
await api.get('/auth/check-username', {
|
||||
params: { username },
|
||||
})
|
||||
return true
|
||||
} catch (err) {
|
||||
const { status } = err.response
|
||||
if (status === 429) {
|
||||
throw new TooManyRequestsError('Too many requests')
|
||||
}
|
||||
if (status === 409) {
|
||||
throw new ValidationError('Username is already taken')
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
export const chooseUsername = async (username) => {
|
||||
try {
|
||||
await api.post('/auth/register-from-session', { username })
|
||||
} catch (err) {
|
||||
switch (err.response?.status) {
|
||||
case 400:
|
||||
throw new ValidationError('Username is already taken')
|
||||
case 401:
|
||||
throw new UnauthorizedError('You are not a new user')
|
||||
default:
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const logout = () => {
|
||||
return api.post('/auth/logout')
|
||||
}
|
||||
Reference in new issue
Block a user