From 62cc416a09554df9dc2df3c1f0256959ccf263c9 Mon Sep 17 00:00:00 2001 From: nd0ut Date: Thu, 21 Dec 2023 16:34:01 +0800 Subject: [PATCH] feat(upload-client): export `UploadcareNetworkError` was renamed to the `NetworkError` `UploadcareNetworkError` is deprecated but still available. `error.name` is also changed from `UploadcareNetworkError` to `NetworkError` --- packages/api-client-utils/src/NetworkError.test.ts | 12 ++++++++++++ .../{UploadcareNetworkError.ts => NetworkError.ts} | 6 +++--- .../src/UploadcareNetworkError.test.ts | 12 ------------ packages/api-client-utils/src/index.ts | 2 +- packages/upload-client/src/index.ts | 6 +++++- .../upload-client/src/request/request.browser.ts | 4 ++-- packages/upload-client/src/tools/retryIfFailed.ts | 6 +++--- .../upload-client/test/tools/retryIfFailed.test.ts | 8 ++++---- 8 files changed, 30 insertions(+), 26 deletions(-) create mode 100644 packages/api-client-utils/src/NetworkError.test.ts rename packages/api-client-utils/src/{UploadcareNetworkError.ts => NetworkError.ts} (54%) delete mode 100644 packages/api-client-utils/src/UploadcareNetworkError.test.ts diff --git a/packages/api-client-utils/src/NetworkError.test.ts b/packages/api-client-utils/src/NetworkError.test.ts new file mode 100644 index 000000000..86c631422 --- /dev/null +++ b/packages/api-client-utils/src/NetworkError.test.ts @@ -0,0 +1,12 @@ +import { NetworkError } from './NetworkError' + +describe('NetworkError', () => { + it('should work', () => { + const progressEvent = new Event('ProgressEvent') as ProgressEvent + const error = new NetworkError(progressEvent) + expect(error.name).toBe('NetworkError') + expect(error.message).toBe('Network error') + expect(error instanceof NetworkError).toBeTruthy() + expect(error.originalProgressEvent).toBe(progressEvent) + }) +}) diff --git a/packages/api-client-utils/src/UploadcareNetworkError.ts b/packages/api-client-utils/src/NetworkError.ts similarity index 54% rename from packages/api-client-utils/src/UploadcareNetworkError.ts rename to packages/api-client-utils/src/NetworkError.ts index 1ab3c3099..a13a66a0e 100644 --- a/packages/api-client-utils/src/UploadcareNetworkError.ts +++ b/packages/api-client-utils/src/NetworkError.ts @@ -1,12 +1,12 @@ -export class UploadcareNetworkError extends Error { +export class NetworkError extends Error { originalProgressEvent: ProgressEvent constructor(progressEvent: ProgressEvent) { super() - this.name = 'UploadcareNetworkError' + this.name = 'NetworkError' this.message = 'Network error' - Object.setPrototypeOf(this, UploadcareNetworkError.prototype) + Object.setPrototypeOf(this, NetworkError.prototype) this.originalProgressEvent = progressEvent } diff --git a/packages/api-client-utils/src/UploadcareNetworkError.test.ts b/packages/api-client-utils/src/UploadcareNetworkError.test.ts deleted file mode 100644 index 6e343ff38..000000000 --- a/packages/api-client-utils/src/UploadcareNetworkError.test.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { UploadcareNetworkError } from './UploadcareNetworkError' - -describe('UploadcareNetworkError', () => { - it('should work', () => { - const progressEvent = new Event('ProgressEvent') as ProgressEvent - const error = new UploadcareNetworkError(progressEvent) - expect(error.name).toBe('UploadcareNetworkError') - expect(error.message).toBe('Network error') - expect(error instanceof UploadcareNetworkError).toBeTruthy() - expect(error.originalProgressEvent).toBe(progressEvent) - }) -}) diff --git a/packages/api-client-utils/src/index.ts b/packages/api-client-utils/src/index.ts index 9615590fd..980f47090 100644 --- a/packages/api-client-utils/src/index.ts +++ b/packages/api-client-utils/src/index.ts @@ -10,7 +10,7 @@ export { export { isNode } from './isNode' export { isObject } from './isObject' export { retrier } from './retrier' -export { UploadcareNetworkError } from './UploadcareNetworkError' +export { NetworkError } from './NetworkError' export { ContentInfo } from './types/ContentInfo' export { ImageInfo } from './types/ImageInfo' export { Metadata } from './types/Metadata' diff --git a/packages/upload-client/src/index.ts b/packages/upload-client/src/index.ts index 94d41ad21..36b8ef00c 100644 --- a/packages/upload-client/src/index.ts +++ b/packages/upload-client/src/index.ts @@ -64,7 +64,7 @@ export { uploadFileGroup, type GroupFromOptions } from './uploadFileGroup' export { default as UploadClient } from './UploadClient' export { getUserAgent, - UploadcareNetworkError, + NetworkError, Metadata, ContentInfo, ImageInfo, @@ -79,6 +79,10 @@ export { } from '@uploadcare/api-client-utils' export { Queue } from './tools/Queue' +import { NetworkError } from '@uploadcare/api-client-utils' +/** @deprecated Please use NetworkError instead. */ +export const UploadcareNetworkError = NetworkError + /* Types */ export { Headers, ErrorRequestInfo } from './request/types' export { UploadcareFile } from './tools/UploadcareFile' diff --git a/packages/upload-client/src/request/request.browser.ts b/packages/upload-client/src/request/request.browser.ts index 7ee6d1252..3b58c0bee 100644 --- a/packages/upload-client/src/request/request.browser.ts +++ b/packages/upload-client/src/request/request.browser.ts @@ -1,6 +1,6 @@ import { RequestOptions, RequestResponse } from './types' import { - UploadcareNetworkError, + NetworkError, onCancel, CancelError } from '@uploadcare/api-client-utils' @@ -94,7 +94,7 @@ const request = ({ if (aborted) return // only triggers if the request couldn't be made at all - reject(new UploadcareNetworkError(progressEvent)) + reject(new NetworkError(progressEvent)) } if (onProgress && typeof onProgress === 'function') { diff --git a/packages/upload-client/src/tools/retryIfFailed.ts b/packages/upload-client/src/tools/retryIfFailed.ts index 9c52effa9..6c3fc41e1 100644 --- a/packages/upload-client/src/tools/retryIfFailed.ts +++ b/packages/upload-client/src/tools/retryIfFailed.ts @@ -1,5 +1,5 @@ import { UploadClientError } from './errors' -import { retrier, UploadcareNetworkError } from '@uploadcare/api-client-utils' +import { retrier, NetworkError } from '@uploadcare/api-client-utils' const REQUEST_WAS_THROTTLED_CODE = 'RequestThrottledError' const DEFAULT_RETRY_AFTER_TIMEOUT = 15000 @@ -28,7 +28,7 @@ export function retryIfFailed( ): Promise { const { retryThrottledRequestMaxTimes, retryNetworkErrorMaxTimes } = options return retrier(({ attempt, retry }) => - fn().catch((error: Error | UploadClientError | UploadcareNetworkError) => { + fn().catch((error: Error | UploadClientError | NetworkError) => { if ( 'response' in error && error?.code === REQUEST_WAS_THROTTLED_CODE && @@ -38,7 +38,7 @@ export function retryIfFailed( } if ( - error instanceof UploadcareNetworkError && + error instanceof NetworkError && attempt < retryNetworkErrorMaxTimes ) { return retry((attempt + 1) * DEFAULT_NETWORK_ERROR_TIMEOUT) diff --git a/packages/upload-client/test/tools/retryIfFailed.test.ts b/packages/upload-client/test/tools/retryIfFailed.test.ts index 23f8a0936..8e45d32a7 100644 --- a/packages/upload-client/test/tools/retryIfFailed.test.ts +++ b/packages/upload-client/test/tools/retryIfFailed.test.ts @@ -1,7 +1,7 @@ import { retryIfFailed } from '../../src/tools/retryIfFailed' import { UploadClientError } from '../../src/tools/errors' import { jest, expect } from '@jest/globals' -import { UploadcareNetworkError } from '@uploadcare/api-client-utils' +import { NetworkError } from '@uploadcare/api-client-utils' const createRunner = ({ attempts = 10, @@ -45,7 +45,7 @@ const throttledError = new UploadClientError( { 'retry-after': '1' } ) -const networkError = new UploadcareNetworkError( +const networkError = new NetworkError( new Event('ProgressEvent') as ProgressEvent ) @@ -143,7 +143,7 @@ describe('retryIfFailed', () => { expect(spy).toHaveBeenCalledTimes(1) }) - it('should be rejected with UploadcareNetworkError if MaxTimes = 0', async () => { + it('should be rejected with NetworkError if MaxTimes = 0', async () => { const { spy, task } = createRunner({ error: networkError }) await expect( @@ -151,7 +151,7 @@ describe('retryIfFailed', () => { retryNetworkErrorMaxTimes: 0, retryThrottledRequestMaxTimes: 0 }) - ).rejects.toThrowError(UploadcareNetworkError) + ).rejects.toThrowError(NetworkError) expect(spy).toHaveBeenCalledTimes(1) })