title: Merging stack traces in rethrown errors
body: <p>I'm rethrowing here an error from Sequelize promise (Bluebird). In the first place, this was done to change error message, but as it appeared, this also produces more informative stack trace.</p>
  
  <p>It is something like</p>
  
  <pre><code>sequelize.sync().catch(originalError =&gt; {
    const rethrownError = new Error(originalError.msg + ': ' + originalError.sql);
    throw rethrownError;
  });
  </code></pre>
  
  <p>Where <code>originalError.stack</code> doesn't contain the line that caused the error but it holds important information that it originates in Sequelize and MySQL driver:</p>
  
  <pre><code>SequelizeDatabaseError: ER_KEY_COLUMN_DOES_NOT_EXITS: Key column 'NonExisting' doesn't exist in table
      at Query.formatError (...\node_modules\sequelize\lib\dialects\mysql\query.js:175:14)
      at Query._callback (...\node_modules\sequelize\lib\dialects\mysql\query.js:49:21)
      at Query.Sequence.end (...\node_modules\mysql\lib\protocol\sequences\Sequence.js:85:24)
      at Query.ErrorPacket (...\node_modules\mysql\lib\protocol\sequences\Query.js:94:8)
      at Protocol._parsePacket (...\node_modules\mysql\lib\protocol\Protocol.js:280:23)
      at Parser.write (...\node_modules\mysql\lib\protocol\Parser.js:74:12)
      at Protocol.write (...\node_modules\mysql\lib\protocol\Protocol.js:39:16)
      at Socket.&lt;anonymous&gt; (...\node_modules\mysql\lib\Connection.js:109:28)
      at emitOne (events.js:96:13)
      at Socket.emit (events.js:188:7)
      at readableAddChunk (_stream_readable.js:176:18)
      at Socket.Readable.push (_stream_readable.js:134:10)
      at TCP.onread (net.js:548:20)
  </code></pre>
  
  <p><code>rethrownError.stack</code> contains the point of interest (the first line in the stack) but everything else is a rubbish:</p>
  
  <pre><code>Error: ER_KEY_COLUMN_DOES_NOT_EXITS: Key column 'NonExisting' doesn't exist in table
      at sequelize.sync.catch (...\app.js:59:17)
      at tryCatcher (...\node_modules\bluebird\js\release\util.js:16:23)
      at Promise._settlePromiseFromHandler (...\node_modules\bluebird\js\release\promise.js:504:31)
      at Promise._settlePromise (...\node_modules\bluebird\js\release\promise.js:561:18)
      at Promise._settlePromise0 (...\node_modules\bluebird\js\release\promise.js:606:10)
      at Promise._settlePromises (...\node_modules\bluebird\js\release\promise.js:681:18)
      at Async._drainQueue (...\node_modules\bluebird\js\release\async.js:138:16)
      at Async._drainQueues (...\node_modules\bluebird\js\release\async.js:148:10)
      at Immediate.Async.drainQueues (...\node_modules\bluebird\js\release\async.js:17:14)
      at runCallback (timers.js:637:20)
      at tryOnImmediate (timers.js:610:5)
      at processImmediate [as _immediateCallback] (timers.js:582:5)
  </code></pre>
  
  <p>I would like to keep the information about both of them - and to designate the link between them, not just to add as two unrelated log entries.</p>
  
  <p>I've been thinking on logging them as a single error with concatenated stack, <code>rethrownError.stack += '\n' + originalError.stack</code>. </p>
  
  <p>How should these two errors be treated? Should their stack traces be joined? Is there a convention for merging error stacks in JavaScript (Node.js in particular)?</p>
  
  <p>The intention is to keep the resulting error meaningful and to not upset existing tools that parse error stack traces (namely Stacktrace.js).</p>
  
  <p>The projects under consideration use Winston logger or plain <code>console.error</code>, so the error is stringified at some point (in the example above it was logged via unhandled rejection handler).</p>