Giter Club home page Giter Club logo

Comments (4)

szmarczak avatar szmarczak commented on August 23, 2024

Simplest reproduction:

const {PassThrough} = require('stream');

const think = () => new Promise((resolve, reject) => {
	const p = new PassThrough();
    process.nextTick(() => p.destroy(new Error('no way')));
    resolve(p);
});

(async () => {
	const stream = await think();

	stream.once('error', () => {
		console.log('Caught stream error');
	});
})();

from http2-wrapper.

szmarczak avatar szmarczak commented on August 23, 2024

It is easily fixable for streams:

const {PassThrough} = require('stream');

const think = () => new Promise((resolve, reject) => {
	const p = new PassThrough();
    p.__destroy = p._destroy;
    p._destroy = async (...args) => {
        const callback = args.pop();
        
        const next = Promise.resolve();
        p.__destroy(...args, async error => {
            await next;
            callback(error);
        });
    };
    
    process.nextTick(() => p.destroy(new Error('no way')));
    resolve(p);
});

(async () => {
	const stream = await think();

	stream.once('error', () => {
		console.log('Caught stream error');
	});
})();

but unfortunately the Http2Session doesn't have a _destroy we don't need to fix agent.getSession() because errors are handled by the parent - an Agent instance.

from http2-wrapper.

szmarczak avatar szmarczak commented on August 23, 2024

This is also applicable to Http2Session:

const {connect} = require('http2');
const {TLSSocket} = require('tls');

const think = () => new Promise((resolve, reject) => {
	const session = connect('https://httpbin.org', {
        createConnection: (...args) => {
            const socket = new TLSSocket();
            process.nextTick(() => socket.destroy(new Error('oh no')));
            return socket;
        }
    });

    // session.once('error', () => {
    //     console.log('Error caught before await');
    // });

    resolve(session);
});

(async () => {
	const session = await think();

	session.once('error', () => {
		console.log('Caught session error');
	});
})();

from http2-wrapper.

szmarczak avatar szmarczak commented on August 23, 2024

An easy fix for Http2Session (we can reemit error because it's not a stream):

const {connect} = require('http2');
const {TLSSocket} = require('tls');

const think = () => new Promise(async (resolve, reject) => {
	const session = connect('https://httpbin.org', {
        createConnection: (...args) => {
            const socket = new TLSSocket();
            process.nextTick(() => socket.destroy(new Error('oh no')));
            return socket;
        }
    });

    const promise = Promise.resolve();
    const onError = async error => {
        await promise;

        session.emit('error', error);
    };

    session.once('error', onError);
    resolve(session);
    
    await promise;
    session.off('error', onError);
});

(async () => {
	const session = await think();

	session.once('error', () => {
		console.log('Caught session error');
	});
})();

from http2-wrapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.