npm / npm-registry-client

http://npm.im/npm-registry-client
ISC License
264 stars 108 forks source link

Gracefully handle varnish errors / 503 status codes #168

Open daniel-j-h opened 6 years ago

daniel-j-h commented 6 years ago

Hey there,

seems like the npm registry was a bit unstable today: I hit several 503 / guru mediation varnish errors resulting in responses containing HTML and not JSON.

Seems like we try to parse JSON here and a couple of lines further down check for status code >= 400.

Can we handle this error more gracefully, hide the varnish error HTML response and make it clear to the user what's going on here?

Please find relevant log lines attached below.

16 http request GET https://registry.npmjs.org/MYPKG
17 http 503 https://registry.npmjs.org/MYPKG
18 verbose bad json <?xml version="1.0" encoding="utf-8"?>
18 verbose bad json <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
18 verbose bad json  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
18 verbose bad json <html>
18 verbose bad json   <head>
18 verbose bad json     <title>503 first byte timeout</title>
18 verbose bad json   </head>
18 verbose bad json   <body>
18 verbose bad json     <h1>Error 503 first byte timeout</h1>
18 verbose bad json     <p>first byte timeout</p>
18 verbose bad json     <h3>Guru Mediation:</h3>
18 verbose bad json     <p>Details: cache-ams4125-AMS 1515077081 2517622118</p>
18 verbose bad json     <hr>
18 verbose bad json     <p>Varnish cache server</p>
18 verbose bad json   </body>
18 verbose bad json </html>
19 error registry error parsing json
20 verbose headers { server: 'Varnish',
20 verbose headers   'retry-after': '0',
20 verbose headers   'content-type': 'text/html; charset=utf-8',
20 verbose headers   'content-length': '448',
20 verbose headers   'accept-ranges': 'bytes',
20 verbose headers   date: 'Thu, 04 Jan 2018 14:44:40 GMT',
20 verbose headers   via: '1.1 varnish',
20 verbose headers   connection: 'close',
20 verbose headers   'x-served-by': 'cache-ams4125-AMS',
20 verbose headers   'x-cache': 'MISS',
20 verbose headers   'x-cache-hits': '0',
20 verbose headers   'x-timer': 'S1515077050.478090,VS0,VE30049' }
21 info retry will retry, error on last attempt: SyntaxError: Unexpected token <
37 verbose stack     at Object.parse (native)
37 verbose stack     at RegClient.<anonymous> (/home/daniel/.nvm/versions/node/v4.8.6/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:243:23)
37 verbose stack     at Request._callback (/home/daniel/.nvm/versions/node/v4.8.6/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:216:14)
37 verbose stack     at Request.self.callback (/home/daniel/.nvm/versions/node/v4.8.6/lib/node_modules/npm/node_modules/request/request.js:186:22)
37 verbose stack     at emitTwo (events.js:87:13)
37 verbose stack     at Request.emit (events.js:172:7)
37 verbose stack     at Request.<anonymous> (/home/daniel/.nvm/versions/node/v4.8.6/lib/node_modules/npm/node_modules/request/request.js:1163:10)
37 verbose stack     at emitOne (events.js:77:13)
37 verbose stack     at Request.emit (events.js:169:7)
37 verbose stack     at IncomingMessage.<anonymous> (/home/daniel/.nvm/versions/node/v4.8.6/lib/node_modules/npm/node_modules/request/request.js:1085:12)
37 verbose stack     at IncomingMessage.g (events.js:260:16)
37 verbose stack     at emitNone (events.js:72:20)
37 verbose stack     at IncomingMessage.emit (events.js:166:7)
37 verbose stack     at endReadableNT (_stream_readable.js:923:12)
37 verbose stack     at nextTickCallbackWith2Args (node.js:511:9)
37 verbose stack     at process._tickCallback (node.js:425:17)