diff --git a/src/browser.js b/src/browser.js index 063ceb6..f2b7529 100644 --- a/src/browser.js +++ b/src/browser.js @@ -20,6 +20,7 @@ class MicrolinkError extends Error { const got = async (url, opts) => { try { + if (opts.retry > 0) opts.retry = opts.retry + 1 if (opts.timeout === undefined) opts.timeout = false const response = await ky(url, opts) const body = await response.json() diff --git a/src/factory.js b/src/factory.js index 2ca07bc..6898926 100644 --- a/src/factory.js +++ b/src/factory.js @@ -52,7 +52,7 @@ const factory = ({ VERSION, MicrolinkError, urlHttp, got, flatten }) => { }, {}) } - const fetchFromApi = async (apiUrl, opts = {}, retryCount = 0) => { + const fetchFromApi = async (apiUrl, opts = {}) => { try { const response = await got(apiUrl, opts) return opts.responseType === 'buffer' @@ -73,10 +73,6 @@ const factory = ({ VERSION, MicrolinkError, urlHttp, got, flatten }) => { ? rawBody : parseBody(isBodyBuffer ? rawBody.toString() : rawBody, err, uri) - if (body.code === 'EFATALCLIENT' && retryCount++ < 2) { - return fetchFromApi(apiUrl, opts, retryCount) - } - throw new MicrolinkError({ ...body, message: body.message, diff --git a/test/error/client.js b/test/error/client.js index 8975a2a..cd71f6f 100644 --- a/test/error/client.js +++ b/test/error/client.js @@ -19,11 +19,20 @@ clients.forEach(({ constructor: mql, target }) => { }) test(`${target} ยป EFATALCLIENT`, async t => { + let count = 0 + + const hooks = { + beforeRetry: [ + () => ++count + ] + } + const error = await t.throwsAsync( - mql('https://example.com', { endpoint: 'https://notexist.dev' }), + mql('https://example.com', { endpoint: 'https://notexist.dev', retry: 3 }, { hooks }), { instanceOf: mql.MicrolinkError } ) + t.is(count, 3) t.true(error.url === 'https://notexist.dev?url=https%3A%2F%2Fexample.com') t.true(error.code === 'EFATALCLIENT') t.true(error.status === 'error')