Skip to content

Include status message (reason phrase) in error message #9197

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

candrews
Copy link

@candrews candrews commented Jul 8, 2025

When an HTTP request fails, the status code and description is logged/output. That description currently is derived by mapping the status code to a predefined mapping of messages. This change uses the HTTP response status line reason phrase if it is available, falling back to the predefined mapping to a message if the server didn't provide one.

The reason phrase that the server responds with may have more information that will be helpful for the user. For example, Sonatype Nexus Firewall includes a message in the reason phrase indicating the requested package is blocked. Other software similarly returns useful information in the reason phrase.

See:

When an HTTP request fails, the status code and description is logged/output. That description currently is derived by mapping the status code to a predefined mapping of messages. This change uses the HTTP response status line reason phrase if it is available, falling back to the predefined mapping to a message if the server didn't provide one.

The reason phrase that the server responds with may have more information that will be helpful for the user. For example, Sonatype Nexus Firewall includes a message in the reason phrase indicating the requested package is blocked. Other software similarly returns useful information in the reason phrase.

See:
* https://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html
* https://nodejs.org/api/http.html#messagestatusmessage
@candrews
Copy link
Author

candrews commented Jul 8, 2025

This PR improves the error handling.

With yarn 1.22.22, the output is:

$ yarn install
yarn cache v1.22.22
warning package.json: No license field
success Cleared cache.
Done in 14.21s.
yarn install v1.22.22
warning package.json: No license field
warning No license field
[1/4] Resolving packages...
[2/4] Fetching packages...
info There appears to be trouble with your network connection. Retrying...
error Error: https://example.com/repository/npm-read-group/unpopular-js/-/unpopular-js-1.0.12.tgz: Request failed "403 Forbidden"
    at Request.<anonymous> (/usr/lib/node_modules_22/yarn/lib/util/request-manager.js:521:16)
    at Request.emit (node:events:518:28)
    at Request.onRequestResponse (/usr/lib/node_modules_22/yarn/node_modules/request/request.js:1062:10)
    at ClientRequest.emit (node:events:518:28)
    at HTTPParser.parserOnIncomingClient (node:_http_client:716:27)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17)
    at TLSSocket.socketOnData (node:_http_client:558:22)
    at TLSSocket.emit (node:events:518:28)
    at addChunk (node:internal/streams/readable:561:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
[1]

With this change, the output is:

$ yarn install
yarn cache v1.22.22
warning package.json: No license field
success Cleared cache.
Done in 14.21s.
yarn install v1.22.22
warning package.json: No license field
warning No license field
[1/4] Resolving packages...
[2/4] Fetching packages...
info There appears to be trouble with your network connection. Retrying...
error Error: https://example.com/repository/npm-read-group/unpopular-js/-/unpopular-js-1.0.12.tgz: Request failed "403 Use of requested item is not safe and blocked by the Nexus Firewall."
    at Request.<anonymous> (/usr/lib/node_modules_22/yarn/lib/util/request-manager.js:521:16)
    at Request.emit (node:events:518:28)
    at Request.onRequestResponse (/usr/lib/node_modules_22/yarn/node_modules/request/request.js:1062:10)
    at ClientRequest.emit (node:events:518:28)
    at HTTPParser.parserOnIncomingClient (node:_http_client:716:27)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17)
    at TLSSocket.socketOnData (node:_http_client:558:22)
    at TLSSocket.emit (node:events:518:28)
    at addChunk (node:internal/streams/readable:561:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
[1]

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

Successfully merging this pull request may close these issues.

1 participant