1
- import React from 'react' ;
2
1
import { extend } from 'umi-request' ;
3
2
import { notification } from 'antd' ;
4
- import { history } from 'umi' ;
3
+ import { history , formatMessage } from 'umi' ;
5
4
import { stringify } from 'qs' ;
6
5
7
- const codeMessage = {
8
- 200 : '服务器成功返回请求的数据。' ,
9
- 201 : '新建或修改数据成功。' ,
10
- 202 : '一个请求已经进入后台排队(异步任务)。' ,
11
- 204 : '删除数据成功。' ,
12
- 400 : '发出的请求有错误,服务器没有进行新建或修改数据的操作。' ,
13
- 401 : '用户没有权限(令牌、用户名、密码错误)。' ,
14
- 403 : '用户得到授权,但是访问是被禁止的。' ,
15
- 404 : '发出的请求针对的是不存在的记录,服务器没有进行操作。' ,
16
- 406 : '请求的格式不可得。' ,
17
- 410 : '请求的资源被永久删除,且不会再得到的。' ,
18
- 422 : '当创建一个对象时,发生一个验证错误。' ,
19
- 500 : '服务器发生错误,请检查服务器。' ,
20
- 502 : '网关错误。' ,
21
- 503 : '服务不可用,服务器暂时过载或维护。' ,
22
- 504 : '网关超时。' ,
23
- } ;
24
-
25
6
/**
26
- * 异常处理程序
7
+ * Error handler
27
8
*/
28
9
const errorHandler = error => {
29
10
const { response, data } = error ;
30
- const errortext = (
31
- < >
32
- { codeMessage [ response . status ] || response . statusText } < br />
33
- { data && data . msg && Array . isArray ( data . msg ) && data . msg . length > 0 && data . msg [ 0 ] }
34
- </ >
35
- ) ;
11
+
12
+ if ( ! response ) {
13
+ notification . error ( {
14
+ message : formatMessage ( {
15
+ id : 'error.network' ,
16
+ defaultMessage : 'Network Error' ,
17
+ } ) ,
18
+ } ) ;
19
+ return ;
20
+ }
21
+
36
22
const { status, url } = response ;
37
- let verifyUserFail = false ;
38
23
24
+ // Handle specific error cases
39
25
if ( status === 400 ) {
40
26
const api = url . split ( '/' ) . pop ( ) ;
41
-
42
27
if ( api === 'login' ) {
43
28
notification . error ( {
44
- message : '用户名或密码错误。' ,
29
+ message : formatMessage ( {
30
+ id : 'error.login.invalidCredentials' ,
31
+ defaultMessage : 'Invalid username or password.' ,
32
+ } ) ,
33
+ description : url ,
45
34
} ) ;
46
35
return ;
47
36
}
48
-
49
- if ( api === 'token-verify' ) {
50
- verifyUserFail = true ;
51
- }
52
37
}
53
38
54
- if ( status === 401 || verifyUserFail ) {
39
+ if ( status === 401 ) {
55
40
notification . error ( {
56
- message : '未登录或登录已过期,请重新登录。' ,
41
+ message : formatMessage ( {
42
+ id : 'error.login.expired' ,
43
+ defaultMessage : 'Not logged in or session expired. Please log in again.' ,
44
+ } ) ,
45
+ description : url ,
57
46
} ) ;
58
-
59
47
history . replace ( {
60
48
pathname : '/user/login' ,
61
49
search : stringify ( {
@@ -69,26 +57,41 @@ const errorHandler = error => {
69
57
const api = url . split ( '/' ) . pop ( ) ;
70
58
if ( api === 'register' ) {
71
59
notification . error ( {
72
- message : '邮箱地址或组织名已存在。' ,
60
+ message : formatMessage ( {
61
+ id : 'error.register.duplicate' ,
62
+ defaultMessage : 'Email address or organization name already exists.' ,
63
+ } ) ,
64
+ description : url ,
73
65
} ) ;
74
66
return ;
75
67
}
76
68
}
77
69
70
+ // Generic error handling
71
+ const errorMessage = formatMessage ( {
72
+ id : `error.request.${ status } ` ,
73
+ defaultMessage : `Request error (${ status } )` ,
74
+ } ) ;
75
+
76
+ const detailMessage =
77
+ data ?. detail ||
78
+ data ?. msg ||
79
+ formatMessage ( {
80
+ id : 'error.request.generic' ,
81
+ defaultMessage : 'An error occurred while processing your request.' ,
82
+ } ) ;
83
+
78
84
notification . error ( {
79
- message : `请求错误 ${ status } : ${ url } ` ,
80
- description : errortext ,
85
+ message : errorMessage ,
86
+ description : ` ${ url } \n ${ detailMessage } ` ,
81
87
} ) ;
82
- // environment should not be used
88
+
89
+ // Handle navigation for specific error codes
83
90
if ( status === 403 ) {
84
91
history . push ( '/exception/403' ) ;
85
- return ;
86
- }
87
- if ( status <= 504 && status >= 500 ) {
92
+ } else if ( status >= 500 && status <= 504 ) {
88
93
history . push ( '/exception/500' ) ;
89
- return ;
90
- }
91
- if ( status >= 404 && status < 422 ) {
94
+ } else if ( status >= 404 && status < 422 ) {
92
95
history . push ( '/exception/404' ) ;
93
96
}
94
97
} ;
@@ -101,11 +104,9 @@ const request = extend({
101
104
request . interceptors . request . use ( async ( url , options ) => {
102
105
const token = window . localStorage . getItem ( 'cello-token' ) ;
103
106
if ( url . indexOf ( 'api/v1/login' ) < 0 && url . indexOf ( 'api/v1/register' ) < 0 && token ) {
104
- // 如果有token 就走token逻辑
105
107
const headers = {
106
108
Authorization : `JWT ${ token } ` ,
107
109
} ;
108
-
109
110
return {
110
111
url,
111
112
options : { ...options , headers } ,
0 commit comments