1- import React from 'react' ;
21import { extend } from 'umi-request' ;
32import { notification } from 'antd' ;
4- import { history } from 'umi' ;
3+ import { history , formatMessage } from 'umi' ;
54import { stringify } from 'qs' ;
65
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-
256/**
26- * 异常处理程序
7+ * Error handler
278 */
289const errorHandler = error => {
2910 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+
3622 const { status, url } = response ;
37- let verifyUserFail = false ;
3823
24+ // Handle specific error cases
3925 if ( status === 400 ) {
4026 const api = url . split ( '/' ) . pop ( ) ;
41-
4227 if ( api === 'login' ) {
4328 notification . error ( {
44- message : '用户名或密码错误。' ,
29+ message : formatMessage ( {
30+ id : 'error.login.invalidCredentials' ,
31+ defaultMessage : 'Invalid username or password.' ,
32+ } ) ,
33+ description : url ,
4534 } ) ;
4635 return ;
4736 }
48-
49- if ( api === 'token-verify' ) {
50- verifyUserFail = true ;
51- }
5237 }
5338
54- if ( status === 401 || verifyUserFail ) {
39+ if ( status === 401 ) {
5540 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 ,
5746 } ) ;
58-
5947 history . replace ( {
6048 pathname : '/user/login' ,
6149 search : stringify ( {
@@ -69,26 +57,41 @@ const errorHandler = error => {
6957 const api = url . split ( '/' ) . pop ( ) ;
7058 if ( api === 'register' ) {
7159 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 ,
7365 } ) ;
7466 return ;
7567 }
7668 }
7769
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+
7884 notification . error ( {
79- message : `请求错误 ${ status } : ${ url } ` ,
80- description : errortext ,
85+ message : errorMessage ,
86+ description : ` ${ url } \n ${ detailMessage } ` ,
8187 } ) ;
82- // environment should not be used
88+
89+ // Handle navigation for specific error codes
8390 if ( status === 403 ) {
8491 history . push ( '/exception/403' ) ;
85- return ;
86- }
87- if ( status <= 504 && status >= 500 ) {
92+ } else if ( status >= 500 && status <= 504 ) {
8893 history . push ( '/exception/500' ) ;
89- return ;
90- }
91- if ( status >= 404 && status < 422 ) {
94+ } else if ( status >= 404 && status < 422 ) {
9295 history . push ( '/exception/404' ) ;
9396 }
9497} ;
@@ -101,11 +104,9 @@ const request = extend({
101104request . interceptors . request . use ( async ( url , options ) => {
102105 const token = window . localStorage . getItem ( 'cello-token' ) ;
103106 if ( url . indexOf ( 'api/v1/login' ) < 0 && url . indexOf ( 'api/v1/register' ) < 0 && token ) {
104- // 如果有token 就走token逻辑
105107 const headers = {
106108 Authorization : `JWT ${ token } ` ,
107109 } ;
108-
109110 return {
110111 url,
111112 options : { ...options , headers } ,
0 commit comments