feat: browser router add custom error
This commit is contained in:
parent
d6eb8393e0
commit
c462dc31f8
@ -1,4 +1,4 @@
|
||||
import { QueryRouterServer } from '@kevisual/router';
|
||||
import { CustomError, QueryRouterServer } from '@kevisual/router/browser';
|
||||
|
||||
const router = new QueryRouterServer();
|
||||
|
||||
@ -12,6 +12,18 @@ router
|
||||
})
|
||||
.addTo(router);
|
||||
|
||||
router
|
||||
.route({
|
||||
path: 'hello',
|
||||
key: 'world2',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
ctx.body = 'Hello, world!';
|
||||
// throw new CustomError('error');
|
||||
throw new CustomError(5000, 'error');
|
||||
})
|
||||
.addTo(router);
|
||||
|
||||
router
|
||||
.run({
|
||||
path: 'hello',
|
||||
@ -20,3 +32,12 @@ router
|
||||
.then((res) => {
|
||||
console.log(res);
|
||||
});
|
||||
|
||||
router
|
||||
.run({
|
||||
path: 'hello',
|
||||
key: 'world2',
|
||||
})
|
||||
.then((res) => {
|
||||
console.log(res);
|
||||
});
|
||||
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/package",
|
||||
"name": "@kevisual/router",
|
||||
"version": "0.0.4-alpha-4",
|
||||
"version": "0.0.4-alpha-5",
|
||||
"description": "",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.js",
|
||||
@ -50,8 +50,8 @@
|
||||
"require": "./dist/index.js"
|
||||
},
|
||||
"./browser": {
|
||||
"import": "./dist/route.js",
|
||||
"require": "./dist/route.js"
|
||||
"import": "./dist/browser.js",
|
||||
"require": "./dist/browser.js"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,9 +24,9 @@ export default [
|
||||
external: ['ws'],
|
||||
},
|
||||
{
|
||||
input: 'src/route.ts',
|
||||
input: 'src/browser.ts',
|
||||
output: {
|
||||
file: 'dist/route.js',
|
||||
file: 'dist/browser.js',
|
||||
format: 'es',
|
||||
},
|
||||
plugins: [
|
||||
@ -37,7 +37,7 @@ export default [
|
||||
typescript({
|
||||
allowImportingTsExtensions: true,
|
||||
noEmit: true,
|
||||
declaration: false,
|
||||
declaration: true,
|
||||
}),
|
||||
],
|
||||
},
|
||||
|
9
src/browser.ts
Normal file
9
src/browser.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export { Route, QueryRouter, QueryRouterServer } from './route.ts';
|
||||
|
||||
export { Rule, Schema, createSchema } from './validator/index.ts';
|
||||
|
||||
export type { RouteContext, RouteOpts } from './route.ts';
|
||||
|
||||
export type { Run } from './route.ts';
|
||||
|
||||
export { CustomError } from './result/error.ts';
|
@ -1,45 +1 @@
|
||||
export const Code400 = [
|
||||
{
|
||||
code: 400,
|
||||
msg: 'Bad Request',
|
||||
zn: '表示其他错误,就是4xx都无法描述的前端发生的错误',
|
||||
},
|
||||
{ code: 401, msg: 'Authentication', zn: '表示认证类型的错误' }, // token 无效 (无token, token无效, token 过期)
|
||||
{
|
||||
code: 403,
|
||||
msg: 'Authorization',
|
||||
zn: '表示授权的错误(认证和授权的区别在于:认证表示“识别前来访问的是谁”,而授权则是“赋予特定用户执行特定操作的权限”)',
|
||||
},
|
||||
{ code: 404, msg: 'Not Found', zn: '表示访问的数据不存在' },
|
||||
{
|
||||
code: 405,
|
||||
msg: 'Method Not Allowd',
|
||||
zn: '表示可以访问接口,但是使用的HTTP方法不允许',
|
||||
},
|
||||
];
|
||||
|
||||
export const ResultCode = [{ code: 200, msg: 'OK', zn: '请求成功。' }].concat(Code400);
|
||||
type ResultProps = {
|
||||
code?: number;
|
||||
msg?: string;
|
||||
userTip?: string;
|
||||
};
|
||||
export const Result = ({ code, msg, userTip, ...other }: ResultProps) => {
|
||||
const Code = ResultCode.find((item) => item.code === code);
|
||||
let _result = {
|
||||
code: code || Code?.code,
|
||||
msg: msg || Code?.msg,
|
||||
userTip: undefined,
|
||||
...other,
|
||||
};
|
||||
if (userTip) {
|
||||
_result.userTip = userTip;
|
||||
}
|
||||
return _result;
|
||||
};
|
||||
Result.success = (data?: any) => {
|
||||
return {
|
||||
code: 200,
|
||||
data,
|
||||
};
|
||||
};
|
||||
export * from './error.ts';
|
||||
|
22
src/route.ts
22
src/route.ts
@ -563,7 +563,25 @@ export class QueryRouterServer extends QueryRouter {
|
||||
}
|
||||
async run({ path, key, payload }: { path: string; key: string; payload?: any }) {
|
||||
const handle = this.handle;
|
||||
const end = handle({ path, key, ...payload });
|
||||
return end;
|
||||
const resultError = (error: string, code = 500) => {
|
||||
const r = {
|
||||
code: code,
|
||||
message: error,
|
||||
};
|
||||
return r;
|
||||
};
|
||||
try {
|
||||
const end = handle({ path, key, ...payload });
|
||||
return end;
|
||||
} catch (e) {
|
||||
if (e.code && typeof e.code === 'number') {
|
||||
return {
|
||||
code: e.code,
|
||||
message: e.message,
|
||||
};
|
||||
} else {
|
||||
return resultError('Router Server error');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user