【问题标题】:How to expect and receive 404 as normal response in swagger-api/swagger-js, not an error如何在 swagger-api/swagger-js 中期望和接收 404 作为正常响应,而不是错误
【发布时间】:2018-11-19 15:37:01
【问题描述】:

是否可以将 Swagger-JS 客户端配置为不将 404 视为错误,甚至预期它?

In the docs 它以“未找到”作为正常响应的示例来暗示这一点:

Swagger.http(request)
  .then((res) => {
    res.statusCode // status code
    res.statusText // status text, ie: "Not Found"
    res.body       // JSON object or undefined
    res.obj        // same as above, legacy
    res.text       // textual body, or Blob
    res.headers    // header hash
  })
  .catch((err) => {
    err            // instanceof Error
    err.response   // response or null
  })

但是我发现 404 总是作为捕获的错误出现。

在我的应用程序中,我正在调用我的 REST API 以从队列中获取下一个项目,并且 404 是该过程的自然结束,在这种情况下不是语义上的错误。

这很好,当我打开 JS 异常断点时,我的 IDE 在n.then.a.responseError() 的缩小客户端代码中的底层抛出中断,这很烦人。

【问题讨论】:

    标签: javascript node.js swagger


    【解决方案1】:

    自行回答我刚刚想到的替代解决方案,我想我可以更改 API 以在列表末尾返回成功类响应:

    204 No Content
    The server successfully processed the request and is not returning any content
    

    wikipedia

    我不确定这在 RESTful API 中有多普遍或合适——我以前从未使用过它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-12
      • 2021-09-18
      • 2022-08-19
      • 2013-10-01
      • 2018-06-11
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      相关资源
      最近更新 更多