diff --git a/index.js b/index.js index 146422f..60b2118 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,7 @@ async function connectRetry(opts, retryCount = 0) { const response = await ngrokClient.startTunnel(opts); return response.public_url; } catch (err) { - if (!isRetriable(err) || retryCount >= 100) { + if (!isRetriable(err) || retryCount >= 20) { throw err; } await new Promise((resolve) => setTimeout(resolve, 200)); diff --git a/src/client.js b/src/client.js index d657d03..283ca0d 100644 --- a/src/client.js +++ b/src/client.js @@ -28,20 +28,12 @@ class NgrokClient { } } catch (error) { let clientError; - try { - const response = JSON.parse(error.response.body); - clientError = new NgrokClientError( - response.msg, - error.response, + const response = error.response ? error.response.body ? JSON.parse(error.response.body) : error.response : error; + clientError = new NgrokClientError( + error.msg, + error.details, response - ); - } catch (e) { - clientError = new NgrokClientError( - error.response.body, - error.response, - error.response.body - ); - } + ); throw clientError; } } @@ -51,9 +43,9 @@ class NgrokClient { return await this.internalApi[method](path, { json: options }).then( (response) => response.statusCode === 204 ); - } catch (error) { - const response = JSON.parse(error.response.body); - throw new NgrokClientError(response.msg, error.response, response); + } catch (e) { + const response = e.response ? e.response.body ? JSON.parse(e.response.body) : e.response : e + throw new NgrokClientError(e.msg, e.response, response); } } diff --git a/src/utils.js b/src/utils.js index 1c2288a..a954881 100644 --- a/src/utils.js +++ b/src/utils.js @@ -33,10 +33,7 @@ function validate(opts) { } function isRetriable(err) { - if (!err.response) { - return false; - } - const statusCode = err.response.statusCode; + const statusCode = err.response ? err.response.statusCode : err.body? err.body.status_code : 0; const body = err.body; const notReady500 = statusCode === 500 && /panic/.test(body); const notReady502 = @@ -47,7 +44,7 @@ function isRetriable(err) { statusCode === 503 && body.details && body.details.err === - "a successful ngrok tunnel session has not yet been established"; + "a successful ngrok tunnel session has not yet been established"; return notReady500 || notReady502 || notReady503; }