mjsonrpc_send_request impossible to debug

Issue #94 closed
Stefan Ritt created an issue

The midas library call mjsonrpc_send_request uses a promise which in principle is a good idea. The downside is that it's very hard to debug. Let's assume we have code like

      mjsonrpc_send_request(req).then(function (rpc) {
         callback(rpc);
      }).catch(function (error) {
         alert("RPC error");
      });

If anything in callback() goes bad, it throws an exception, but that exception is caught by the catch() function of the promise, and therefore the alert("RPC error") is executed. I would rather prefer that the exception is not caught and my browser stops the JS code and tells me in which line of callback() I have a problem.

Comments (5)

  1. dd1

    in google-chrome, I go to "inspect->source" and check "pause on exceptions" and "pause on caught exceptions", then it does exactly what you asked for. what is your browser? K.O.

  2. Stefan Ritt reporter

    Thanks, didn't know that trick. Unfortunately my chrome has several extensions, which create exceptions all the time, so I have to skip like 10 exceptions until I get to the one I want. Is there a way to paus on exceptions only in a certain file?

  3. Stefan Ritt reporter

    I just figured out that rewriting the catch() block fixes the problem. Originally I had

    mjsonrpc_send_request(req).then(function (rpc) {
             callback(rpc);
          }).catch(function (error) {
             if (error.xhr.readyState == 4 && error.xhr.status == 0)
               alert("RPC error");
          });
    

    Now if the callback() function has a syntax or runtime error, the catch() block is called and crashes, since error.xhr is not define. In this case, the error is a simple string describing the syntax or runtime error. Changing the code to the one below fixes the problem, so the proper error is shown.

    mjsonrpc_send_request(req).then(function (rpc) {
             callback(rpc);
          }).catch(function (error) {
             if (error.xhr && error.xhr.readyState == 4 && error.xhr.status == 0)
               alert("RPC error");
             else
               alert(error);
          });
    
  4. Log in to comment