Skip to content

Commit

Permalink
feat(upload-client): export UploadcareNetworkError was renamed to t…
Browse files Browse the repository at this point in the history
…he `NetworkError`

`UploadcareNetworkError` is deprecated but still available.

`error.name` is also changed from `UploadcareNetworkError` to `NetworkError`
  • Loading branch information
nd0ut committed Dec 21, 2023
1 parent 3ba1bc0 commit 62cc416
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 26 deletions.
12 changes: 12 additions & 0 deletions packages/api-client-utils/src/NetworkError.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
12 changes: 0 additions & 12 deletions packages/api-client-utils/src/UploadcareNetworkError.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/api-client-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 5 additions & 1 deletion packages/upload-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export { uploadFileGroup, type GroupFromOptions } from './uploadFileGroup'
export { default as UploadClient } from './UploadClient'
export {
getUserAgent,
UploadcareNetworkError,
NetworkError,
Metadata,
ContentInfo,
ImageInfo,
Expand All @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions packages/upload-client/src/request/request.browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RequestOptions, RequestResponse } from './types'
import {
UploadcareNetworkError,
NetworkError,
onCancel,
CancelError
} from '@uploadcare/api-client-utils'
Expand Down Expand Up @@ -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') {
Expand Down
6 changes: 3 additions & 3 deletions packages/upload-client/src/tools/retryIfFailed.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -28,7 +28,7 @@ export function retryIfFailed<T>(
): Promise<T> {
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 &&
Expand All @@ -38,7 +38,7 @@ export function retryIfFailed<T>(
}

if (
error instanceof UploadcareNetworkError &&
error instanceof NetworkError &&
attempt < retryNetworkErrorMaxTimes
) {
return retry((attempt + 1) * DEFAULT_NETWORK_ERROR_TIMEOUT)
Expand Down
8 changes: 4 additions & 4 deletions packages/upload-client/test/tools/retryIfFailed.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -45,7 +45,7 @@ const throttledError = new UploadClientError(
{ 'retry-after': '1' }
)

const networkError = new UploadcareNetworkError(
const networkError = new NetworkError(
new Event('ProgressEvent') as ProgressEvent
)

Expand Down Expand Up @@ -143,15 +143,15 @@ 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(
retryIfFailed<number>(task, {
retryNetworkErrorMaxTimes: 0,
retryThrottledRequestMaxTimes: 0
})
).rejects.toThrowError(UploadcareNetworkError)
).rejects.toThrowError(NetworkError)
expect(spy).toHaveBeenCalledTimes(1)
})

Expand Down

0 comments on commit 62cc416

Please sign in to comment.