拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 使用docker时API请求状态码不同

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

白鹭 - 2022-03-03 2039 0 0

我正在使用我在本地没有 docker 的情况下开发的 Strapi 3.6 开发一个 api,我在生产中使用 docker 运行。

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

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

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

编辑

后端部署在 AWS Elastic Beanstalk 上。

我尝试了什么:

  • ? 直接请求负载均衡器
  • ? 直接请求底层 ec2
  • ? 在生产模式下本地运行 docker
  • ? 在开发模式下本地运行 docker
  • ? 像 docker 一样构建 Strapi 并运行服务器,但没有 docker

以上所有案例都给了我 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
  }
]

码头工人日志:

[
  {
    "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
  }
]

uj5u.com热心网友回复:

查看日志,我看到在生产中,api 回传了这一行:

...
"isBoom": true,
...

这意味着错误boom由默认情况下由strapi附带的全域错误处理程序处理

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

深入查看日志后,我还发现requestId生产中缺少该属性,这是我们还在自定义中间件之一中添加的一条信息。

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

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

现在一切都好。

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

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *

热门标签