Api 使用 docker 时请求状态码不同

Api request status code different when using docker

我正在使用 strapi 3.6 开发一个 api,我在本地开发时没有 docker,我在生产中 运行 使用 docker。

在本地,returns 400 将在生产中变为 500 的端点。

我一直在调查这个问题,当我在本地 运行 docker 时,我也得到了 500 而不是 400。

我找不到任何相关资源,有人知道为什么会发生这种情况吗?

编辑

后端部署在 AWS Elastic Beanstalk 上。

我尝试了什么:

以上所有案例都给了我 500,而不是预期的 400,除了最后一个案例,它按预期返回了 400。

日志中显示的错误是预期的错误。只有 api 返回错误的方式发生了变化。

这里是全局处理错误的中间件代码:

module.exports = (strapi) => {
  return {
    initialize() {
      strapi.app.use(async (ctx, next) => {
        try {
          await next();
        } catch (e) {
          strapi.log.error(e, e.stack);

          // The error raised is a BusinessError
          // Logs are the ones expected
          if (e instanceof BusinessError) {
            ctx.status = 400;
            ctx.body = {
              message: e.message,
              code: e.code,
            };
          } else {
            throw e;
          }
        }
      });
    },
  };
};

编辑 2

本地日志:

[
  {
    "level": 50,
    "time": 1641722347605,
    "pid": 3986,
    "hostname": "MacBook-Pro-de-Baptiste-2.local",
    "status": 400,
    "headers": {},
    "body": {
      "code": "ValidationError",
      "message": "Validation error(s) present. See embedded errors list for more details.",
      "_embedded": {
        "errors": [{ "code": "Invalid", "message": "Invalid amount. The supplied amount is greater than your transaction limit.", "path": "/amount/value", "_links": {} }]
      }
    },
    "requestId": "eeb84a16-b250-40d2-9a53-901f92843aa1",
    "v": 1
  },
  {
    "level": 50,
    "time": 1641722347743,
    "msg": "BusinessError: User has reached their transfer limit\n    at Object.makeTransfer (/Users/baboo/Documents/projects/burrow/burrow-backend/api/dwolla/services/dwolla.js:154:13)\n    at async makeTransfer (/Users/baboo/Documents/projects/burrow/burrow-backend/api/payment/controllers/submitOrder.js:83:7)\n    at async /Users/baboo/Documents/projects/burrow/burrow-backend/api/payment/controllers/submitOrder.js:121:25",
    "pid": 3986,
    "hostname": "MacBook-Pro-de-Baptiste-2.local",
    "name": "BusinessError",
    "code": "USER_TRANSFER_LIMIT",
    "requestId": "eeb84a16-b250-40d2-9a53-901f92843aa1",
    "v": 1
  },
  {
    "level": 20,
    "time": 1641722347744,
    "msg": "POST /checkout/submit-order (7616 ms) \u001b[33m400\u001b[39m",
    "pid": 3986,
    "hostname": "MacBook-Pro-de-Baptiste-2.local",
    "requestId": "eeb84a16-b250-40d2-9a53-901f92843aa1",
    "v": 1
  }
]

Docker 日志:

[
  {
    "level": 50,
    "msg": "{\"code\":\"ValidationError\",\"message\":\"Validation error(s) present. See embedded errors list for more details.\",\"_embedded\":{\"errors\":[{\"code\":\"Invalid\",\"message\":\"Invalid amount. The supplied amount is greater than your transaction limit.\",\"path\":\"/amount/value\",\"_links\":{}}]}}",
    "pid": 17,
    "hostname": "ef76780ba954",
    "type": "Error",
    "stack": "Error: {\"code\":\"ValidationError\",\"message\":\"Validation error(s) present. See embedded errors list for more details.\",\"_embedded\":{\"errors\":[{\"code\":\"Invalid\",\"message\":\"Invalid amount. The supplied amount is greater than your transaction limit.\",\"path\":\"/amount/value\",\"_links\":{}}]}}\n    at errorFrom (/strapi/node_modules/dwolla-v2/src/dwolla/Token.js:49:15)\n    at /strapi/node_modules/dwolla-v2/src/dwolla/Token.js:70:29\n    at tryCatcher (/strapi/node_modules/bluebird/js/release/util.js:16:23)\n    at Promise._settlePromiseFromHandler (/strapi/node_modules/bluebird/js/release/promise.js:547:31)\n    at Promise._settlePromise (/strapi/node_modules/bluebird/js/release/promise.js:604:18)\n    at Promise._settlePromise0 (/strapi/node_modules/bluebird/js/release/promise.js:649:10)\n    at Promise._settlePromises (/strapi/node_modules/bluebird/js/release/promise.js:729:18)\n    at _drainQueueStep (/strapi/node_modules/bluebird/js/release/async.js:93:12)\n    at _drainQueue (/strapi/node_modules/bluebird/js/release/async.js:86:9)\n    at Async._drainQueues (/strapi/node_modules/bluebird/js/release/async.js:102:5)\n    at Immediate.Async.drainQueues [as _onImmediate] (/strapi/node_modules/bluebird/js/release/async.js:15:14)\n    at processImmediate (internal/timers.js:464:21)",
    "status": 400,
    "headers": {},
    "body": {
      "code": "ValidationError",
      "message": "Validation error(s) present. See embedded errors list for more details.",
      "_embedded": {
        "errors": [{ "code": "Invalid", "message": "Invalid amount. The supplied amount is greater than your transaction limit.", "path": "/amount/value", "_links": {} }]
      }
    },
    "v": 1
  },
  {
    "level": 50,
    "msg": "User has reached their transfer limit",
    "pid": 17,
    "hostname": "ef76780ba954",
    "type": "Error",
    "stack": "BusinessError: User has reached their transfer limit\n    at Object.makeTransfer (/strapi/api/dwolla/services/dwolla.js:154:13)\n    at async makeTransfer (/strapi/api/payment/controllers/submitOrder.js:83:7)\n    at async /strapi/api/payment/controllers/submitOrder.js:121:25",
    "name": "BusinessError",
    "code": "USER_TRANSFER_LIMIT",
    "isBoom": true,
    "isServer": true,
    "data": null,
    "output": { "statusCode": 500, "payload": { "statusCode": 500, "error": "Internal Server Error", "message": "An internal server error occurred" }, "headers": {} },
    "v": 1
  },
  {
    "level": 20,
    "msg": "POST /checkout/submit-order (3390 ms) 500",
    "pid": 17,
    "hostname": "ef76780ba954",
    "v": 1
  }
]

查看日志,我看到在生产环境中,api:

返回了这一行
...
"isBoom": true,
...

这意味着该错误默认由strapi自带的全局错误处理器boom处理。

但是我们创建了自己的全局错误处理程序。

更深入地查看日志,我还看到 属性 requestId 在生产中丢失了,我们也在我们的一个自定义中间件中添加了一条信息。

我推断我们的中间件没有在生产中加载。

打开 Dockerfile 时,我得到了解决问题的答案:我们在构建图像时根本没有复制 middlewares 文件夹。

现在一切都很好。

感谢@OneCricketeer 为我指明了正确的方向