怎樣從Express API發送一致的錯誤響應

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作者 | Simon Plenderleith譯者 | 王強策劃 | 李俊辰"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 Express 構建 API 時,可能很難知道如何發送一致的錯誤響應。這個框架似乎並未爲此提供什麼特殊功能,因此你需要自己去解決問題。某些時候,你可能會想知道自己是否在以“正確的方式”操作。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"正如我在《使用 Express 構建現代 API 的 5 種最佳實踐》博客文章中提到的那樣:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"構建 API 來發明自己的錯誤響應格式是非常誘人的,但是 HTTP 響應狀態代碼是一個很好的起點,因爲它們可以傳達特定的錯誤狀態。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你發明了自己的格式,就必須在 API 中構建一堆額外的邏輯,並且你可能還要確保對它們進行了全面的測試。沒有人想在錯誤響應代碼中還看到錯誤,不是嗎?最重要的是,它也需要客戶端,例如前端 JavaScript,來實現用於處理 API 錯誤響應特殊格式的額外代碼。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果有一種更簡單的方法,一種經過實踐檢驗的標準方法來發送錯誤響應,那豈不是很好嗎?幸運的是,這種方法是存在的!HTTP 標準定義了狀態代碼,你可以在 API 響應中使用這些狀態代碼來指示請求是否成功,或是否發生了錯誤。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面是一個 HTTP 錯誤響應示例,帶有 400 狀態代碼,指示來自客戶端的“錯誤請求”(Bad Request):"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"< HTTP\/1.1 400 Bad Request\n< Content-Type: text\/html; charset=utf-8\n< Content-Length: 138\n< Date: Wed, 28 Oct 2020 20:11:07 GMT\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果要發送這樣的錯誤響應,可以使用 Express 的 res.status() 方法:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"res.status(400).end();\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"遵循 HTTP 標準"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章