Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dinky
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangpeng
dinky
Commits
d70b621a
Unverified
Commit
d70b621a
authored
Apr 19, 2023
by
gaoyan
Committed by
GitHub
Apr 19, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Global err check (#1894)
* add global error check * add global error check * add global error check
parent
566be090
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
71 additions
and
86 deletions
+71
-86
Result.java
...y-admin/src/main/java/org/dinky/common/result/Result.java
+2
-2
request.ts
dinky-web/src/locales/en-US/request.ts
+6
-0
request.ts
dinky-web/src/locales/zh-CN/request.ts
+6
-0
index.tsx
dinky-web/src/pages/Other/Login/index.tsx
+27
-34
requestErrorConfig.ts
dinky-web/src/requestErrorConfig.ts
+30
-50
No files found.
dinky-admin/src/main/java/org/dinky/common/result/Result.java
View file @
d70b621a
...
...
@@ -44,8 +44,8 @@ public class Result<T> implements Serializable {
private
T
datas
;
private
Integer
code
;
private
String
msg
;
private
String
time
;
private
boolean
success
;
public
Result
(
Integer
code
,
String
msg
)
{
this
.
code
=
code
;
...
...
@@ -89,7 +89,7 @@ public class Result<T> implements Serializable {
}
public
static
<
T
>
Result
<
T
>
of
(
T
datas
,
Integer
code
,
String
msg
)
{
return
new
Result
<>(
datas
,
code
,
msg
,
new
DateTime
().
toString
());
return
new
Result
<>(
datas
,
code
,
msg
,
new
DateTime
().
toString
()
,
code
==
0
);
}
public
static
<
T
>
Result
<
T
>
failed
(
String
msg
)
{
...
...
dinky-web/src/locales/en-US/request.ts
View file @
d70b621a
...
...
@@ -52,4 +52,10 @@ export default {
'app.request.update'
:
'Modify'
,
'app.request.running'
:
'Now is '
,
'app.request.test.alert.msg'
:
'Sending test alert message'
,
'app.response.response-error'
:
'ResponseError'
,
'app.response.sucess'
:
'Success'
,
'app.response.error'
:
'Error'
,
'app.response.exception'
:
'Server exception'
,
'app.response.notlogin'
:
'Not Login'
,
};
dinky-web/src/locales/zh-CN/request.ts
View file @
d70b621a
...
...
@@ -50,4 +50,10 @@ export default {
'app.request.update'
:
'修改'
,
'app.request.running'
:
'正在'
,
'app.request.test.alert.msg'
:
'正在发送测试告警信息'
,
'app.response.response-error'
:
'响应出错'
,
'app.response.sucess'
:
'成功'
,
'app.response.error'
:
'错误'
,
'app.response.exception'
:
'服务端错误'
,
'app.response.notlogin'
:
'未登录'
,
};
dinky-web/src/pages/Other/Login/index.tsx
View file @
d70b621a
...
...
@@ -124,44 +124,37 @@ const Login: React.FC = () => {
try
{
// login
const
result
=
await
login
({
...
values
});
if
(
result
.
code
===
0
)
{
await
SuccessMessageAsync
(
l
(
'login.result'
,
''
,
{
msg
:
result
.
msg
,
time
:
result
.
time
}));
/**
* After successful login, set the tenant list
*/
const
tenantList
:
UserBaseInfo
.
Tenant
[]
=
result
.
datas
.
tenantList
;
if
(
tenantList
===
null
||
tenantList
.
length
===
0
)
{
ErrorMessage
(
l
(
'login.notbindtenant'
));
return
;
}
else
{
setTenant
(
tenantList
);
}
await
SuccessMessageAsync
(
l
(
'login.result'
,
''
,
{
msg
:
result
.
msg
,
time
:
result
.
time
}));
/**
* After successful login, set the tenant list
*/
const
tenantList
:
UserBaseInfo
.
Tenant
[]
=
result
.
datas
.
tenantList
;
if
(
tenantList
===
null
||
tenantList
.
length
===
0
)
{
ErrorMessage
(
l
(
'login.notbindtenant'
));
return
;
}
else
{
setTenant
(
tenantList
);
}
/**
* Determine whether the current tenant list is multiple
* 1. If there are multiple execution pop-up modals, the user selects a specific tenant to enter the system
* 2. If it is a single, only use the unique tenant id to enter the system directly
*/
/**
* Determine whether the current tenant list is multiple
* 1. If there are multiple execution pop-up modals, the user selects a specific tenant to enter the system
* 2. If it is a single, only use the unique tenant id to enter the system directly
*/
if
(
tenantList
&&
tenantList
.
length
>
1
)
{
handleTenantVisible
(
true
);
}
else
{
setTenantIdParams
(
tenantList
[
0
].
id
as
number
);
setTenantStorageAndCookie
(
tenantList
[
0
].
id
as
number
);
const
chooseTenantResult
:
API
.
Result
=
await
chooseTenantSubmit
({
tenantId
:
tenantList
[
0
].
id
as
number
,
});
await
handleChooseTenant
(
chooseTenantResult
);
}
return
;
if
(
tenantList
&&
tenantList
.
length
>
1
)
{
handleTenantVisible
(
true
);
}
else
{
/**
* If it fails to set the user error message
*/
ErrorMessage
(
l
(
'login.result'
,
''
,
{
msg
:
result
.
msg
,
time
:
result
.
time
}));
setTenantIdParams
(
tenantList
[
0
].
id
as
number
);
setTenantStorageAndCookie
(
tenantList
[
0
].
id
as
number
);
const
chooseTenantResult
:
API
.
Result
=
await
chooseTenantSubmit
({
tenantId
:
tenantList
[
0
].
id
as
number
,
});
await
handleChooseTenant
(
chooseTenantResult
);
}
}
catch
(
error
)
{
ErrorMessage
(
l
(
'login.error'
,
''
,
{
msg
:
error
}));
return
;
}
catch
(
error
:
any
)
{
return
;
}
};
...
...
dinky-web/src/requestErrorConfig.ts
View file @
d70b621a
...
...
@@ -15,25 +15,29 @@
* limitations under the License.
*/
import
type
{
RequestOptions
}
from
'@@/plugin-request/request'
;
import
type
{
RequestConfig
}
from
'@umijs/max'
;
import
{
message
,
notification
}
from
'antd'
;
import
type
{
RequestOptions
}
from
'@@/plugin-request/request'
;
import
type
{
RequestConfig
}
from
'@umijs/max'
;
import
{
l
}
from
"@/utils/intl"
;
import
{
history
}
from
"@@/core/history"
;
import
{
API_CONSTANTS
}
from
"@/services/constants"
;
import
{
ErrorNotification
}
from
"@/utils/messages"
;
const
loginPath
=
API_CONSTANTS
.
LOGIN_PATH
;
// 错误处理方案: 错误类型
enum
ErrorShowType
{
SILENT
=
0
,
WARN_MESSAGE
=
1
,
ERROR_MESSAGE
=
2
,
NOTIFICATION
=
3
,
REDIRECT
=
9
,
enum
ErrorCode
{
'app.response.sucess'
=
0
,
'app.response.error'
=
1
,
'app.response.exception'
=
5
,
'app.response.notlogin'
=
401
,
}
// 与后端约定的响应数据格式
interface
ResponseStructure
{
success
:
boolean
;
data
:
any
;
code
?:
number
;
msg
?:
string
;
showType
?:
ErrorShowType
;
datas
?:
boolean
;
code
:
number
;
msg
:
string
;
}
/**
...
...
@@ -45,12 +49,12 @@ export const errorConfig: RequestConfig = {
// 错误处理: umi@3 的错误处理方案。
errorConfig
:
{
// 错误抛出
errorThrower
:
(
res
)
=>
{
const
{
success
,
data
,
code
,
msg
,
showType
}
=
res
as
unknown
as
ResponseStructure
;
errorThrower
:
(
res
:
ResponseStructure
)
=>
{
const
{
success
,
datas
,
msg
,
code
}
=
res
as
ResponseStructure
;
if
(
!
success
)
{
const
error
:
any
=
new
Error
(
msg
);
error
.
name
=
'BizError'
;
error
.
info
=
{
code
,
msg
,
showType
,
data
};
error
.
info
=
{
msg
,
code
,
datas
};
throw
error
;
// 抛出自制的错误
}
},
...
...
@@ -59,44 +63,20 @@ export const errorConfig: RequestConfig = {
if
(
opts
?.
skipErrorHandler
)
throw
error
;
// 我们的 errorThrower 抛出的错误。
if
(
error
.
name
===
'BizError'
)
{
const
errorInfo
:
ResponseStructure
|
undefined
=
error
.
info
;
const
errorInfo
:
ResponseStructure
=
error
.
info
;
if
(
errorInfo
)
{
const
{
msg
,
code
}
=
errorInfo
;
switch
(
errorInfo
.
showType
)
{
case
ErrorShowType
.
SILENT
:
// do nothing
break
;
case
ErrorShowType
.
WARN_MESSAGE
:
message
.
warning
(
msg
);
break
;
case
ErrorShowType
.
ERROR_MESSAGE
:
message
.
error
(
msg
);
break
;
case
ErrorShowType
.
NOTIFICATION
:
notification
.
open
({
description
:
msg
,
message
:
code
,
});
break
;
case
ErrorShowType
.
REDIRECT
:
// TODO: redirect
break
;
default
:
message
.
error
(
msg
);
}
const
{
msg
,
code
}
=
errorInfo
;
ErrorNotification
(
msg
,
l
(
ErrorCode
[
code
],
"Error"
))
}
}
else
if
(
error
.
response
)
{
// Axios 的错误
// 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围
message
.
error
(
`Response status:
${
error
.
response
.
status
}
`
);
ErrorNotification
(
error
.
response
.
data
.
msg
,
l
(
ErrorCode
[
error
.
response
.
data
.
code
],
"ResponseError"
))
}
else
if
(
error
.
request
)
{
// 请求已经成功发起,但没有收到响应
// \`error.request\` 在浏览器中是 XMLHttpRequest 的实例,
// 而在node.js中是 http.ClientRequest 的实例
message
.
error
(
'None response! Please retry.'
);
ErrorNotification
(
error
.
toString
(),
l
(
'app.response.noresponse'
))
}
else
{
// 发送请求时出了点问题
message
.
error
(
'Request error, please retry.'
);
ErrorNotification
(
error
.
toString
(),
l
(
'app.request.failed'
))
}
},
},
...
...
@@ -114,10 +94,10 @@ export const errorConfig: RequestConfig = {
responseInterceptors
:
[
(
response
)
=>
{
// 拦截响应数据,进行个性化处理
const
{
data
}
=
response
as
unknown
as
ResponseStructure
;
if
(
data
?.
success
===
false
)
{
message
.
error
(
'请求失败!'
);
// 不再需要异步处理读取返回体内容,可直接在data中读出,部分字段可在 config 中找到
const
{
data
=
{}
as
any
,
config
}
=
response
;
if
(
data
?.
code
===
401
)
{
history
.
push
(
loginPath
);
}
return
response
;
},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment