diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index b7a56850a308c8..35218a12f371df 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -1109,6 +1109,11 @@ class Http2Session extends EventEmitter { // Destroy any pending and open streams const cancel = new errors.Error('ERR_HTTP2_STREAM_CANCEL'); + if (error) { + cancel.cause = error; + if (typeof error.message === 'string') + cancel.message += ` (caused by: ${error.message})`; + } state.pendingStreams.forEach((stream) => stream.destroy(cancel)); state.streams.forEach((stream) => stream.destroy(error)); diff --git a/test/parallel/test-http2-client-onconnect-errors.js b/test/parallel/test-http2-client-onconnect-errors.js index 901caf18d9177f..a75dc590c669a1 100644 --- a/test/parallel/test-http2-client-onconnect-errors.js +++ b/test/parallel/test-http2-client-onconnect-errors.js @@ -91,9 +91,14 @@ function runTest(test) { req.on('error', errorMustCall); } else { client.on('error', errorMustCall); - req.on('error', common.expectsError({ - code: 'ERR_HTTP2_STREAM_CANCEL' - })); + req.on('error', (err) => { + common.expectsError({ + code: 'ERR_HTTP2_STREAM_CANCEL' + })(err); + common.expectsError({ + code: 'ERR_HTTP2_ERROR' + })(err.cause); + }); } req.on('end', common.mustCall());