diff --git a/packages/api-client-utils/src/CancelError.test.ts b/packages/api-client-utils/src/CancelError.test.ts index c73245470..f54b4e8f8 100644 --- a/packages/api-client-utils/src/CancelError.test.ts +++ b/packages/api-client-utils/src/CancelError.test.ts @@ -1,20 +1,25 @@ import { CancelError } from './CancelError' +import { UploadcareError } from './UploadcareError' describe('CancelError', () => { - it('should have "isCancel" property with "true" value', async () => { + it('should have "isCancel" property with "true" value', () => { const cancelError = new CancelError('Cancelled!') expect(cancelError.isCancel).toBe(true) }) - it('should have default messgage', async () => { + it('should have default messgage', () => { const cancelError = new CancelError() expect(cancelError.message).toBe('Request canceled') expect(cancelError.isCancel).toBe(true) }) - it('should be able to pass message', async () => { + it('should be able to pass message', () => { const cancelError = new CancelError('Message') expect(cancelError.message).toBe('Message') expect(cancelError.isCancel).toBe(true) }) + + it('should be instanceof UploadcareError', () => { + expect(new CancelError()).toBeInstanceOf(UploadcareError) + }) }) diff --git a/packages/api-client-utils/src/CancelError.ts b/packages/api-client-utils/src/CancelError.ts index 94dc3fbb6..906b96cd2 100644 --- a/packages/api-client-utils/src/CancelError.ts +++ b/packages/api-client-utils/src/CancelError.ts @@ -1,7 +1,12 @@ -export class CancelError extends Error { +import { UploadcareError } from './UploadcareError' + +export class CancelError extends UploadcareError { isCancel = true + constructor(message = 'Request canceled') { super(message) + + this.name = 'CancelError' Object.setPrototypeOf(this, CancelError.prototype) } } 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..7a9a60475 --- /dev/null +++ b/packages/api-client-utils/src/NetworkError.test.ts @@ -0,0 +1,19 @@ +import { NetworkError } from './NetworkError' +import { UploadcareError } from './UploadcareError' + +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) + }) + + it('should be instanceof UploadcareError', () => { + const progressEvent = new Event('ProgressEvent') as ProgressEvent + const error = new NetworkError(progressEvent) + expect(error).toBeInstanceOf(UploadcareError) + }) +}) diff --git a/packages/api-client-utils/src/NetworkError.ts b/packages/api-client-utils/src/NetworkError.ts new file mode 100644 index 000000000..cf884269e --- /dev/null +++ b/packages/api-client-utils/src/NetworkError.ts @@ -0,0 +1,15 @@ +import { UploadcareError } from './UploadcareError' + +export class NetworkError extends UploadcareError { + originalProgressEvent: ProgressEvent + + constructor(progressEvent: ProgressEvent) { + super() + + this.name = 'NetworkError' + this.message = 'Network error' + Object.setPrototypeOf(this, NetworkError.prototype) + + this.originalProgressEvent = progressEvent + } +} diff --git a/packages/api-client-utils/src/UploadcareError.test.ts b/packages/api-client-utils/src/UploadcareError.test.ts new file mode 100644 index 000000000..a49183f39 --- /dev/null +++ b/packages/api-client-utils/src/UploadcareError.test.ts @@ -0,0 +1,8 @@ +import { UploadcareError } from './UploadcareError' + +describe('UploadcareError', () => { + it('should be instanceof Error', async () => { + const uploadcareError = new UploadcareError('This is error!') + expect(uploadcareError instanceof Error).toBe(true) + }) +}) diff --git a/packages/api-client-utils/src/UploadcareError.ts b/packages/api-client-utils/src/UploadcareError.ts new file mode 100644 index 000000000..934e5e590 --- /dev/null +++ b/packages/api-client-utils/src/UploadcareError.ts @@ -0,0 +1 @@ +export class UploadcareError extends Error {} 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/UploadcareNetworkError.ts b/packages/api-client-utils/src/UploadcareNetworkError.ts deleted file mode 100644 index 1ab3c3099..000000000 --- a/packages/api-client-utils/src/UploadcareNetworkError.ts +++ /dev/null @@ -1,13 +0,0 @@ -export class UploadcareNetworkError extends Error { - originalProgressEvent: ProgressEvent - - constructor(progressEvent: ProgressEvent) { - super() - - this.name = 'UploadcareNetworkError' - this.message = 'Network error' - Object.setPrototypeOf(this, UploadcareNetworkError.prototype) - - this.originalProgressEvent = progressEvent - } -} diff --git a/packages/api-client-utils/src/index.ts b/packages/api-client-utils/src/index.ts index 9615590fd..c594d2ab0 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' @@ -19,3 +19,4 @@ export { StoreValue } from './types/StoreValue' export { onCancel } from './onCancel' export { CancelError } from './CancelError' export { poll } from './poll' +export { UploadcareError } from './UploadcareError' diff --git a/packages/rest-client/src/index.ts b/packages/rest-client/src/index.ts index e54093938..3c5f765f8 100644 --- a/packages/rest-client/src/index.ts +++ b/packages/rest-client/src/index.ts @@ -69,13 +69,15 @@ export { CustomUserAgent, CustomUserAgentFn, CustomUserAgentOptions, - GetUserAgentOptions + GetUserAgentOptions, + UploadcareError } from '@uploadcare/api-client-utils' /** Helpers from `@uploadcare/api-client-utils` */ export { getUserAgent } from '@uploadcare/api-client-utils' /** Tools */ +export { RestClientError } from './tools/RestClientError' export { paginate, Paginator } from './tools/paginate' export { addonJobPoller, AddonJobPollerOptions } from './tools/addonJobPoller' export { diff --git a/packages/rest-client/src/tools/RestClientError.test.ts b/packages/rest-client/src/tools/RestClientError.test.ts index ef5562ce0..234a284ee 100644 --- a/packages/rest-client/src/tools/RestClientError.test.ts +++ b/packages/rest-client/src/tools/RestClientError.test.ts @@ -1,5 +1,6 @@ import { RestClientError } from './RestClientError' import { Request, Response } from '../lib/fetch/fetch.node' +import { UploadcareError } from '@uploadcare/api-client-utils' describe('RestClientError', () => { it('should work', () => { @@ -65,4 +66,9 @@ describe('RestClientError', () => { expect(error.message).toBe('[200] OK') }) + + it('should be instanceof UploadcareError', () => { + const error = new RestClientError() + expect(error).toBeInstanceOf(UploadcareError) + }) }) diff --git a/packages/rest-client/src/tools/RestClientError.ts b/packages/rest-client/src/tools/RestClientError.ts index 5ba895bfa..51e5926f8 100644 --- a/packages/rest-client/src/tools/RestClientError.ts +++ b/packages/rest-client/src/tools/RestClientError.ts @@ -1,3 +1,5 @@ +import { UploadcareError } from '@uploadcare/api-client-utils' + export type RestClientErrorOptions = { request?: Request response?: Response @@ -9,7 +11,7 @@ const DEFAULT_MESSAGE = 'Unknown error' * TODO: it's better to split errors into something like Runtime error and * ServerError (RestApiError) */ -export class RestClientError extends Error { +export class RestClientError extends UploadcareError { readonly status?: number readonly statusText?: string diff --git a/packages/upload-client/src/api/base.ts b/packages/upload-client/src/api/base.ts index 84f23e6e5..d312d99ce 100644 --- a/packages/upload-client/src/api/base.ts +++ b/packages/upload-client/src/api/base.ts @@ -7,7 +7,7 @@ import { import { defaultSettings } from '../defaultSettings' import request from '../request/request.node' import buildFormData from '../tools/buildFormData' -import { UploadClientError } from '../tools/errors' +import { UploadError } from '../tools/UploadError' import getUrl from '../tools/getUrl' import { getUserAgent } from '../tools/getUserAgent' import { retryIfFailed } from '../tools/retryIfFailed' @@ -100,7 +100,7 @@ export default function base( }).then(({ data, headers, request }) => { const response = camelizeKeys(JSON.parse(data)) as Response if ('error' in response) { - throw new UploadClientError( + throw new UploadError( response.error.content, response.error.errorCode, request, diff --git a/packages/upload-client/src/api/fromUrl.ts b/packages/upload-client/src/api/fromUrl.ts index 6f1777d71..84b4585e5 100644 --- a/packages/upload-client/src/api/fromUrl.ts +++ b/packages/upload-client/src/api/fromUrl.ts @@ -12,7 +12,7 @@ import getUrl from '../tools/getUrl' import defaultSettings from '../defaultSettings' import { getUserAgent } from '../tools/getUserAgent' -import { UploadClientError } from '../tools/errors' +import { UploadError } from '../tools/UploadError' import { retryIfFailed } from '../tools/retryIfFailed' import { getStoreValue } from '../tools/getStoreValue' @@ -118,7 +118,7 @@ export default function fromUrl( const response = camelizeKeys(JSON.parse(data)) as Response if ('error' in response) { - throw new UploadClientError( + throw new UploadError( response.error.content, response.error.errorCode, request, diff --git a/packages/upload-client/src/api/fromUrlStatus.ts b/packages/upload-client/src/api/fromUrlStatus.ts index f1fe99a2a..92ad21493 100644 --- a/packages/upload-client/src/api/fromUrlStatus.ts +++ b/packages/upload-client/src/api/fromUrlStatus.ts @@ -7,8 +7,9 @@ import getUrl from '../tools/getUrl' import defaultSettings from '../defaultSettings' import { getUserAgent } from '../tools/getUserAgent' -import { UploadClientError } from '../tools/errors' +import { UploadError } from '../tools/UploadError' import { retryIfFailed } from '../tools/retryIfFailed' +import { ServerErrorCode } from '../tools/ServerErrorCode' export enum Status { Unknown = 'unknown', @@ -36,7 +37,7 @@ export type StatusProgressResponse = { export type StatusErrorResponse = { status: Status.Error error: string - errorCode: string + errorCode: ServerErrorCode } export type StatusSuccessResponse = { @@ -107,9 +108,9 @@ export default function fromUrlStatus( const response = camelizeKeys(JSON.parse(data)) as Response if ('error' in response && !isErrorResponse(response)) { - throw new UploadClientError( + throw new UploadError( response.error.content, - undefined, + response.error.errorCode, request, response, headers diff --git a/packages/upload-client/src/api/group.ts b/packages/upload-client/src/api/group.ts index 923360a5c..1209c4e7b 100644 --- a/packages/upload-client/src/api/group.ts +++ b/packages/upload-client/src/api/group.ts @@ -7,7 +7,7 @@ import getUrl from '../tools/getUrl' import defaultSettings from '../defaultSettings' import { getUserAgent } from '../tools/getUserAgent' -import { UploadClientError } from '../tools/errors' +import { UploadError } from '../tools/UploadError' import { retryIfFailed } from '../tools/retryIfFailed' export type GroupOptions = { @@ -68,7 +68,7 @@ export default function group( const response = camelizeKeys(JSON.parse(data)) as Response if ('error' in response) { - throw new UploadClientError( + throw new UploadError( response.error.content, response.error.errorCode, request, diff --git a/packages/upload-client/src/api/groupInfo.ts b/packages/upload-client/src/api/groupInfo.ts index 28947e1c2..55249d288 100644 --- a/packages/upload-client/src/api/groupInfo.ts +++ b/packages/upload-client/src/api/groupInfo.ts @@ -7,7 +7,7 @@ import getUrl from '../tools/getUrl' import defaultSettings from '../defaultSettings' import { getUserAgent } from '../tools/getUserAgent' -import { UploadClientError } from '../tools/errors' +import { UploadError } from '../tools/UploadError' import { retryIfFailed } from '../tools/retryIfFailed' export type GroupInfoOptions = { @@ -58,7 +58,7 @@ export default function groupInfo( const response = camelizeKeys(JSON.parse(data)) as Response if ('error' in response) { - throw new UploadClientError( + throw new UploadError( response.error.content, response.error.errorCode, request, diff --git a/packages/upload-client/src/api/info.ts b/packages/upload-client/src/api/info.ts index c870c9c58..f451646ad 100644 --- a/packages/upload-client/src/api/info.ts +++ b/packages/upload-client/src/api/info.ts @@ -3,7 +3,7 @@ import getUrl from '../tools/getUrl' import defaultSettings from '../defaultSettings' import { getUserAgent } from '../tools/getUserAgent' import { camelizeKeys, CustomUserAgent } from '@uploadcare/api-client-utils' -import { UploadClientError } from '../tools/errors' +import { UploadError } from '../tools/UploadError' import { retryIfFailed } from '../tools/retryIfFailed' /* Types */ @@ -59,7 +59,7 @@ export default function info( const response = camelizeKeys(JSON.parse(data)) as Response if ('error' in response) { - throw new UploadClientError( + throw new UploadError( response.error.content, response.error.errorCode, request, diff --git a/packages/upload-client/src/api/multipartComplete.ts b/packages/upload-client/src/api/multipartComplete.ts index 997dbca74..adf33331e 100644 --- a/packages/upload-client/src/api/multipartComplete.ts +++ b/packages/upload-client/src/api/multipartComplete.ts @@ -8,7 +8,7 @@ import getUrl from '../tools/getUrl' import defaultSettings from '../defaultSettings' import { getUserAgent } from '../tools/getUserAgent' import { retryIfFailed } from '../tools/retryIfFailed' -import { UploadClientError } from '../tools/errors' +import { UploadError } from '../tools/UploadError' export type MultipartCompleteOptions = { publicKey: string @@ -55,7 +55,7 @@ export default function multipartComplete( const response = camelizeKeys(JSON.parse(data)) as Response if ('error' in response) { - throw new UploadClientError( + throw new UploadError( response.error.content, response.error.errorCode, request, diff --git a/packages/upload-client/src/api/multipartStart.ts b/packages/upload-client/src/api/multipartStart.ts index 4db02eb34..7aa12376a 100644 --- a/packages/upload-client/src/api/multipartStart.ts +++ b/packages/upload-client/src/api/multipartStart.ts @@ -17,7 +17,7 @@ import { } from '../defaultSettings' import { getUserAgent } from '../tools/getUserAgent' import { retryIfFailed } from '../tools/retryIfFailed' -import { UploadClientError } from '../tools/errors' +import { UploadError } from '../tools/UploadError' import { getStoreValue } from '../tools/getStoreValue' export type MultipartStartOptions = { @@ -94,7 +94,7 @@ export default function multipartStart( const response = camelizeKeys(JSON.parse(data)) as Response if ('error' in response) { - throw new UploadClientError( + throw new UploadError( response.error.content, response.error.errorCode, request, diff --git a/packages/upload-client/src/index.ts b/packages/upload-client/src/index.ts index 94d41ad21..033633aee 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, @@ -75,15 +75,26 @@ export { CustomUserAgent, CustomUserAgentFn, CustomUserAgentOptions, - GetUserAgentOptions + GetUserAgentOptions, + CancelError, + UploadcareError } 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' export { UploadcareGroup } from './tools/UploadcareGroup' -export { UploadClientError, ErrorResponseInfo } from './tools/errors' +export { UploadError, ErrorResponseInfo } from './tools/UploadError' + +import { UploadError } from './tools/UploadError' +/** @deprecated Please use UploadError instead. */ +export const UploadClientError = UploadError + export { Settings, SupportedFileInput as SupportedFileInput } from './types' export { NodeFile as NodeFile, 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/request/types.ts b/packages/upload-client/src/request/types.ts index 32b129ae8..642c5023b 100644 --- a/packages/upload-client/src/request/types.ts +++ b/packages/upload-client/src/request/types.ts @@ -5,6 +5,7 @@ import { UnknownProgressInfo } from '../api/types' import { SupportedFileInput } from '../types' +import { ServerErrorCode } from '../tools/ServerErrorCode' export type Headers = { [key: string]: string | string[] | undefined @@ -39,6 +40,6 @@ export type FailedResponse = { error: { content: string statusCode: number - errorCode: string + errorCode: ServerErrorCode } } diff --git a/packages/upload-client/src/tools/ServerErrorCode.ts b/packages/upload-client/src/tools/ServerErrorCode.ts new file mode 100644 index 000000000..d72ab2e1e --- /dev/null +++ b/packages/upload-client/src/tools/ServerErrorCode.ts @@ -0,0 +1,97 @@ +/** @see https://uploadcare.com/api-refs/upload-api/#tag/Errors */ +export type ServerErrorCode = + // Base upload errors: + | 'AccountBlockedError' // 403 Account has been blocked. + | 'AccountLimitsExceededError' // 403 Account has reached its limits. + | 'AccountUnpaidError' // 403 Account has been blocked for non payment. + | 'AutostoreDisabledError' // 403 Autostore is disabled. + | 'BaseViewsError' // 400 Request processing failed. + | 'FileMetadataKeyDuplicatedError' // 400 File's metadata key `%s` has a duplicate. + | 'FileMetadataKeyEmptyError' // 400 File's metadata key can not be empty. + | 'FileMetadataKeyForbiddenError' // 400 File's metadata key `%s` contains symbols not allowed by the metadata key format. + | 'FileMetadataKeyLengthTooBigError' // 400 Length of file metadata key `%s` can not be more than %d symbols. + | 'FileMetadataKeysNumberTooBigError' // 400 A file can not have more than %d metadata keys. + | 'FileMetadataValueEmptyError' // 400 Value of the file metadata key `%s` can not be empty. + | 'FileMetadataValueForbiddenError' // 400 Value of file metadata key `%s` contains symbols not allowed by the metadata value format. + | 'FileMetadataValueLengthTooBigError' // 400 Value of file metadata's key `%s` can not be more than %d symbols in length. + | 'FileSizeLimitExceededError' // 400 File is too large. + | 'MethodNotAllowedError' // 405 HTTP method %s is not allowed for %s + | 'NullCharactersForbiddenError' // 400 Null characters are not allowed. + | 'PostRequestParserFailedError' // 400 HTTP POST request parsing failed. + | 'ProjectPublicKeyInvalidError' // 403 %s is invalid. + | 'ProjectPublicKeyRemovedError' // 403 Project %s is marked as removed. + | 'ProjectPublicKeyRequiredError' // 403 %s is required. + | 'RequestFileNumberLimitExceededError' // 400 The request contains too many files. + | 'RequestFiledsNumberLimitExceededError' // 400 The request contains too many HTTP POST fields. + | 'RequestSizeLimitExceededError' // 413 The size of the request is too large. + | 'RequestThrottledError' // 429 Request was throttled. + | 'SignatureExpirationError' // 403 Expired signature. + | 'SignatureExpirationInvalidError' // 400 `expire` must be a UNIX timestamp. + | 'SignatureExpirationRequiredError' // 400 `expire` is required. + | 'SignatureInvalidError' // 403 Invalid signature. + | 'SignatureRequiredError' // 400 `signature` is required. + | 'UploadAPIError' // 500 Internal error. + | 'UploadFailedError' // 403 Upload failed. + // FromURL upload errors: + | 'DownloadFileError' // 500 Failed to download the file. + | 'DownloadFileHTTPClientError' // 500 HTTP client error: %s. + | 'DownloadFileHTTPNetworkError' // 500 HTTP network error: %s. + | 'DownloadFileHTTPServerError' // 500 HTTP server error: %s. + | 'DownloadFileHTTPURLValidationError' // 500 HTTP URL validation error: %s. + | 'DownloadFileInternalServerError' // 500 Internal server error. + | 'DownloadFileNotFoundError' // 500 downloaded file not found. + | 'DownloadFileSizeLimitExceededError' // 500 Downloaded file is too big: %s > %s. + | 'DownloadFileTaskFailedError' // 500 download task failed. + | 'DownloadFileTimeLimitExceededError' // 500 Failed to download the file within the allotted time limit of %s seconds. + | 'DownloadFileValidationFailedError' // 500 File validation error: %s + // File upload errors: + | 'FileIdInvalidError' // 400 file_id is invalid. + | 'FileIdNotUniqueError' // 400 File id must be unique. + | 'FileIdRequiredError' // 400 file_id is required. + | 'FileNotFoundError' // 404 File is not found. + | 'FileRequiredError' // 400 There should be a file. + | 'FilesNumberLimitExceededError' // 400 There are too many files. + | 'FilesRequiredError' // 400 Request does not contain files. + | 'InternalRequestForbiddenError' // 403 Forbidden request. + | 'InternalRequestInvalidError' // 400 Incorrect request. + | 'MultipartFileAlreadyUploadedError' // 400 File is already uploaded. + | 'MultipartFileCompletionFailedError' // 400 Can not complete upload. Wrong parts size? + | 'MultipartFileIdRequiredError' // 400 uuid is required. + | 'MultipartFileNotFoundError' // 404 File is not found. + | 'MultipartFileSizeLimitExceededError' // 400 File size exceeds project limit. + | 'MultipartFileSizeTooSmallError' // 400 File size can not be less than %d bytes. Please use direct upload instead of multipart. + | 'MultipartPartSizeInvalidError' // 400 Multipart Upload Part Size should be an integer. + | 'MultipartPartSizeTooBigError' // 400 Multipart Upload Part Size can not be more than %d bytes. + | 'MultipartPartSizeTooSmallError' // 400 Multipart Upload Part Size can not be less than %d bytes. + | 'MultipartSizeInvalidError' // 400 size should be integer. + | 'MultipartUploadSizeTooLargeError' // 400 Uploaded size is more than expected. + | 'MultipartUploadSizeTooSmallError' // 400 File size mismatch. Not all parts uploaded? + | 'RequestParamRequiredError' // 400 %s is required. + | 'SourceURLRequiredError' // 400 source_url is required. + | 'TokenRequiredError' // 400 token is required. + | 'UUIDInvalidError' // 400 uuid is invalid. + | 'UploadViewsError' // 400 Upload request processing failed. + | 'UploadcareFileIdDuplicatedError' // 400 UPLOADCARE_FILE_ID is duplicated. You are probably a lottery winner. + | 'UploadcareFileIdInvalidError' // 400 UPLOADCARE_FILE_ID should be a valid UUID. + | 'UploadcareFileIdRequiredError' // 400 UPLOADCARE_FILE_ID is required. + // File group errors: + | 'GroupFileURLParsingFailedError' // 400 This is not valid file url: %s. + | 'GroupFilesInvalidError' // 400 No files[N] parameters found. + | 'GroupFilesNotFoundError' // 400 Some files not found. + | 'GroupIdRequiredError' // 400 group_id is required. + | 'GroupNotFoundError' // 404 group_id is invalid. + | 'GroupViewsError' // 400 Request to group processing failed. + // File content validation errors: + | 'FileInfectedError' // 400 The file is infected by %s virus. + | 'FileTypeForbiddenError' // 400 Uploading of these file types is not allowed. + // URL validation errors: + | 'HostnameNotFoundError' // 400 Host does not exist. + | 'URLBlacklistedError' // 400 Source is blacklisted. + | 'URLHostMalformedError' // 400 URL host is malformed. + | 'URLHostPrivateIPForbiddenError' // 400 Only public IPs are allowed. + | 'URLHostRequiredError' // 400 No URL host supplied. + | 'URLParsingFailedError' // 400 Failed to parse URL. + | 'URLRedirectsLimitExceededError' // 400 Too many redirects. + | 'URLSchemeInvalidError' // 400 Invalid URL scheme. + | 'URLSchemeRequiredError' // 400 No URL scheme supplied. + | 'URLValidationError' // 400 Failed to validate URL. diff --git a/packages/upload-client/src/tools/errors.ts b/packages/upload-client/src/tools/UploadError.ts similarity index 61% rename from packages/upload-client/src/tools/errors.ts rename to packages/upload-client/src/tools/UploadError.ts index ccea15752..59baf697f 100644 --- a/packages/upload-client/src/tools/errors.ts +++ b/packages/upload-client/src/tools/UploadError.ts @@ -1,37 +1,37 @@ +import { UploadcareError } from '@uploadcare/api-client-utils' import { Headers, ErrorRequestInfo } from '../request/types' +import type { ServerErrorCode } from './ServerErrorCode' export type ErrorResponseInfo = { error?: { statusCode: number content: string - errorCode: string + errorCode: ServerErrorCode } } -export class UploadClientError extends Error { - isCancel?: boolean - - readonly code?: string +export class UploadError extends UploadcareError { + readonly code?: ServerErrorCode readonly request?: ErrorRequestInfo readonly response?: ErrorResponseInfo readonly headers?: Headers constructor( message: string, - code?: string, + code?: ServerErrorCode, request?: ErrorRequestInfo, response?: ErrorResponseInfo, headers?: Headers ) { super() - this.name = 'UploadClientError' + this.name = 'UploadError' this.message = message this.code = code this.request = request this.response = response this.headers = headers - Object.setPrototypeOf(this, UploadClientError.prototype) + Object.setPrototypeOf(this, UploadError.prototype) } } diff --git a/packages/upload-client/src/tools/retryIfFailed.ts b/packages/upload-client/src/tools/retryIfFailed.ts index 9c52effa9..0f73ac265 100644 --- a/packages/upload-client/src/tools/retryIfFailed.ts +++ b/packages/upload-client/src/tools/retryIfFailed.ts @@ -1,11 +1,11 @@ -import { UploadClientError } from './errors' -import { retrier, UploadcareNetworkError } from '@uploadcare/api-client-utils' +import { UploadError } from './UploadError' +import { retrier, NetworkError } from '@uploadcare/api-client-utils' const REQUEST_WAS_THROTTLED_CODE = 'RequestThrottledError' const DEFAULT_RETRY_AFTER_TIMEOUT = 15000 const DEFAULT_NETWORK_ERROR_TIMEOUT = 1000 -function getTimeoutFromThrottledRequest(error: UploadClientError): number { +function getTimeoutFromThrottledRequest(error: UploadError): number { const { headers } = error || {} if (!headers || typeof headers['retry-after'] !== 'string') { return DEFAULT_RETRY_AFTER_TIMEOUT @@ -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 | UploadError | 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/src/uploadFile/pusher.ts b/packages/upload-client/src/uploadFile/pusher.ts index d39f3d6f6..60944e96c 100644 --- a/packages/upload-client/src/uploadFile/pusher.ts +++ b/packages/upload-client/src/uploadFile/pusher.ts @@ -1,7 +1,8 @@ import WebSocket from '../tools/sockets.node' -import { FileInfo } from '../api/types' import { Status } from '../api/fromUrlStatus' +import { FileInfo } from '../api/types' +import { ServerErrorCode } from '../tools/ServerErrorCode' import { Events } from './events' type AllStatuses = @@ -19,7 +20,7 @@ type StatusErrorResponse = { status: Status.Error msg: string url: string - error_code: string + error_code: ServerErrorCode } type StatusSuccessResponse = { diff --git a/packages/upload-client/src/uploadFile/uploadFromUrl.ts b/packages/upload-client/src/uploadFile/uploadFromUrl.ts index 7d896e50b..d42ba548e 100644 --- a/packages/upload-client/src/uploadFile/uploadFromUrl.ts +++ b/packages/upload-client/src/uploadFile/uploadFromUrl.ts @@ -1,6 +1,6 @@ import fromUrlStatus, { Status } from '../api/fromUrlStatus' import fromUrl, { TypeEnum, FromUrlOptions } from '../api/fromUrl' -import { UploadClientError } from '../tools/errors' +import { UploadError } from '../tools/UploadError' import { race } from '../tools/race' import { isReadyPoll } from '../tools/isReadyPoll' import defaultSettings from '../defaultSettings' @@ -35,8 +35,8 @@ function pollStrategy({ retryNetworkErrorMaxTimes?: number onProgress?: ProgressCallback signal?: AbortSignal -}): Promise { - return poll({ +}): Promise { + return poll({ check: (signal) => fromUrlStatus(token, { publicKey, @@ -49,13 +49,13 @@ function pollStrategy({ }).then((response) => { switch (response.status) { case Status.Error: { - return new UploadClientError(response.error, response.errorCode) + return new UploadError(response.error, response.errorCode) } case Status.Waiting: { return false } case Status.Unknown: { - return new UploadClientError(`Token "${token}" was not found.`) + return new UploadError(`Token "${token}" was not found.`) } case Status.Progress: { if (onProgress) { @@ -103,7 +103,7 @@ const pushStrategy = ({ pusherKey: string signal: AbortSignal onProgress?: ProgressCallback -}): Promise => +}): Promise => new Promise((resolve, reject) => { const pusher = getPusher(pusherKey) const unsubErrorHandler = pusher.onError(reject) @@ -146,7 +146,7 @@ const pushStrategy = ({ case Status.Error: { destroy() - reject(new UploadClientError(result.msg, result.error_code)) + reject(new UploadError(result.msg, result.error_code)) } } }) @@ -202,9 +202,9 @@ export const uploadFromUrl = ( if (urlResponse.type === TypeEnum.FileInfo) { return urlResponse } else { - return race( + return race( [ - ({ signal }): Promise => + ({ signal }): Promise => pollStrategy({ token: urlResponse.token, publicKey, @@ -215,7 +215,7 @@ export const uploadFromUrl = ( onProgress, signal }), - ({ signal }): Promise => + ({ signal }): Promise => pushStrategy({ token: urlResponse.token, pusherKey, @@ -228,7 +228,7 @@ export const uploadFromUrl = ( } }) .then((result) => { - if (result instanceof UploadClientError) throw result + if (result instanceof UploadError) throw result return result }) diff --git a/packages/upload-client/test/api/base.test.ts b/packages/upload-client/test/api/base.test.ts index 887e50484..9e0d1285e 100644 --- a/packages/upload-client/test/api/base.test.ts +++ b/packages/upload-client/test/api/base.test.ts @@ -1,6 +1,6 @@ import base from '../../src/api/base' import * as factory from '../_fixtureFactory' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' import { assertComputableProgress } from '../_helpers' import { jest, expect } from '@jest/globals' @@ -48,10 +48,10 @@ describe('API - base', () => { try { await base(fileToUpload.data, { publicKey }) } catch (error) { - expect((error as UploadClientError).message).toEqual( + expect((error as UploadError).message).toEqual( 'UPLOADCARE_PUB_KEY is invalid.' ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/api/fromUrl.test.ts b/packages/upload-client/test/api/fromUrl.test.ts index 2fd7d0659..c9a12e00e 100644 --- a/packages/upload-client/test/api/fromUrl.test.ts +++ b/packages/upload-client/test/api/fromUrl.test.ts @@ -1,7 +1,7 @@ import fromUrl, { TypeEnum } from '../../src/api/fromUrl' import * as factory from '../_fixtureFactory' import { getSettingsForTesting } from '../_helpers' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' describe('API - from url', () => { const sourceUrl = factory.imageUrl('valid') @@ -68,10 +68,8 @@ describe('API - from url', () => { try { await fromUrl(sourceUrl, { publicKey }) } catch (error) { - expect((error as UploadClientError).message).toEqual( - 'pub_key is invalid.' - ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).message).toEqual('pub_key is invalid.') + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/api/fromUrlStatus.test.ts b/packages/upload-client/test/api/fromUrlStatus.test.ts index cee5a1863..dd359d578 100644 --- a/packages/upload-client/test/api/fromUrlStatus.test.ts +++ b/packages/upload-client/test/api/fromUrlStatus.test.ts @@ -1,7 +1,7 @@ import fromUrlStatus, { Status } from '../../src/api/fromUrlStatus' import * as factory from '../_fixtureFactory' import { getSettingsForTesting } from '../_helpers' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' describe('API - from url status', () => { const token = factory.token('valid') @@ -50,10 +50,10 @@ describe('API - from url status', () => { try { await fromUrlStatus('token', { publicKey }) } catch (error) { - expect((error as UploadClientError).message).toEqual( + expect((error as UploadError).message).toEqual( 'UPLOADCARE_PUB_KEY is invalid.' ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/api/group.test.ts b/packages/upload-client/test/api/group.test.ts index 111baebd8..2192e1085 100644 --- a/packages/upload-client/test/api/group.test.ts +++ b/packages/upload-client/test/api/group.test.ts @@ -1,7 +1,7 @@ import * as factory from '../_fixtureFactory' import { getSettingsForTesting } from '../_helpers' import group from '../../src/api/group' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' import { GroupFileInfo } from '../../src' describe('API - group', () => { @@ -69,10 +69,8 @@ describe('API - group', () => { try { await group([], { publicKey }) } catch (error) { - expect((error as UploadClientError).message).toEqual( - 'pub_key is invalid.' - ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).message).toEqual('pub_key is invalid.') + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/api/groupInfo.test.ts b/packages/upload-client/test/api/groupInfo.test.ts index bedf34007..f9a7b8828 100644 --- a/packages/upload-client/test/api/groupInfo.test.ts +++ b/packages/upload-client/test/api/groupInfo.test.ts @@ -2,7 +2,7 @@ import * as factory from '../_fixtureFactory' import { getSettingsForTesting } from '../_helpers' import group from '../../src/api/group' import groupInfo from '../../src/api/groupInfo' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' describe('API - group info', () => { const files = factory.groupOfFiles('valid') @@ -50,10 +50,8 @@ describe('API - group info', () => { try { await groupInfo('id', { publicKey }) } catch (error) { - expect((error as UploadClientError).message).toEqual( - 'pub_key is invalid.' - ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).message).toEqual('pub_key is invalid.') + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/api/info.test.ts b/packages/upload-client/test/api/info.test.ts index 35f7c48e1..94cbe84a0 100644 --- a/packages/upload-client/test/api/info.test.ts +++ b/packages/upload-client/test/api/info.test.ts @@ -1,7 +1,7 @@ import info from '../../src/api/info' import * as factory from '../_fixtureFactory' import { getSettingsForTesting } from '../_helpers' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' describe('API - info', () => { it('should return file info', async () => { @@ -46,10 +46,8 @@ describe('API - info', () => { try { await info('uuid', { publicKey }) } catch (error) { - expect((error as UploadClientError).message).toEqual( - 'pub_key is invalid.' - ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).message).toEqual('pub_key is invalid.') + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/api/multipartComplete.test.ts b/packages/upload-client/test/api/multipartComplete.test.ts index b6b1a9582..44b828fb3 100644 --- a/packages/upload-client/test/api/multipartComplete.test.ts +++ b/packages/upload-client/test/api/multipartComplete.test.ts @@ -3,7 +3,7 @@ import multipartUpload from '../../src/api/multipartUpload' import multipartComplete from '../../src/api/multipartComplete' import * as factory from '../_fixtureFactory' import { getSettingsForTesting } from '../_helpers' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' import { jest, expect } from '@jest/globals' const getChunk = ( @@ -74,7 +74,7 @@ describe('API - multipartComplete', () => { await expect( multipartComplete(completedUuid, settings) - ).rejects.toThrowError(new UploadClientError('Request canceled')) + ).rejects.toThrowError(new UploadError('Request canceled')) expect(Date.now() - time).toBeLessThan(200) // could be slow on ci }) @@ -87,7 +87,7 @@ describe('API - multipartComplete', () => { const upload = multipartComplete('', settings) await expect(upload).rejects.toThrowError( - new UploadClientError('uuid is required.') + new UploadError('uuid is required.') ) }) @@ -97,10 +97,10 @@ describe('API - multipartComplete', () => { try { await multipartComplete('', { publicKey }) } catch (error) { - expect((error as UploadClientError).message).toEqual( + expect((error as UploadError).message).toEqual( 'UPLOADCARE_PUB_KEY is invalid.' ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/api/multipartStart.test.ts b/packages/upload-client/test/api/multipartStart.test.ts index a5170a593..6c98ee9c0 100644 --- a/packages/upload-client/test/api/multipartStart.test.ts +++ b/packages/upload-client/test/api/multipartStart.test.ts @@ -1,7 +1,7 @@ import multipartStart from '../../src/api/multipartStart' import * as factory from '../_fixtureFactory' import { getSettingsForTesting } from '../_helpers' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' describe('API - multipartStart', () => { const size = factory.file(12).size @@ -31,7 +31,7 @@ describe('API - multipartStart', () => { }) await expect(upload).rejects.toThrowError( - new UploadClientError('Request canceled') + new UploadError('Request canceled') ) }) @@ -45,7 +45,7 @@ describe('API - multipartStart', () => { const upload = multipartStart(size, settings) await expect(upload).rejects.toThrowError( - new UploadClientError( + new UploadError( 'File size can not be less than 10485760 bytes. Please use direct upload instead of multipart.' ) ) @@ -61,10 +61,10 @@ describe('API - multipartStart', () => { try { await multipartStart(size, settings) } catch (error) { - expect((error as UploadClientError).message).toEqual( + expect((error as UploadError).message).toEqual( 'UPLOADCARE_PUB_KEY is invalid.' ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/api/multipartUpload.test.ts b/packages/upload-client/test/api/multipartUpload.test.ts index 6a77063f7..c761ab1aa 100644 --- a/packages/upload-client/test/api/multipartUpload.test.ts +++ b/packages/upload-client/test/api/multipartUpload.test.ts @@ -2,7 +2,7 @@ import * as factory from '../_fixtureFactory' import multipartUpload from '../../src/api/multipartUpload' import { getSettingsForTesting, assertComputableProgress } from '../_helpers' import multipartStart from '../../src/api/multipartStart' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' import { jest, expect } from '@jest/globals' let parts: [string, Blob | Buffer][] = [] @@ -50,7 +50,7 @@ describe('API - multipartUpload', () => { }) await expect(multipartUpload(part, url, options)).rejects.toThrowError( - new UploadClientError('Request canceled') + new UploadError('Request canceled') ) }) @@ -76,10 +76,10 @@ describe('API - multipartUpload', () => { try { await multipartUpload(part, url, options) } catch (error) { - expect((error as UploadClientError).message).toEqual( + expect((error as UploadError).message).toEqual( 'UPLOADCARE_PUB_KEY is invalid.' ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/browser/request.test.ts b/packages/upload-client/test/browser/request.test.ts index 58847c128..1a19c91a7 100644 --- a/packages/upload-client/test/browser/request.test.ts +++ b/packages/upload-client/test/browser/request.test.ts @@ -1,6 +1,6 @@ /** @jest-environment jsdom */ -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' import request from '../../src/request/request.browser' import getUrl from '../../src/tools/getUrl' import { jest, expect } from '@jest/globals' @@ -53,7 +53,7 @@ describe('request', () => { }), signal: cntr.signal }) - ).rejects.toThrowError(new UploadClientError('Request canceled')) + ).rejects.toThrowError(new UploadError('Request canceled')) }) it('should handle progress', async () => { diff --git a/packages/upload-client/test/tools/UploadError.test.ts b/packages/upload-client/test/tools/UploadError.test.ts new file mode 100644 index 000000000..ab51176d9 --- /dev/null +++ b/packages/upload-client/test/tools/UploadError.test.ts @@ -0,0 +1,34 @@ +import { UploadcareError } from '@uploadcare/api-client-utils' +import { UploadError } from '../../src/tools/UploadError' + +describe('UploadError', () => { + it('should work', () => { + const error = new UploadError('test error') + + expect(error instanceof UploadError).toBeTruthy() + }) + + it('should have message', () => { + const error = new UploadError('test error') + + expect(error.message).toBe('test error') + }) + + it('should have code', () => { + const error = new UploadError('test error', 'AccountBlockedError') + + expect(error.message).toBe('test error') + expect(error.code).toBe('AccountBlockedError') + }) + + it('should have stack', () => { + const error = new UploadError('test error') + + expect(error.stack).toBeDefined() + }) + + it('should be instanceof UploadcareError', () => { + const error = new UploadError('test error') + expect(error).toBeInstanceOf(UploadcareError) + }) +}) diff --git a/packages/upload-client/test/tools/errors.test.ts b/packages/upload-client/test/tools/errors.test.ts deleted file mode 100644 index 5cbe43376..000000000 --- a/packages/upload-client/test/tools/errors.test.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { UploadClientError } from '../../src/tools/errors' - -describe('UploadClientError', () => { - it('should work', () => { - const error = new UploadClientError('test error') - - expect(error instanceof UploadClientError).toBeTruthy() - }) - - it('should have message', () => { - const error = new UploadClientError('test error') - - expect(error.message).toBe('test error') - }) - - it('should have code', () => { - const error = new UploadClientError('test error', 'error code') - - expect(error.message).toBe('test error') - expect(error.code).toBe('error code') - }) - - it('should have stack', () => { - const error = new UploadClientError('test error') - - expect(error.stack).toBeDefined() - }) -}) diff --git a/packages/upload-client/test/tools/retryIfFailed.test.ts b/packages/upload-client/test/tools/retryIfFailed.test.ts index 23f8a0936..199e819ef 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 { UploadError } from '../../src/tools/UploadError' import { jest, expect } from '@jest/globals' -import { UploadcareNetworkError } from '@uploadcare/api-client-utils' +import { NetworkError } from '@uploadcare/api-client-utils' const createRunner = ({ attempts = 10, @@ -31,7 +31,7 @@ const createRunner = ({ return { spy, task } } -const throttledError = new UploadClientError( +const throttledError = new UploadError( 'test error', 'RequestThrottledError', undefined, @@ -45,7 +45,7 @@ const throttledError = new UploadClientError( { 'retry-after': '1' } ) -const networkError = new UploadcareNetworkError( +const networkError = new NetworkError( new Event('ProgressEvent') as ProgressEvent ) @@ -76,7 +76,7 @@ describe('retryIfFailed', () => { expect(spy).toHaveBeenCalledTimes(1) }) - it('should be rejected with UploadClientError if MaxTimes = 0', async () => { + it('should be rejected with UploadError if MaxTimes = 0', async () => { const { spy, task } = createRunner({ error: throttledError }) await expect( @@ -84,7 +84,7 @@ describe('retryIfFailed', () => { retryThrottledRequestMaxTimes: 0, retryNetworkErrorMaxTimes: 0 }) - ).rejects.toThrowError(UploadClientError) + ).rejects.toThrowError(UploadError) expect(spy).toHaveBeenCalledTimes(1) }) @@ -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) }) diff --git a/packages/upload-client/test/uploadFile/uploadDirect.test.ts b/packages/upload-client/test/uploadFile/uploadDirect.test.ts index 5d727863d..af1427a58 100644 --- a/packages/upload-client/test/uploadFile/uploadDirect.test.ts +++ b/packages/upload-client/test/uploadFile/uploadDirect.test.ts @@ -1,6 +1,6 @@ import * as factory from '../_fixtureFactory' import { getSettingsForTesting, assertComputableProgress } from '../_helpers' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' import { uploadDirect } from '../../src/uploadFile/uploadDirect' import { jest, expect } from '@jest/globals' @@ -89,10 +89,10 @@ describe('uploadDirect', () => { try { await uploadDirect(fileToUpload, settings) } catch (error) { - expect((error as UploadClientError).message).toEqual( + expect((error as UploadError).message).toEqual( 'UPLOADCARE_PUB_KEY is invalid.' ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/uploadFile/uploadFromUploaded.test.ts b/packages/upload-client/test/uploadFile/uploadFromUploaded.test.ts index 8471b5660..0f3e426f0 100644 --- a/packages/upload-client/test/uploadFile/uploadFromUploaded.test.ts +++ b/packages/upload-client/test/uploadFile/uploadFromUploaded.test.ts @@ -1,6 +1,6 @@ import * as factory from '../_fixtureFactory' import { getSettingsForTesting, assertComputableProgress } from '../_helpers' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' import { uploadFromUploaded } from '../../src/uploadFile/uploadFromUploaded' import { jest, expect } from '@jest/globals' @@ -26,7 +26,7 @@ describe('uploadFromUploaded', () => { ctrl.abort() await expect(upload).rejects.toThrowError( - new UploadClientError('Request canceled') + new UploadError('Request canceled') ) }) @@ -61,10 +61,8 @@ describe('uploadFromUploaded', () => { try { await uploadFromUploaded(uuid, settings) } catch (error) { - expect((error as UploadClientError).message).toEqual( - 'pub_key is invalid.' - ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).message).toEqual('pub_key is invalid.') + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/uploadFile/uploadFromUrl.test.ts b/packages/upload-client/test/uploadFile/uploadFromUrl.test.ts index 205abaf43..049bf92e1 100644 --- a/packages/upload-client/test/uploadFile/uploadFromUrl.test.ts +++ b/packages/upload-client/test/uploadFile/uploadFromUrl.test.ts @@ -4,7 +4,7 @@ import { assertComputableProgress, assertUnknownProgress } from '../_helpers' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' import http from 'http' import https, { RequestOptions } from 'https' import { uploadFromUrl } from '../../src/uploadFile/uploadFromUrl' @@ -93,7 +93,7 @@ describe('uploadFromUrl', () => { }) await expect(uploadFromUrl(sourceUrl, settings)).rejects.toThrowError( - new UploadClientError('Request canceled') + new UploadError('Request canceled') ) }) @@ -145,10 +145,8 @@ describe('uploadFromUrl', () => { try { await uploadFromUrl(sourceUrl, settings) } catch (error) { - expect((error as UploadClientError).message).toEqual( - 'pub_key is invalid.' - ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).message).toEqual('pub_key is invalid.') + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/uploadFile/uploadMultipart.test.ts b/packages/upload-client/test/uploadFile/uploadMultipart.test.ts index 4200f21b4..9d1bb1546 100644 --- a/packages/upload-client/test/uploadFile/uploadMultipart.test.ts +++ b/packages/upload-client/test/uploadFile/uploadMultipart.test.ts @@ -1,6 +1,6 @@ import * as factory from '../_fixtureFactory' import { getSettingsForTesting, assertComputableProgress } from '../_helpers' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' import { uploadMultipart } from '../../src/uploadFile/uploadMultipart' import { jest, expect } from '@jest/globals' @@ -39,7 +39,7 @@ describe('uploadMultipart', () => { ctrl.abort() await expect(upload).rejects.toThrowError( - new UploadClientError('Request canceled') + new UploadError('Request canceled') ) }) @@ -74,10 +74,10 @@ describe('uploadMultipart', () => { try { await uploadMultipart(fileToUpload, settings) } catch (error) { - expect((error as UploadClientError).message).toEqual( + expect((error as UploadError).message).toEqual( 'UPLOADCARE_PUB_KEY is invalid.' ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/uploadFileGroup/groupFromObject.test.ts b/packages/upload-client/test/uploadFileGroup/groupFromObject.test.ts index 61c74bea7..7a69b69ab 100644 --- a/packages/upload-client/test/uploadFileGroup/groupFromObject.test.ts +++ b/packages/upload-client/test/uploadFileGroup/groupFromObject.test.ts @@ -1,7 +1,7 @@ import * as factory from '../_fixtureFactory' import { uploadFileGroup } from '../../src/uploadFileGroup' import { getSettingsForTesting, assertComputableProgress } from '../_helpers' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' import { jest, expect } from '@jest/globals' describe('groupFrom Object[]', () => { @@ -38,7 +38,7 @@ describe('groupFrom Object[]', () => { ctrl.abort() await expect(upload).rejects.toThrowError( - new UploadClientError('Request canceled') + new UploadError('Request canceled') ) }) @@ -62,10 +62,10 @@ describe('groupFrom Object[]', () => { try { await uploadFileGroup(files, settings) } catch (error) { - expect((error as UploadClientError).message).toEqual( + expect((error as UploadError).message).toEqual( 'UPLOADCARE_PUB_KEY is invalid.' ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/uploadFileGroup/groupFromUploaded.test.ts b/packages/upload-client/test/uploadFileGroup/groupFromUploaded.test.ts index 1488647ef..5a2afb756 100644 --- a/packages/upload-client/test/uploadFileGroup/groupFromUploaded.test.ts +++ b/packages/upload-client/test/uploadFileGroup/groupFromUploaded.test.ts @@ -1,7 +1,7 @@ import * as factory from '../_fixtureFactory' import { getSettingsForTesting, assertComputableProgress } from '../_helpers' import { uploadFileGroup } from '../../src/uploadFileGroup' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' import { jest, expect } from '@jest/globals' describe('groupFrom Uploaded[]', () => { @@ -43,7 +43,7 @@ describe('groupFrom Uploaded[]', () => { ctrl.abort() await expect(upload).rejects.toThrowError( - new UploadClientError('Request canceled') + new UploadError('Request canceled') ) }) @@ -67,10 +67,8 @@ describe('groupFrom Uploaded[]', () => { try { await uploadFileGroup(files, settings) } catch (error) { - expect((error as UploadClientError).message).toEqual( - 'pub_key is invalid.' - ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).message).toEqual('pub_key is invalid.') + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) } diff --git a/packages/upload-client/test/uploadFileGroup/groupFromUrl.test.ts b/packages/upload-client/test/uploadFileGroup/groupFromUrl.test.ts index c8103ddd1..9d0805b29 100644 --- a/packages/upload-client/test/uploadFileGroup/groupFromUrl.test.ts +++ b/packages/upload-client/test/uploadFileGroup/groupFromUrl.test.ts @@ -5,7 +5,7 @@ import { assertUnknownProgress } from '../_helpers' import { uploadFileGroup } from '../../src/uploadFileGroup' -import { UploadClientError } from '../../src/tools/errors' +import { UploadError } from '../../src/tools/UploadError' import { jest, expect } from '@jest/globals' describe('groupFrom Url[]', () => { @@ -42,7 +42,7 @@ describe('groupFrom Url[]', () => { ctrl.abort() await expect(upload).rejects.toThrowError( - new UploadClientError('Request canceled') + new UploadError('Request canceled') ) }) @@ -83,10 +83,8 @@ describe('groupFrom Url[]', () => { try { await uploadFileGroup(files, settings) } catch (error) { - expect((error as UploadClientError).message).toEqual( - 'pub_key is invalid.' - ) - expect((error as UploadClientError).code).toEqual( + expect((error as UploadError).message).toEqual('pub_key is invalid.') + expect((error as UploadError).code).toEqual( 'ProjectPublicKeyInvalidError' ) }