<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>167934</bug_id>
          
          <creation_ts>2017-02-07 07:12:16 -0800</creation_ts>
          <short_desc>Rethrowing error resets stack trace making debugging hard</short_desc>
          <delta_ts>2017-09-14 22:39:48 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>JavaScriptCore</component>
          <version>Safari 10</version>
          <rep_platform>All</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>DUPLICATE</resolution>
          <dup_id>174380</dup_id>
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          <dependson>174380</dependson>
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Malte Ubl">malte</reporter>
          <assigned_to name="Saam Barati">saam</assigned_to>
          <cc>caitp</cc>
    
    <cc>dvoytenko</cc>
    
    <cc>fpizlo</cc>
    
    <cc>fred.wang</cc>
    
    <cc>saam</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1273981</commentid>
    <comment_count>0</comment_count>
    <who name="Malte Ubl">malte</who>
    <bug_when>2017-02-07 07:12:16 -0800</bug_when>
    <thetext>In JavaScript it is common to rethrow errors when they cannot immediately be handled. Here is some example code:

try {
  mayThrow();
} catch (e) {
  if (e.message == &apos;something I can handle&apos;) {
    handleError(e);
  } else {
    throw e;
  }
}

When executed in JavaScriptCore the error object created in the try block gets a new stack trace when it is rethrown. While there are situations when that might be desirable in the vast majority of cases it is not. Especially in this common pattern:

try {
  mayThrow();
} catch (e) {
  // Don&apos;t interrupt execution flow, but do report error.
  setTimeout(function() {
    throw e;
  });
}

Java solves this my making stack traces a linked list of places an exception haa been thrown. Since this is not available in JS, one must choose, and choosing the original location is better in the vast majority of cases.

Here is a full repro case:
http://output.jsbin.com/lufeqe/quiet

Compare the console output between V8 and JavaScriptCore. In V8 it is immediately obvious what happened, while JSC provides no useful information for 2 of the 3 errors.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1274664</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2017-02-08 17:21:52 -0800</bug_when>
    <thetext>&lt;rdar://problem/30433531&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1325568</commentid>
    <comment_count>2</comment_count>
    <who name="Frédéric Wang Nélar">fred.wang</who>
    <bug_when>2017-07-05 08:11:57 -0700</bug_when>
    <thetext>Testing in Firefox, it seems that SpiderMonkey behaves the same as V8.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1325958</commentid>
    <comment_count>3</comment_count>
      <attachid>314702</attachid>
    <who name="Frédéric Wang Nélar">fred.wang</who>
    <bug_when>2017-07-06 00:26:18 -0700</bug_when>
    <thetext>Created attachment 314702
Testcase (console output)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1325959</commentid>
    <comment_count>4</comment_count>
      <attachid>314703</attachid>
    <who name="Frédéric Wang Nélar">fred.wang</who>
    <bug_when>2017-07-06 00:27:19 -0700</bug_when>
    <thetext>Created attachment 314703
Testcase (print stack trace)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1325960</commentid>
    <comment_count>5</comment_count>
      <attachid>314704</attachid>
    <who name="Frédéric Wang Nélar">fred.wang</who>
    <bug_when>2017-07-06 00:27:47 -0700</bug_when>
    <thetext>Created attachment 314704
Testcase (to run in JSC)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1325963</commentid>
    <comment_count>6</comment_count>
    <who name="Frédéric Wang Nélar">fred.wang</who>
    <bug_when>2017-07-06 00:38:07 -0700</bug_when>
    <thetext>The stack trace of attachment 314704 seems correct using JSC (TOT):

WebKitBuild/Release/bin/jsc -e &quot;`curl https://bug-167934-attachments.webkit.org/attachment.cgi?id=314704`&quot;
Stack of an exception:
throwError@[Command Line]:2:18
caller@[Command Line]:6:13
invokee@[Command Line]:10:9
root@[Command Line]:14:10
global code@[Command Line]:19:7

Stack of a rethrown exception:
throwError@[Command Line]:2:18
caller@[Command Line]:6:13
invokee@[Command Line]:10:9
root@[Command Line]:14:10
global code@[Command Line]:28:9

Similarly, opening attachment 314703 in MiniBrowser (TOT) the exception stack trace are correctly printed in the textarea.

Attachment 314702 is similar to the original test case (i.e. using separate script tags and checking the console) and indeed shows that the stack incorrect for the rethrown exception. Again, using TOT.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1325964</commentid>
    <comment_count>7</comment_count>
      <attachid>314706</attachid>
    <who name="Frédéric Wang Nélar">fred.wang</who>
    <bug_when>2017-07-06 00:42:11 -0700</bug_when>
    <thetext>Created attachment 314706
Testcase (console output, only the rethrown error)

This is a simplified version of attachment 314702 with only the rethrown exception and using only one &lt;script&gt; tag. It still exhibits the issue in MiniBrowser.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1327330</commentid>
    <comment_count>8</comment_count>
    <who name="Caitlin Potter (:caitp)">caitp</who>
    <bug_when>2017-07-10 19:39:09 -0700</bug_when>
    <thetext>Quick design for a fix:

1. In `JSC::Exception::create(VM&amp;, JSValue);`:
If thrownValue inherits ErrorInstance, and does not have a captured stacktrace already, store the captured stacktrace on it (private symbol, or member of ErrorInstance).

2. In createScriptCallStackFromException() (ScriptCallStackFactory.cpp):
If exception-&gt;value() is an instance of ErrorInstance, load the stacktrace symbol,
and use it to assemble the ScriptCallStack

Console API adds the message like usual, and it should include the originally thrown stacktrace (provided the thrown object inherits(ErrorInstance::class())).

---

