exception的适配

This commit is contained in:
Xu Chang 2025-01-21 20:10:40 +08:00
parent 3779a8c2de
commit aad5f0c6f4
2 changed files with 62 additions and 36 deletions

View File

@ -1,7 +1,6 @@
import { import {
OakException, OakException,
OakUnloggedInException, OakUnloggedInException,
OakUserUnpermittedException,
OakAttrNotNullException, OakAttrNotNullException,
OakInputIllegalException, OakInputIllegalException,
} from 'oak-domain/lib/types/Exception'; } from 'oak-domain/lib/types/Exception';
@ -10,47 +9,80 @@ import {
} from '@project/types/Exception'; } from '@project/types/Exception';
import { FeatureDict } from '@project/features'; import { FeatureDict } from '@project/features';
import { AFD } from '@project/types/RuntimeCxt'; import { AFD } from '@project/types/RuntimeCxt';
import { OakApplicationLoadingException, OakHasToVerifyPassword, OakPasswordUnset, OakUserInfoLoadingException } from 'oak-general-business';
// import { OakUserInfoLoadingException, OakApplicationLoadingException } from 'oak-general-business'; // import { OakUserInfoLoadingException, OakApplicationLoadingException } from 'oak-general-business';
function showMessage(reason: OakException<any>, features: AFD, type?: MessageProps['type']) {
const { message, _module, params} = reason;
if (_module) {
features.message.setMessage({
content: features.locales.t(message, {
['#oakModule']: _module,
...(params || {}),
}),
type: type || 'error',
});
}
else {
features.message.setMessage({
content: message,
type: type || 'error',
});
}
}
function navigateTo(features: AFD, url: string, state?: Record<string, any>) {
const { pathname } = features.navigator.getLocation();
// 页面已经在本页的话,就不在跳转。这里要处理路径字符串的区别
const s1 = pathname.split('/').filter(ele => !!ele);
const s2 = url.split('/').filter(ele => !!ele);
if (s1.length === s2.length) {
let same = true;
s1.forEach(
(ele, idx) => {
if (ele !== s2[idx]) {
same = false;
}
}
);
if (same) {
return;
}
}
features.navigator.navigateTo({ url }, state);
}
/** /**
* backUrl * backUrl
* @param location * @param location
* @param encode * @param encode
* @returns * @returns
*/ */
export const handler = async (reason: any, features: AFD) => { export const handler = async (reason: any, features: AFD) => {
if (reason instanceof OakException) { if (reason instanceof OakException) {
if (reason instanceof OakUnloggedInException) { if (reason instanceof OakUnloggedInException) {
// await features.token.logout(); navigateTo(features, '/login');
features.navigator.navigateTo( }
{ else if (reason instanceof OakInputIllegalException) {
url: '/login', showMessage(reason, features);
}, }
{ isGoBack: true }, else if (reason instanceof OakHasToVerifyPassword) {
true navigateTo(features, '/my/password/verify');
); }
} else if (reason instanceof OakInputIllegalException) { else if (reason instanceof OakPasswordUnset) {
features.message.setMessage({ navigateTo(features, 'my/password/update');
content: features.locales.t(reason.message, {}), }
type: 'error', else if (reason instanceof OakAttrNotNullException) {
}); showMessage(reason, features);
} else if (reason instanceof OakAttrNotNullException) { } else if (reason instanceof OakUserInfoLoadingException || reason instanceof OakApplicationLoadingException) {
features.message.setMessage({
content: features.locales.t(reason.message, {}),
type: 'error',
});
}/* else if (reason instanceof OakUserInfoLoadingException || reason instanceof OakApplicationLoadingException) {
// 暂不处理在oak-general-business的page的生命周期里等token加载完成了会刷新页面的 // 暂不处理在oak-general-business的page的生命周期里等token加载完成了会刷新页面的
console.warn(reason); // console.warn(reason);
} */else if (reason instanceof ExampleException) { } else if (reason instanceof ExampleException) {
console.log('在此处理ExampleException'); console.log('在此处理ExampleException');
} else { } else {
console.warn(reason); console.warn(reason);
features.message.setMessage({ showMessage(reason, features);
content: features.locales.t(reason.message, {}),
type: 'error',
});
} }
return true; return true;
} }

View File

@ -1,15 +1,9 @@
import { OakException, OakUserException } from 'oak-domain/lib/types'; import { OakException, OakUserException } from 'oak-domain/lib/types';
import { EntityDict } from '@oak-app-domain'; import { EntityDict } from '@project/oak-app-domain';
import makeDepedentException from './DependentExceptions'; import makeDepedentException from './DependentExceptions';
export class ExampleException extends OakException<EntityDict> {}; export class ExampleException extends OakException<EntityDict> {};
export class ConsoleModeIllegalException extends OakException<EntityDict> {};
export class ConsoleLoadingDataException extends OakException<EntityDict> {};
export class ExistsNewBidException extends OakUserException<EntityDict> {};
export function makeException(msg: string | object) { export function makeException(msg: string | object) {
const data = typeof msg === 'string' ? JSON.parse(msg) : msg; const data = typeof msg === 'string' ? JSON.parse(msg) : msg;
@ -18,10 +12,10 @@ export function makeException(msg: string | object) {
return exception; return exception;
} }
const { name, message } = data; const { name, message, _module, params } = data;
switch (name) { switch (name) {
case 'ExampleException': { case 'ExampleException': {
return new ExampleException(message); return new ExampleException(message, _module, params);
} }
default: { default: {
throw new OakException(`不可解读的exception信息「${msg}`); throw new OakException(`不可解读的exception信息「${msg}`);