lrf hai 11 meses
pai
achega
3212dcdab9

+ 1 - 1
src/config/config.default.ts

@@ -23,7 +23,7 @@ export default {
       },
       zh_cn: {
         default: require('../locales/zh_cn/defaults'),
-        login: require('../locales/zh_cn/login'),
+        LoginError: require('../locales/zh_cn/error/LoginError'),
       },
     },
   },

+ 8 - 0
src/error/Readme.md

@@ -0,0 +1,8 @@
+# 自定义异常
+
+## 变量与文件名
+  - config.default.ts i18n.localTable 下所有语种的 key 和 引入的文件名
+  - error目录下面的 自定义异常文件中的 code 
+  - locales目录下 各个语种下的error目录的文件名 与 文件中的 codePrefix
+  
+#### errorcode 一般都是 "=" 左右两侧是一个值 值也可以无视大小写,都会经过全大写处理

+ 4 - 5
src/error/login.error.ts

@@ -1,18 +1,17 @@
 import { MidwayError, registerErrorCode } from '@midwayjs/core';
+// 需保持一致的标识,具体看error的readme
+const code = 'LoginError';
 
 export enum LoginErroCode {
   TEST = 'TEST',
 }
-export const FrameworkErrorEnum = registerErrorCode(
-  'LoginError',
-  LoginErroCode
-);
+export const FrameworkErrorEnum = registerErrorCode(code, LoginErroCode);
 
 export class LoginError extends MidwayError {
   detail: any;
   group: string;
   constructor(errcode: string) {
     super(errcode);
-    this.group = 'login';
+    this.group = code;
   }
 }

+ 3 - 4
src/filter/customError.filter.ts

@@ -8,7 +8,7 @@ import { Context } from '@midwayjs/koa';
 import { LoginError } from '../error/login.error';
 import { ServiceError } from '../error/service.error';
 import { MidwayI18nService } from '@midwayjs/i18n';
-
+import { toLower } from 'lodash';
 interface CustomError extends Error {
   group: string;
 }
@@ -19,14 +19,13 @@ export class CustomErrorFilter {
   ctx: Context;
   @ApplicationContext()
   applicationContext: IMidwayContainer;
-  // @Inject()
-  // i18n: MidwayI18nService;
   async catch(err: CustomError, ctx: Context) {
     // 所有的未分类错误会到这里
     const i18n = await this.applicationContext.getAsync(MidwayI18nService);
     const errmsg = i18n.translate(err.message, {
       locale: this.ctx.locale,
-      group: err.group,
+      // 需要全小写
+      group: toLower(err.group),
     });
     const result: any = {
       errcode: err.message,

+ 1 - 1
src/locales/zh_cn/login.ts

@@ -7,6 +7,6 @@ const obj = {
 const listObject = {};
 Object.keys(obj).map(i => {
   const val = obj[i];
-  listObject[`${toUpper(codePrefix)}_${toUpper(i)}`] = val;
+  listObject[`${toUpper(codePrefix)}_${toUpper(i)}`] = toUpper(val);
 });
 export default listObject;