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

throwOnError does not work on HTTPParserError #3136

Closed
sandratatarevicova opened this issue Apr 19, 2024 · 4 comments
Closed

throwOnError does not work on HTTPParserError #3136

sandratatarevicova opened this issue Apr 19, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@sandratatarevicova
Copy link

Bug Description

throwOnError does not work on HTTPParserError.

Reproducible By

const { request } = require('undici');

process.on('uncaughtException', err => console.error('uncaught exception:', err));
process.on('unhandledRejection', err => console.error('unhandled rejection:', err));

request('http://www.rolbox.it/rk-kastelruth/webcam/web03.jpg', { throwOnError: true })
    .then(() => console.log('then called'))
    .catch(e => console.error('catch called:', e))
    .finally(() => console.log('finally called'));

output:

unhandled rejection: HTTPParserError: Response does not match the HTTP/1.1 protocol (Invalid character in chunk size)
    at Parser.execute (/private/tmp/undici-test/node_modules/undici/lib/dispatcher/client-h1.js:277:15)
    at Parser.readMore (/private/tmp/undici-test/node_modules/undici/lib/dispatcher/client-h1.js:218:12)
    at Socket.<anonymous> (/private/tmp/undici-test/node_modules/undici/lib/dispatcher/client-h1.js:669:14)
    at Socket.emit (node:events:518:28)
    at emitReadable_ (node:internal/streams/readable:832:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:81:21) {
  code: 'HPE_INVALID_CHUNK_SIZE',
  data: ' \r\n' +
    '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n' +
    '<HTML><HEAD>\n' +
    '<TITLE>404 Not Found</TITLE>\n' +
    '</HEAD><BODY>\n' +
    '<H1>Not Found</H1>\n' +
    'The requested URL /rk-kastelruth/webcam/web03.jpg was not found on this server.<P>\n' +
    '</BODY></HTML>\n' +
    '\r\n' +
    '0\r\n' +
    '\r\n'
}

Expected Behavior

I would expect the HTTPParserError to be caught in catch and not to get unhandled rejection.

Environment

Node v20.12.2

Additional context

The behavior with throwOnError: false is also weird.

const { request } = require('undici');

process.on('uncaughtException', err => console.error('uncaught exception:', err));
process.on('unhandledRejection', err => console.error('unhandled rejection:', err));

request('http://www.rolbox.it/rk-kastelruth/webcam/web03.jpg', { throwOnError: false })
    .then(() => console.log('then called'))
    .catch(e => console.error('catch called:', e))
    .finally(() => console.log('finally called'));

output:

then called
finally called
uncaught exception: HTTPParserError: Response does not match the HTTP/1.1 protocol (Invalid character in chunk size)
    at Parser.execute (/private/tmp/undici-test/node_modules/undici/lib/dispatcher/client-h1.js:277:15)
    at Parser.readMore (/private/tmp/undici-test/node_modules/undici/lib/dispatcher/client-h1.js:218:12)
    at Socket.<anonymous> (/private/tmp/undici-test/node_modules/undici/lib/dispatcher/client-h1.js:669:14)
    at Socket.emit (node:events:518:28)
    at emitReadable_ (node:internal/streams/readable:832:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:81:21) {
  code: 'HPE_INVALID_CHUNK_SIZE',
  data: ' \r\n' +
    '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n' +
    '<HTML><HEAD>\n' +
    '<TITLE>404 Not Found</TITLE>\n' +
    '</HEAD><BODY>\n' +
    '<H1>Not Found</H1>\n' +
    'The requested URL /rk-kastelruth/webcam/web03.jpg was not found on this server.<P>\n' +
    '</BODY></HTML>\n' +
    '\r\n' +
    '0\r\n' +
    '\r\n'
}
@sandratatarevicova sandratatarevicova added the bug Something isn't working label Apr 19, 2024
@ronag
Copy link
Member

ronag commented Apr 19, 2024

Throw on error is not related to this.

throwOnError boolean (optional) - Default: false - Whether Undici should throw an error upon receiving a 4xx or 5xx response from the server.

Here is a correct version of your code that does not throw uncaught:

request('http://www.rolbox.it/rk-kastelruth/webcam/web03.jpg', { throwOnError: false })
    .then(({ body }) => body.on('error', () => console.error("body error, err")))
    .catch(e => console.error('catch called:', e))
    .finally(() => console.log('finally called'));

@sandratatarevicova
Copy link
Author

throwOnError boolean (optional) - Default: false - Whether Undici should throw an error upon receiving a 4xx or 5xx response from the server.

Based on this, I would expect an error to be thrown as the request ends with 404.

@sandratatarevicova
Copy link
Author

The stream function has the same behavior:

const fs = require('node:fs');
const { stream } = require('undici');

stream(
    'http://www.rolbox.it/rk-kastelruth/webcam/web03.jpg',
    {
        throwOnError: true,
        method: 'GET',
    },
    () => fs.createWriteStream('/tmp/test.jpg'),
)
    .then(() => console.log('then called'))
    .catch(e => console.error('catch called:', e))
    .finally(() => console.log('finally called'));

output:

node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

HTTPParserError: Response does not match the HTTP/1.1 protocol (Invalid character in chunk size)
    at Parser.execute (/private/tmp/undici-test/node_modules/undici/lib/dispatcher/client-h1.js:277:15)
    at Parser.readMore (/private/tmp/undici-test/node_modules/undici/lib/dispatcher/client-h1.js:218:12)
    at Socket.<anonymous> (/private/tmp/undici-test/node_modules/undici/lib/dispatcher/client-h1.js:669:14)
    at Socket.emit (node:events:518:28)
    at emitReadable_ (node:internal/streams/readable:832:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:81:21) {
  code: 'HPE_INVALID_CHUNK_SIZE',
  data: ' \r\n' +
    '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n' +
    '<HTML><HEAD>\n' +
    '<TITLE>404 Not Found</TITLE>\n' +
    '</HEAD><BODY>\n' +
    '<H1>Not Found</H1>\n' +
    'The requested URL /rk-kastelruth/webcam/web03.jpg was not found on this server.<P>\n' +
    '</BODY></HTML>\n' +
    '\r\n' +
    '0\r\n' +
    '\r\n'
}

@KhafraDev
Copy link
Member

I believe this was fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants