我正在使用我在本地沒有 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 為我指明了正確的方向????
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/407329.html
標籤:
