How to print path to the current Zone in zone.js : <p>I'm experimenting with zones (<a href="https://github.com/angular/zone.js/" rel="noreferrer">zone.js</a>) and I realized I don't know what's the best way to print all the zones from root to the current zone that threw an error.</p>

<p>For example this code uses two nested callbacks with <code>setTimeout()</code> and then calls a function called <code>failedFunc()</code> that throws an error:</p>

<pre><code>require('zone.js');

function failedFunc() {
  throw new Error("it's broken");
}

let rootZone = Zone.current;

function func1() {
  let zoneA = rootZone.fork({name: 'zoneA'});
  zoneA.run(() =&gt; {
    setTimeout(() =&gt; func2());
  });
}

function func2() {
  let zoneB = Zone.current.fork({name: 'zoneB'});

  zoneB.run(() =&gt; {
    setTimeout(() =&gt; failedFunc());
  });
}

func1();
</code></pre>

<p>When I run this example it gives the following output:</p>

<pre><code>/.../zone-test/node_modules/zone.js/dist/zone-node.js:170
                        throw error;
                        ^
Error: it's broken
    at new Error (native)
    at failedFunc (/.../zone-test/zone_02.js:12:9) [zoneB]
    at Timeout.setTimeout (/.../zone-test/zone_02.js:28:22) [zoneB]
    at Zone.runTask (/.../zone-test/node_modules/zone.js/dist/zone-node.js:166:47) [&lt;root&gt; =&gt; zoneB]
    at Timeout.ZoneTask.invoke (/.../zone-test/node_modules/zone.js/dist/zone-node.js:416:38) [&lt;root&gt;]
    at Timeout.data.args.(anonymous function) [as _onTimeout] (/.../zone-test/node_modules/zone.js/dist/zone-node.js:1590:25) [&lt;root&gt;]
    at ontimeout (timers.js:365:14) [&lt;root&gt;]
    at tryOnTimeout (timers.js:237:5) [&lt;root&gt;]
    at Timer.listOnTimeout (timers.js:207:5) [&lt;root&gt;]
</code></pre>

<p>The correct path for the zone that threw the error is <code>&lt;root&gt;</code> => <code>zoneA</code> => <code>zoneB</code>.</p>

<p>However it's not obvious from the output. There's just <code>[root]</code> and <code>zoneB</code> and it doesn't mention the <code>zoneA</code> which is confusing. I guess this is because the <code>zone.js</code>'s monkey patch only adds zone info to the lines in the call stack. So when I'm using <code>setTimeout()</code> than creating <code>zoneA</code> doesn't correspond to any line in the output and that's why I don't see it anywhere.</p>

<p>Nonetheless, I can print the path to the current zone by iterating all it's parents but this means I need to know where the error first happened and add the following code to it (which would be very tedious in practice):</p>

<pre><code>function failedFunc() {
  let zone = Zone.current;
  let zones = [];
  while (zone) {
    zones.push(zone.name);
    zone = zone.parent;
  }
  console.log(zones);

  throw new Error("it's broken");
}

// ...
</code></pre>

<p>Now when I run this I'll get what I need:</p>

<pre><code>[ 'zoneB', 'zoneA', '&lt;root&gt;' ]

/.../zone-test/node_modules/zone.js/dist/zone-node.js:170
                        throw error;
                        ^
Error: it's broken
    at new Error (native)
</code></pre>

<p>So I'm wondering how to use zone.js in practice and in a way that it's easier than this.</p>
