0

This one is confusing to describe, so I'll just put a video

https://dl.dropboxusercontent.com/s/ju485susn7ypsgx/2015-12-09_15-03-50.mp4?dl=0

When I turn the server off, m.request throws errors (obviously) net::ERR_CONNECTION_REFUSED

  1. How can I handle these errors and just turn the data into an empty array [] (I know .then accepts a success and failure callback, but it doesn't seem to work)
  2. Why doesn't the app start working after I turn the server back on? (It seems like the issue is that m.route stops working?)
Farzher
  • 13,934
  • 21
  • 69
  • 100
  • What I think may be happening is that the error prevents execution of m.redraw. Mithril's m.request promise doesn't include a catch -- you could either use a better ajax library that has a catch (I love mithril but the m.request is a mess), or wrap your m.request in a promise and catch any errors. You could then resolve with an empty array and call m.redraw manually (I don't know if that'll be necessary). – dcochran Dec 10 '15 at 01:05
  • @dcochran I found the specific problem right here: http://i.imgur.com/HaXEKBO.png `deserialize` is `JSON.parse`, it's actually in a `try` block... How is it possible that this error is not being caught!? http://i.imgur.com/kZ74Arv.png – Farzher Dec 10 '15 at 03:27

1 Answers1

1

I fixed this by editing the mithril.js source. I had to wrap the call to m.endComputation at the bottom in a finally block.

I don't understand why the try/catch doesn't work, and why this finally is required. But I guess I'll post it as a bug.

    xhrOptions.onload = xhrOptions.onerror = function(e) {
        try {
            e = e || event;
            var unwrap = (e.type === "load" ? xhrOptions.unwrapSuccess : xhrOptions.unwrapError) || identity;
            var response = unwrap(deserialize(extract(e.target, xhrOptions)), e.target);
            if (e.type === "load") {
                if (type.call(response) === ARRAY && xhrOptions.type) {
                    for (var i = 0; i < response.length; i++) response[i] = new xhrOptions.type(response[i])
                }
                else if (xhrOptions.type) response = new xhrOptions.type(response)
            }
            deferred[e.type === "load" ? "resolve" : "reject"](response)
        }
        catch (e) {
            m.deferred.onerror(e);
            deferred.reject(e)
        } finally {
// This is now in a finally block
            if (xhrOptions.background !== true) m.endComputation()
        }
    };
Farzher
  • 13,934
  • 21
  • 69
  • 100
  • 1
    Actually, this is expected behavior and I once filed this as a bug, funny enough: https://github.com/lhorie/mithril.js/issues/780 Like I said, m.request is a mess. You need to pass in an extract option into your m.request (see link). – dcochran Dec 10 '15 at 19:05
  • 1
    @dcochran Wth?! They're silly, especially considering I fixed it simply by adding a `finally` block. I talked with some contributor about this, and I think he's going to push a fix while also cleaning up some of the messy ajax code. – Farzher Dec 10 '15 at 21:18