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 { ...@@ -44,8 +44,8 @@ public class Result<T> implements Serializable {
private T datas; private T datas;
private Integer code; private Integer code;
private String msg; private String msg;
private String time; private String time;
private boolean success;
public Result(Integer code, String msg) { public Result(Integer code, String msg) {
this.code = code; this.code = code;
...@@ -89,7 +89,7 @@ public class Result<T> implements Serializable { ...@@ -89,7 +89,7 @@ public class Result<T> implements Serializable {
} }
public static <T> Result<T> of(T datas, Integer code, String msg) { 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) { public static <T> Result<T> failed(String msg) {
......
...@@ -52,4 +52,10 @@ export default { ...@@ -52,4 +52,10 @@ export default {
'app.request.update': 'Modify', 'app.request.update': 'Modify',
'app.request.running': 'Now is ', 'app.request.running': 'Now is ',
'app.request.test.alert.msg': 'Sending test alert message', '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 { ...@@ -50,4 +50,10 @@ export default {
'app.request.update':'修改', 'app.request.update':'修改',
'app.request.running':'正在', 'app.request.running':'正在',
'app.request.test.alert.msg':'正在发送测试告警信息', '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 = () => { ...@@ -124,44 +124,37 @@ const Login: React.FC = () => {
try { try {
// login // login
const result = await login({ ...values }); const result = await login({ ...values });
if (result.code === 0) { await SuccessMessageAsync(l('login.result', '', { msg: result.msg, time: result.time }));
await SuccessMessageAsync(l('login.result', '', { msg: result.msg, time: result.time })); /**
/** * After successful login, set the tenant list
* After successful login, set the tenant list */
*/ const tenantList: UserBaseInfo.Tenant[] = result.datas.tenantList;
const tenantList: UserBaseInfo.Tenant[] = result.datas.tenantList; if (tenantList === null || tenantList.length === 0) {
if (tenantList === null || tenantList.length === 0) { ErrorMessage(l('login.notbindtenant'));
ErrorMessage(l('login.notbindtenant')); return;
return; } else {
} else { setTenant(tenantList);
setTenant(tenantList); }
}
/** /**
* Determine whether the current tenant list is multiple * 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 * 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 * 2. If it is a single, only use the unique tenant id to enter the system directly
*/ */
if (tenantList && tenantList.length > 1) { if (tenantList && tenantList.length > 1) {
handleTenantVisible(true); 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;
} else { } else {
/** setTenantIdParams(tenantList[0].id as number);
* If it fails to set the user error message setTenantStorageAndCookie(tenantList[0].id as number);
*/ const chooseTenantResult: API.Result = await chooseTenantSubmit({
ErrorMessage(l('login.result', '', { msg: result.msg, time: result.time })); tenantId: tenantList[0].id as number,
});
await handleChooseTenant(chooseTenantResult);
} }
} catch (error) { return;
ErrorMessage(l('login.error', '', { msg: error })); } catch (error:any) {
return;
} }
}; };
......
...@@ -15,25 +15,29 @@ ...@@ -15,25 +15,29 @@
* limitations under the License. * limitations under the License.
*/ */
import type { RequestOptions } from '@@/plugin-request/request'; import type {RequestOptions} from '@@/plugin-request/request';
import type { RequestConfig } from '@umijs/max'; import type {RequestConfig} from '@umijs/max';
import { message, notification } from 'antd'; 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 { enum ErrorCode {
SILENT = 0, 'app.response.sucess' = 0,
WARN_MESSAGE = 1, 'app.response.error' = 1,
ERROR_MESSAGE = 2, 'app.response.exception' = 5,
NOTIFICATION = 3, 'app.response.notlogin' = 401,
REDIRECT = 9,
} }
// 与后端约定的响应数据格式 // 与后端约定的响应数据格式
interface ResponseStructure { interface ResponseStructure {
success: boolean; success: boolean;
data: any; datas?: boolean;
code?: number; code: number;
msg?: string; msg: string;
showType?: ErrorShowType;
} }
/** /**
...@@ -45,12 +49,12 @@ export const errorConfig: RequestConfig = { ...@@ -45,12 +49,12 @@ export const errorConfig: RequestConfig = {
// 错误处理: umi@3 的错误处理方案。 // 错误处理: umi@3 的错误处理方案。
errorConfig: { errorConfig: {
// 错误抛出 // 错误抛出
errorThrower: (res) => { errorThrower: (res: ResponseStructure) => {
const { success, data, code, msg, showType } = res as unknown as ResponseStructure; const {success, datas, msg, code} = res as ResponseStructure;
if (!success) { if (!success) {
const error: any = new Error(msg); const error: any = new Error(msg);
error.name = 'BizError'; error.name = 'BizError';
error.info = { code, msg, showType, data }; error.info = {msg, code, datas};
throw error; // 抛出自制的错误 throw error; // 抛出自制的错误
} }
}, },
...@@ -59,44 +63,20 @@ export const errorConfig: RequestConfig = { ...@@ -59,44 +63,20 @@ export const errorConfig: RequestConfig = {
if (opts?.skipErrorHandler) throw error; if (opts?.skipErrorHandler) throw error;
// 我们的 errorThrower 抛出的错误。 // 我们的 errorThrower 抛出的错误。
if (error.name === 'BizError') { if (error.name === 'BizError') {
const errorInfo: ResponseStructure | undefined = error.info; const errorInfo: ResponseStructure = error.info;
if (errorInfo) { if (errorInfo) {
const { msg, code } = errorInfo; const {msg, code} = errorInfo;
switch (errorInfo.showType) { ErrorNotification(msg,l(ErrorCode[code],"Error"))
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);
}
} }
} else if (error.response) { } else if (error.response) {
// Axios 的错误
// 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围 // 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围
message.error(`Response status:${error.response.status}`); ErrorNotification(error.response.data.msg,l(ErrorCode[error.response.data.code],"ResponseError"))
} else if (error.request) { } else if (error.request) {
// 请求已经成功发起,但没有收到响应 // 请求已经成功发起,但没有收到响应
// \`error.request\` 在浏览器中是 XMLHttpRequest 的实例, ErrorNotification(error.toString(),l('app.response.noresponse'))
// 而在node.js中是 http.ClientRequest 的实例
message.error('None response! Please retry.');
} else { } else {
// 发送请求时出了点问题 // 发送请求时出了点问题
message.error('Request error, please retry.'); ErrorNotification(error.toString(),l('app.request.failed'))
} }
}, },
}, },
...@@ -114,10 +94,10 @@ export const errorConfig: RequestConfig = { ...@@ -114,10 +94,10 @@ export const errorConfig: RequestConfig = {
responseInterceptors: [ responseInterceptors: [
(response) => { (response) => {
// 拦截响应数据,进行个性化处理 // 拦截响应数据,进行个性化处理
const { data } = response as unknown as ResponseStructure; // 不再需要异步处理读取返回体内容,可直接在data中读出,部分字段可在 config 中找到
const {data = {} as any, config} = response;
if (data?.success === false) { if (data?.code === 401) {
message.error('请求失败!'); history.push(loginPath);
} }
return response; 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