Coder Social home page Coder Social logo

Comments (1)

jaywhen avatar jaywhen commented on May 29, 2024

看了官网的实例,似乎没办法获取到后端500报错的body中的detail,打印看过onopen中的response,似乎也没有参数能直接获取到

这个库的实现里面,会

  • 先去执行fetch,拿到response,
  • 把上一步拿到的response传给onopen去执行
  • 处理流数据
  • 依次执行onclose,dispose,resolve(这部分可以不用细究)
  • 上面的几个步骤都在try中执行,catch住错误后如果不是内部主动中断请求,就会执行你传入的onerror

所以你如果希望获取到后端500报错的body中的detail,直接在onopen中从response中拿到即可(前提是你说的这个detail后端返回体中存在,假如你说的是后端返回的错误信息,我理解应该直接await response.json()即可拿到)

而且我发现onopen不抛异常的话onerror是捕获不了请求500的错误

根据上面说的执行逻辑, 你需要在onopen中判断响应是否是500,是的话抛出,才能在onerror中去捕获到,onerror中不负责对响应进行校验

from fetch-event-source.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.