This seems to more or less match the behaviour of Chromium, so although there are caveats, I don&apos;t know that anyone has found that behaviour problematic in Chromium yet.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1327671</commentid>
    <comment_count>9</comment_count>
    <who name="Caitlin Potter (:caitp)">caitp</who>
    <bug_when>2017-07-11 13:10:24 -0700</bug_when>
    <thetext>Prototype patch in bug 174380</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1327929</commentid>
    <comment_count>10</comment_count>
    <who name="Frédéric Wang Nélar">fred.wang</who>
    <bug_when>2017-07-11 23:41:10 -0700</bug_when>
    <thetext>(In reply to Frédéric Wang (:fredw) from comment #6)
&gt; The stack trace of attachment 314704 [details] seems correct using JSC (TOT):
&gt; ...
&gt; Similarly, opening attachment 314703 [details] in MiniBrowser (TOT) the
&gt; exception stack trace are correctly printed in the textarea.

Just to follow-up here. These test cases print Error.stack which is indeed the one requested in this bug report. Hence they can not exhibit the issue, contrary to the test cases requiring the use of the Web Inspector.

It turns out that the Web Inspector prints the Exception object&apos;s stack (and does it on purpose), which indicates the location of when that exception was last (re)thrown and hence does not correspond to the behavior Chromium or Firefox&apos;s console.

For details and ideas see the discussion on bug 174380.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1327930</commentid>
    <comment_count>11</comment_count>
      <attachid>314703</attachid>
    <who name="Frédéric Wang Nélar">fred.wang</who>
    <bug_when>2017-07-11 23:42:06 -0700</bug_when>
    <thetext>Comment on attachment 314703
Testcase (print stack trace)

This testcase won&apos;t allow to reproduce the issue per comment 10.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1327931</commentid>
    <comment_count>12</comment_count>
      <attachid>314704</attachid>
    <who name="Frédéric Wang Nélar">fred.wang</who>
    <bug_when>2017-07-11 23:42:18 -0700</bug_when>
    <thetext>Comment on attachment 314704
Testcase (to run in JSC)

This testcase won&apos;t allow to reproduce the issue per comment 10.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1349178</commentid>
    <comment_count>13</comment_count>
    <who name="Saam Barati">saam</who>
    <bug_when>2017-09-14 21:54:03 -0700</bug_when>
    <thetext>Looking into this now</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1349185</commentid>
    <comment_count>14</comment_count>
    <who name="Saam Barati">saam</who>
    <bug_when>2017-09-14 22:38:23 -0700</bug_when>
    <thetext>It seems like all action is in https://bugs.webkit.org/show_bug.cgi?id=174380

Do we need two bugs for the same thing?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1349186</commentid>
    <comment_count>15</comment_count>
    <who name="Saam Barati">saam</who>
    <bug_when>2017-09-14 22:39:48 -0700</bug_when>
    <thetext>

*** This bug has been marked as a duplicate of bug 174380 ***</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>314702</attachid>
            <date>2017-07-06 00:26:18 -0700</date>
            <delta_ts>2017-07-06 00:26:18 -0700</delta_ts>
            <desc>Testcase (console output)</desc>
            <filename>167934-console.html</filename>
            <type>text/html</type>
            <size>430</size>
            <attacher name="Frédéric Wang Nélar">fred.wang</attacher>
            
              <data encoding="base64">PCFET0NUWVBFIGh0bWw+CjxoMT5PcGVuIGNvbnNvbGUgYW5kIGNoZWNrIHRoZSBzdGFjayB0cmFj
ZXMuIEFsbCBzaG91bGQgcG9pbnQgdG8gdGhlIHRocm93RXJyb3IgZnVuY3Rpb24gb2YgaGF2aW5n
IHRocm93biBhbiBlcnJvcjwvaDE+CjxzY3JpcHQ+CmZ1bmN0aW9uIHRocm93RXJyb3IoKSB7CiAg
dGhyb3cgbmV3IEVycm9yKCdJcyBteSBzdGFjayB0cmFjZSBoZWxwZnVsPycpOwp9CgpmdW5jdGlv
biBjYWxsZXIoKSB7CiAgdGhyb3dFcnJvcigpOwp9CgpmdW5jdGlvbiBpbnZva2VlKCkgewogIGNh
bGxlcigpOwp9CgpmdW5jdGlvbiByb290KCkgewogIGludm9rZWUoKTsKfQo8L3NjcmlwdD4KPHNj
cmlwdD4KICByb290KCk7Cjwvc2NyaXB0Pgo8c2NyaXB0Pgp0cnkgewogIHJvb3QoKTsKfSBjYXRj
aCAoZSkgewogIHRocm93IGU7Cn0KPC9zY3JpcHQ+Cg==
</data>

          </attachment>
          <attachment
              isobsolete="1"
              ispatch="0"
              isprivate="0"
          >
            <attachid>314703</attachid>
            <date>2017-07-06 00:27:19 -0700</date>
            <delta_ts>2017-07-11 23:42:06 -0700</delta_ts>
            <desc>Testcase (print stack trace)</desc>
            <filename>167934.html</filename>
            <type>text/html</type>
            <size>588</size>
            <attacher name="Frédéric Wang Nélar">fred.wang</attacher>
            
              <data encoding="base64">PCFET0NUWVBFIGh0bWw+Cjx0ZXh0YXJlYSBjb2xzPSI4MCIgcm93cz0iMjAiPjwvdGV4dGFyZWE+
CjxzY3JpcHQ+CmZ1bmN0aW9uIHRocm93RXJyb3IoKSB7CiAgdGhyb3cgbmV3IEVycm9yKCdJcyBt
eSBzdGFjayB0cmFjZSBoZWxwZnVsPycpOwp9CgpmdW5jdGlvbiBjYWxsZXIoKSB7CiAgdGhyb3dF
cnJvcigpOwp9CgpmdW5jdGlvbiBpbnZva2VlKCkgewogIGNhbGxlcigpOwp9CgpmdW5jdGlvbiBy
b290KCkgewogIGludm9rZWUoKTsKfQoKZnVuY3Rpb24gcHJpbnQodGV4dCkgewogIGRvY3VtZW50
LmdldEVsZW1lbnRzQnlUYWdOYW1lKCd0ZXh0YXJlYScpWzBdLnZhbHVlICs9ICh0ZXh0IHx8ICcn
KSArICdcbic7Cn0KCnByaW50KCdTdGFjayBvZiBhbiBleGNlcHRpb246JykKdHJ5IHsKICByb290
KCk7Cn0gY2F0Y2goZSkgewogIHByaW50KGUuc3RhY2spOwp9CnByaW50KCkKCnByaW50KCdTdGFj
ayBvZiBhIHJldGhyb3duIGV4Y2VwdGlvbjonKQp0cnkgewogIHRyeSB7CiAgICByb290KCk7CiAg
fSBjYXRjaCAoZSkgewogICAgdGhyb3cgZTsKICB9Cn0gY2F0Y2ggKGUpIHsKICBwcmludChlLnN0
YWNrKTsKfQo8L3NjcmlwdD4K
</data>

          </attachment>
          <attachment
              isobsolete="1"
              ispatch="0"
              isprivate="0"
          >
            <attachid>314704</attachid>
            <date>2017-07-06 00:27:47 -0700</date>
            <delta_ts>2017-07-11 23:42:18 -0700</delta_ts>
            <desc>Testcase (to run in JSC)</desc>
            <filename>167934.js</filename>
            <type>application/javascript</type>
            <size>408</size>
            <attacher name="Frédéric Wang Nélar">fred.wang</attacher>
            
              <data encoding="base64">ZnVuY3Rpb24gdGhyb3dFcnJvcigpIHsKICB0aHJvdyBuZXcgRXJyb3IoJ0lzIG15IHN0YWNrIHRy
YWNlIGhlbHBmdWw/Jyk7Cn0KCmZ1bmN0aW9uIGNhbGxlcigpIHsKICB0aHJvd0Vycm9yKCk7Cn0K
CmZ1bmN0aW9uIGludm9rZWUoKSB7CiAgY2FsbGVyKCk7Cn0KCmZ1bmN0aW9uIHJvb3QoKSB7CiAg
aW52b2tlZSgpOwp9CgpwcmludCgnU3RhY2sgb2YgYW4gZXhjZXB0aW9uOicpCnRyeSB7CiAgcm9v
dCgpOwp9IGNhdGNoKGUpIHsKICBwcmludChlLnN0YWNrKTsKfQpwcmludCgpCgpwcmludCgnU3Rh
Y2sgb2YgYSByZXRocm93biBleGNlcHRpb246JykKdHJ5IHsKICB0cnkgewogICAgcm9vdCgpOwog
IH0gY2F0Y2ggKGUpIHsKICAgIHRocm93IGU7CiAgfQp9IGNhdGNoIChlKSB7CiAgcHJpbnQoZS5z
dGFjayk7Cn0K
</data>

          </attachment>
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>314706</attachid>
            <date>2017-07-06 00:42:11 -0700</date>
            <delta_ts>2017-07-06 00:42:11 -0700</delta_ts>
            <desc>Testcase (console output, only the rethrown error)</desc>
            <filename>167934-console-rethrown.html</filename>
            <type>text/html</type>
            <size>312</size>
            <attacher name="Frédéric Wang Nélar">fred.wang</attacher>
            
              <data encoding="base64">PCFET0NUWVBFIGh0bWw+CjxoMT5PcGVuIGNvbnNvbGUgYW5kIGNoZWNrIHRoZSBzdGFjayB0cmFj
ZS48L2gxPgo8c2NyaXB0PgpmdW5jdGlvbiB0aHJvd0Vycm9yKCkgewogIHRocm93IG5ldyBFcnJv
cignSXMgbXkgc3RhY2sgdHJhY2UgaGVscGZ1bD8nKTsKfQoKZnVuY3Rpb24gY2FsbGVyKCkgewog
IHRocm93RXJyb3IoKTsKfQoKZnVuY3Rpb24gaW52b2tlZSgpIHsKICBjYWxsZXIoKTsKfQoKZnVu
Y3Rpb24gcm9vdCgpIHsKICBpbnZva2VlKCk7Cn0KCnRyeSB7CiAgcm9vdCgpOwp9IGNhdGNoIChl
KSB7CiAgdGhyb3cgZTsKfQo8L3NjcmlwdD4K
</data>

          </attachment>
      

    </bug>

</bugzilla>