Include status message (reason phrase) in error message
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
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]