Closed mynamexjl closed 1 year ago
import { Context } from '@midwayjs/web';
(哈哈哈哈。 上面问题完美解决了哈。 能说下 egg 和。web 的 context的区别吗 )还有个问题 typeorm synchronize 日常同步出现表已创建问题导致程序卡住问题(云 mysql 上开启会存在这个问题 本地上是不会) 我不想把 synchronize 设置 false 在通过 cli 去同步的话
1、包含关系 2、不了解,一般不会开。
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
configuration.ts
import { App, Configuration, Inject } from '@midwayjs/decorator'; import { ILifeCycle, IMidwayContainer, MidwayDecoratorService, } from '@midwayjs/core'; // eslint-disable-next-line node/no-extraneous-import import { Application } from 'egg'; import { join } from 'path'; import as oss from '@midwayjs/oss'; import as redis from '@midwayjs/redis'; import as axios from '@midwayjs/axios'; import as validate from '@midwayjs/validate'; import as midwayAuto from '@ali/midway-auto'; // import as typegoose from '@midwayjs/typegoose'; import as swagger from '@midwayjs/swagger'; import as keycenter from '@ali/midway-keycenter'; import { MethodDecorators } from './decorator'; import as bull from '@midwayjs/bull'; import as bullBoard from '@midwayjs/bull-board'; import as orm from '@midwayjs/typeorm'; import { DbInitService } from './service/DbInit'; import as upload from '@midwayjs/upload'; import * as crossDomain from '@midwayjs/cross-domain'; import { MidwayErrorFilter, OtherErrorFilter, ValidateErrorFilter, } from './filter/error';
@Configuration({ imports: [ upload, midwayAuto, oss, redis, // 导入 redis 组件 axios, // http请求 validate, // 参数校验 keycenter, // 加载 keycenter 组件 // typegoose, // 使用mongodb 数据库需要打开这个 bull, // 任务队列 bullBoard, // bull ui orm, // typeorm crossDomain, // 跨域 { component: swagger, // 加载 swagger 组件 enabledEnvironment: ['local'], }, ], importConfigs: [join(__dirname, './config')], }) export class ContainerLifeCycle implements ILifeCycle { @App() app: Application;
@Inject() decoratorService: MidwayDecoratorService;
@Inject() dbInitService: DbInitService;
/**
@param container */ async onConfigLoad(container: IMidwayContainer) { console.log('config env:' + this.app.getEnv()); }
async onReady(): Promise {
// 注册中间件 useMiddleware
// 注册全局处理器 useFilter // 错误统一处理 this.app.useFilter([ MidwayErrorFilter, ValidateErrorFilter, OtherErrorFilter, ]);
// 注册方法修饰器 for (const { key, fn } of MethodDecorators) { this.decoratorService.registerMethodHandler(key, fn); } } }
fiter/error.ts import { Catch, MidwayError } from '@midwayjs/core'; import { MidwayValidationError } from '@midwayjs/validate'; import { Context } from 'egg'; import { Result } from '../function/Result'; import { RESULT_CODE_ERROR, RESULT_MSG_ERROR, RESULT_SERVICE_CODE_ERROR, } from '../constant/constant';
@Catch(MidwayError) export class MidwayErrorFilter { async catch(err: MidwayError, ctx: Context) { console.log('=== MidwayErrorFilter'); console.log(err); // 框架错误信息不抛出 若是后面需要下发自定义错误码可后面扩展 ctx.body = Result.errMsg(RESULT_MSG_ERROR, RESULT_SERVICE_CODE_ERROR); } }
@Catch(MidwayValidationError) export class ValidateErrorFilter { async catch(err: MidwayError, ctx: Context) { console.log('=== MidwayValidationError'); console.log(err); ctx.body = Result.errMsg(err.message, RESULT_CODE_ERROR); } }
@Catch() export class OtherErrorFilter { async catch(err: Error, ctx: Context) { console.log('=== OtherErrorFilter'); console.log(err); ctx.body = Result.errMsg(err.message, RESULT_CODE_ERROR); } }
启动项目直接会报格式类型错
错误信息:
start error: ⨯ Unable to compile TypeScript: src/configuration.ts(136,7): error TS2322: Type 'typeof MidwayErrorFilter' is not assignable to type 'new (...args: any[]) => IFilter<Context, unknown, unknown>'.
Construct signature return types 'MidwayErrorFilter' and 'IFilter<Context, unknown, unknown>' are incompatible.
The types of 'catch' are incompatible between these types.
Type '(err: MidwayError, ctx: Context) => Promise' is not assignable to type '(err: Error, ctx: Context, res?: unknown, next?: unknown) => any'.
Types of parameters 'ctx' and 'ctx' are incompatible.
Type 'Context' is not assignable to type 'Context'.
The types returned by 'cleanupRequestFiles(...)' are incompatible between these types.
Type 'Promise<boolean[]>' is not assignable to type 'Promise'.
Type 'boolean[]' is not assignable to type 'void'.
src/configuration.ts(137,7): error TS2322: Type 'typeof ValidateErrorFilter' is not assignable to type 'new (...args: any[]) => IFilter<Context, unknown, unknown>'.
Construct signature return types 'ValidateErrorFilter' and 'IFilter<Context, unknown, unknown>' are incompatible.
The types of 'catch' are incompatible between these types.
Type '(err: MidwayError, ctx: Context) => Promise' is not assignable to type '(err: Error, ctx: Context, res?: unknown, next?: unknown) => any'.
Types of parameters 'ctx' and 'ctx' are incompatible.
Type 'Context' is not assignable to type 'Context'.
src/configuration.ts(138,7): error TS2322: Type 'typeof OtherErrorFilter' is not assignable to type 'new (...args: any[]) => IFilter<Context, unknown, unknown>'.
Construct signature return types 'OtherErrorFilter' and 'IFilter<Context, unknown, unknown>' are incompatible.
The types of 'catch' are incompatible between these types.
Type '(err: Error, ctx: Context) => Promise' is not assignable to type '(err: Error, ctx: Context, res?: unknown, next?: unknown) => any'.
Types of parameters 'ctx' and 'ctx' are incompatible.
Type 'Context' is not assignable to type 'Context'.
是否有解决方案