koatty icon indicating copy to clipboard operation
koatty copied to clipboard

使用koa-session时,返回报错

Open LYJSPEEDX opened this issue 2 years ago • 2 comments

koatty确实是很优雅的框架,但在开发过程中,配合koa-session与koatty使用时,使用BaseController的return this.ok(res)时,会导致返回抛出错误(404),如下图 image 但改用this.ctx.body = res返回时,则一切正常

我的SessionMiddleware.ts文件如下: image

业务代码,UserController.ts image

LYJSPEEDX avatar Sep 03 '22 08:09 LYJSPEEDX

改成这样呢:

const session = require('koa-session');
@Middleware()
export class SessionMiddleware implements IMiddleware {
    run(options: any, app: App) {
        return session(options);
    }
}

文档: https://koatty.org/#/?id=%e4%bd%bf%e7%94%a8koa%e4%b8%ad%e9%97%b4%e4%bb%b6

richenlin avatar Sep 05 '22 02:09 richenlin

修改成:

@Middleware()
export class SessionMiddleware implements IMiddleware {
    run(options: any, app: App) {
        return session(options, app);
    }
}

只要一使用this.ok()返回值,就会抛出错误 错误依旧,依然是

[2022-09-09 09:02:08.028] [ERROR] Error
    at httpHandler (/Users/x/Desktop/project/node_modules/koatty_trace/dist/index.js:189:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async respWapper (/Users/x/Desktop/project/node_modules/koatty_trace/dist/index.js:469:17)
[2022-09-09 09:02:08.035] [ERROR] {"action":"POST","code":"404","startTime":"1662685327394","duration":"641","traceId":"","endTime":"1662685328035","path":"/api/user/login"}

LYJSPEEDX avatar Sep 09 '22 01:09 LYJSPEEDX

修改成:

@Middleware()
export class SessionMiddleware implements IMiddleware {
    run(options: any, app: App) {
        return session(options, app);
    }
}

只要一使用this.ok()返回值,就会抛出错误 错误依旧,依然是

[2022-09-09 09:02:08.028] [ERROR] Error
    at httpHandler (/Users/x/Desktop/project/node_modules/koatty_trace/dist/index.js:189:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async respWapper (/Users/x/Desktop/project/node_modules/koatty_trace/dist/index.js:469:17)
[2022-09-09 09:02:08.035] [ERROR] {"action":"POST","code":"404","startTime":"1662685327394","duration":"641","traceId":"","endTime":"1662685328035","path":"/api/user/login"}

TKS, 已经明确是 this.ok的一个bug, 将在下一版本修复

richenlin avatar Oct 31 '22 08:10 richenlin

问题原因已经定位

跟踪 koa-session源码,发现:


 return async function session(ctx, next) {
    const sess = ctx[CONTEXT_SESSION];
    if (sess.store) await sess.initFromExternal();
    try {
      await next();
    } catch (err) {
      throw err;
    } finally {
      if (opts.autoCommit) {
        await sess.commit();
      }
    }
  };

使用了 await next();, 在执行完控制器后,没有将控制器返回的结果return,导致ctx.body没有赋值,返回了404。

richenlin avatar Oct 31 '22 09:10 richenlin

修改成:

@Middleware()
export class SessionMiddleware implements IMiddleware {
    run(options: any, app: App) {
        return session(options, app);
    }
}

只要一使用this.ok()返回值,就会抛出错误 错误依旧,依然是

[2022-09-09 09:02:08.028] [ERROR] Error
    at httpHandler (/Users/x/Desktop/project/node_modules/koatty_trace/dist/index.js:189:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async respWapper (/Users/x/Desktop/project/node_modules/koatty_trace/dist/index.js:469:17)
[2022-09-09 09:02:08.035] [ERROR] {"action":"POST","code":"404","startTime":"1662685327394","duration":"641","traceId":"","endTime":"1662685328035","path":"/api/user/login"}

升级 [email protected] 可以解决此问题。

升级注意事项: [email protected]作为一个中版本,有不兼容的变更:

  • HttpController 基类被移除,如果项目中继承了此类,请将历史版本的HttpController.ts文件放入项目内。

richenlin avatar Nov 01 '22 09:11 richenlin