Unverified Commit d70b621a authored by gaoyan's avatar gaoyan Committed by GitHub

Add Global err check (#1894)

* add global error check * add global error check * add global error check
parent 566be090
......@@ -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) {
......
......@@ -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',
};
......@@ -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':'未登录',
};
......@@ -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;
}
};
......
......@@ -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;
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment