Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
fix: error type
Browse files Browse the repository at this point in the history
  • Loading branch information
BoYanZh committed Mar 19, 2024
1 parent d3a298e commit 2484edf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/client/error.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface ErrorResponse extends Error {
status: number
error: { msg: string }
}
8 changes: 7 additions & 1 deletion src/pages/Login/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useRequest } from 'ahooks'
import type { ErrorResponse } from 'client/error'
import PageContainer from 'components/PageContainer'
import { useAuth } from 'hooks/useAuth'
import { useState } from 'react'
import { Col, Row } from 'react-bootstrap'
import { Alert, Col, Row } from 'react-bootstrap'
import Button from 'react-bootstrap/Button'
import Form from 'react-bootstrap/Form'
import { Navigate, useSearchParams } from 'react-router-dom'
Expand All @@ -11,6 +12,7 @@ import Backend from 'utils/service'

const LoginForm: React.FC = () => {
const { user } = useAuth()
const [error, setError] = useState('')
const [searchParams] = useSearchParams()
const [username, setUsername] = useState('demo')
const [password, setPassword] = useState('123456')
Expand All @@ -33,6 +35,9 @@ const LoginForm: React.FC = () => {
if (res.data.redirect_url) {
window.location.href = res.data.redirect_url
}
},
onError: err => {
setError((err as ErrorResponse).error.msg)
}
}
)
Expand All @@ -49,6 +54,7 @@ const LoginForm: React.FC = () => {
<h1>Login</h1>
</div>
<Col xs={12} md={8} lg={6} className='mx-auto mt-3'>
{error ? <Alert variant='danger'>{error}</Alert> : null}
<Form onSubmit={handleSubmit} validated>
<Form.Group as={Row} className='mb-3' controlId='formBasicEmail'>
<Form.Label className='text-sm-end' column sm={2}>
Expand Down
10 changes: 4 additions & 6 deletions src/pages/Register/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import './style.module.css'
import { useRequest } from 'ahooks'
import type { ErrorResponse } from 'client/error'
import PageContainer from 'components/PageContainer'
import type React from 'react'
import { useState } from 'react'
Expand Down Expand Up @@ -107,16 +108,13 @@ const RegistrationForm = () => {
navigate('/register/confirm')
},
onError: err => {
// eslint-disable-next-line
console.log(err)
if (err.status === 409) {
setError('Registration failed. Please try again.')
if ((err as ErrorResponse).status === 409) {
setError(
'This email or username is already registered. Please try again.'
)
} else if (err.status === 400) {
} else if ((err as ErrorResponse).status === 400) {
setError('Invalid input. Please try again.')
} else {
setError('Registration failed. Please try again.')
}
}
}
Expand Down

0 comments on commit 2484edf

Please sign in to comment.