Skip to content

Commit

Permalink
Merge pull request #506 from uploadcare/fix/add-missing-server-errors…
Browse files Browse the repository at this point in the history
…-types

Refactor errors
  • Loading branch information
nd0ut authored Dec 22, 2023
2 parents 3ba1bc0 + 60ba60b commit 1368c66
Show file tree
Hide file tree
Showing 48 changed files with 335 additions and 195 deletions.
11 changes: 8 additions & 3 deletions packages/api-client-utils/src/CancelError.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
7 changes: 6 additions & 1 deletion packages/api-client-utils/src/CancelError.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
19 changes: 19 additions & 0 deletions packages/api-client-utils/src/NetworkError.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
15 changes: 15 additions & 0 deletions packages/api-client-utils/src/NetworkError.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
8 changes: 8 additions & 0 deletions packages/api-client-utils/src/UploadcareError.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
1 change: 1 addition & 0 deletions packages/api-client-utils/src/UploadcareError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class UploadcareError extends Error {}
12 changes: 0 additions & 12 deletions packages/api-client-utils/src/UploadcareNetworkError.test.ts

This file was deleted.

13 changes: 0 additions & 13 deletions packages/api-client-utils/src/UploadcareNetworkError.ts

This file was deleted.

3 changes: 2 additions & 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 All @@ -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'
4 changes: 3 additions & 1 deletion packages/rest-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions packages/rest-client/src/tools/RestClientError.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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)
})
})
4 changes: 3 additions & 1 deletion packages/rest-client/src/tools/RestClientError.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { UploadcareError } from '@uploadcare/api-client-utils'

export type RestClientErrorOptions = {
request?: Request
response?: Response
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions packages/upload-client/src/api/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/upload-client/src/api/fromUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions packages/upload-client/src/api/fromUrlStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -36,7 +37,7 @@ export type StatusProgressResponse = {
export type StatusErrorResponse = {
status: Status.Error
error: string
errorCode: string
errorCode: ServerErrorCode
}

export type StatusSuccessResponse = {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/upload-client/src/api/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/upload-client/src/api/groupInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/upload-client/src/api/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/upload-client/src/api/multipartComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/upload-client/src/api/multipartStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 14 additions & 3 deletions 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 @@ -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,
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
Loading

0 comments on commit 1368c66

Please sign in to comment.