【问题标题】:chrome, opera, firefox, console not show error?chrome、opera、firefox、控制台不显示错误?
【发布时间】:2020-05-14 11:12:52
【问题描述】:

最近发生了,我发现我所有的浏览器,chrome,opera,firefox,console都不再显示错误,只是默默地停止js脚本。

console.log(' undefined variable should show error ', aa, bb)

之前,控制台会显示错误:'aa' is undefined,但现在,控制台上没有任何显示,我有最新版本的opera是v66.0.x,chronm是v79.0.x

编辑 2020 年 1 月 29 日

我有点找到问题所在。如果我把

         console.log(' undefined variable should show error ', aa, bb) 

在常规位置,浏览器会输出:

              Uncaught ReferenceError: aa is not defined

但是,如果我将相同的代码放入 fetch 流中,即使此代码已实现、运行到,浏览器、控制台也不会显示任何错误消息,只需停止运行 js 脚本静默!

                  // ----- fetch.stream api -----
                  fetch(_url_decoded)
                             // Retrieve its body as ReadableStream
                      .then(response => response.body)
                      .then(rs => {

                                  //console.log('test ', uuuu)


                                  const reader = rs.getReader();

                                  return new ReadableStream({


                                    async start(controller) {




                                            while (true) {


                                                          const { done, value } = await reader.read();

                                                          // When no more data needs to be consumed, break the reading
                                                          if (done) {


                                                                  console.log('test ', uuuu)

我在所有最新版本的firefox,chrome,opera上测试了这个案例,所有相同的静音停止而不输出任何错误消息!!!

  Who can tell me why?

【问题讨论】:

  • 也许你有一些.catch() 处理程序?

标签: javascript google-chrome console


【解决方案1】:

我解决了问题,解释起来很复杂,我不知道问题出在哪里。

This is working code:控制台将报告任何未定义变量的错误

        fetch("https://www.example.org/").then((response) => {
               const reader = response.body.getReader();
                const stream = new ReadableStream({
                    start(controller) {
                            // The following function handles each data chunk
                              function push() {
    // "done" is a Boolean and value a "Uint8Array"
    reader.read().then(({ done, value }) => {
      // Is there no more data to read?
      if (done) {
        // Tell the browser that we have finished sending data
        controller.close();
        return;
      }

      // Get the data and send it to the browser via the controller
      controller.enqueue(value);
      push();
    });
  };

  push();
}
});

  return new Response(stream, { headers: { "Content-Type": "text/html" } });
});

我原来的代码有问题,控制台不会报任何未定义变量的错误,只是安静地停止js引擎。

                 fetch(_url_decoded)
                             // Retrieve its body as ReadableStream
                      .then(response => response.body)
                      .then(rs => {

                                  // will catch error at below catch block
                                  // console.log('test ', uuuu)


                                  const reader = rs.getReader();

                                  return new ReadableStream({


                                    async start(controller) {

                                                                // will not show error undefined in console.  
                                                                //console.log('test ', uuuu) 


                                            while (true) {


                                                          const { done, value } = await reader.read();




                                                          // When no more data needs to be consumed, break the reading
                                                          if (done) {

                                                                    break;   // or,  return, depends on, here use while loop, so use break. if it is recursive function "processText()",then use 'return' here.
                                                          }




                                                          // Enqueue the next data chunk into our target stream
                                                          controller.enqueue(value);

                                            } //while




                                            // after 'done' , break the while loop, come here to Close the stream

                                            controller.close();
                                            reader.releaseLock();

                                            return;
                                    } // start




                                  }) // ReadableStream



                    })// then   
                    .catch((error) => {
                      console.error('fetch Error -> :', error);
                    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-09
    • 2013-10-03
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多