Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream postProcessResponse error is not catchable with .on('error') #6032

Open
Cerins opened this issue Mar 12, 2024 · 0 comments
Open

Stream postProcessResponse error is not catchable with .on('error') #6032

Cerins opened this issue Mar 12, 2024 · 0 comments

Comments

@Cerins
Copy link

Cerins commented Mar 12, 2024

Environment

Knex version: 3.1.0
Database + version: Microsoft SQL Server 2019 + tedious 18.1.0
OS: Linux Mint 21

@smorey2

Bug

Actual behaviour and the expected one

When using .stream() query and postProcessResponse, that throws an error, then it is impossible to catch the error with .on('error'. The expected behaviour is that .on('error' should also catch an error that happened in postProcessResponse.

Minimal code to cause the issue.

Specific data is not needed.

const DATABASE = ''; // Name of the database
const USERNAME = ''; // Username
const PASSWORD = ''; // Password
const HOST = ''; // Hostname

function main() {
    const knex = require('knex')({
        client: 'mssql',
        connection: {
            database: DATABASE,
            user: USERNAME,
            password: PASSWORD,
            server: HOST,
        },
        // A showcase what happens if postProcessResponse is a sync function which throws
        postProcessResponse: (result, queryContext) => {
            throw new Error('This is a test error');
        }
    });
    // Query does not matter since the issue is with postProcessResponse
    const stream = knex.raw('SELECT 1 as result').stream();
    stream.on('data', (row) => {
        console.log('row', row);
    });
    // Should handle the error thrown in postProcessResponse
    // But it does not
    stream.on('error', (err) => {
        console.error('error', err);
    });
    stream.on('close', () => {
        console.log('closed');
        knex.destroy();
    });
}
process.on('uncaughtException', (reason) => {
    // This should not fire since the error is caught in the stream
    // But it does
    console.error('uncaughtException', reason);
    process.exit(1);
});
main();

If process.on('uncaughtException' is removed from the minimal code example, then the node process crashes with output containing
throw er; // Unhandled 'error' event

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant