From 51286add0f343f49c183f7cfa2ab0b8b6f70dc34 Mon Sep 17 00:00:00 2001 From: nkzawa Date: Sun, 20 Mar 2016 02:01:46 +0900 Subject: [PATCH 1/2] no retry if client error --- lib/index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/index.js b/lib/index.js index a0425d7..7aa89cc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -87,10 +87,10 @@ export default class Now extends EventEmitter { }); if (this._debug) console.timeEnd('> [debug] /create'); - // no retry on 403 - if (403 === res.status) { + // no retry on 4xx + if (400 <= res.status && 500 > res.status) { if (this._debug) { - console.log('> [debug] bailing on creating due to 403'); + console.log('> [debug] bailing on creating due to %s', res.status); } return bail(responseError(res)); } @@ -140,9 +140,9 @@ export default class Now extends EventEmitter { }); if (this._debug) console.timeEnd(`> [debug] /sync ${name}`); - // no retry on 403 - if (403 === res.status) { - if (this._debug) console.log('> [debug] bailing on creating due to 403'); + // no retry on 4xx + if (400 <= res.status || 500 > res.status) { + if (this._debug) console.log('> [debug] bailing on creating due to %s', res.status); return bail(responseError(res)); } From d0ef4ef7fc659d8e938404614d9278ceeab82658 Mon Sep 17 00:00:00 2001 From: nkzawa Date: Sun, 20 Mar 2016 02:03:06 +0900 Subject: [PATCH 2/2] add messages for rate limit exceeded error --- bin/now | 8 ++++++++ lib/index.js | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/bin/now b/bin/now index eeeb411..4865503 100755 --- a/bin/now +++ b/bin/now @@ -137,6 +137,14 @@ async function sync (token) { function handleError (err) { if (403 === err.status) { error('Authentication error. Run `now -L` or `now --login` to log-in again.'); + } else if (429 === err.status) { + if (null != err.retryAfter) { + error('Rate limit exceeded error. Try again in ' + + ms(err.retryAfter * 1000, { long: true }) + + ', or upgrade your account: https://zeit.co/now#pricing'); + } else { + error('Rate limit exceeded error. Please try later.'); + } } else if (err.userError) { error(err.message); } else if (500 === err.status) { diff --git a/lib/index.js b/lib/index.js index 7aa89cc..4724636 100644 --- a/lib/index.js +++ b/lib/index.js @@ -195,5 +195,13 @@ function toRelative (path, base) { function responseError (res) { const err = new Error('Response error'); err.status = res.status; + + if (429 === res.status) { + const retryAfter = res.headers.get('Retry-After'); + if (retryAfter) { + err.retryAfter = parseInt(retryAfter, 10); + } + } + return err; }