Skip to content

Commit

Permalink
feat(api-client-utils): add base class UploadcareError for the errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nd0ut committed Dec 21, 2023
1 parent 7adf799 commit 766586a
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 5 deletions.
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)
}
}
4 changes: 3 additions & 1 deletion packages/api-client-utils/src/NetworkError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export class NetworkError extends Error {
import { UploadcareError } from './UploadcareError'

export class NetworkError extends UploadcareError {
originalProgressEvent: ProgressEvent

constructor(progressEvent: ProgressEvent) {
Expand Down
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 {}
1 change: 1 addition & 0 deletions packages/api-client-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/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
3 changes: 2 additions & 1 deletion packages/upload-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export {
CustomUserAgentFn,
CustomUserAgentOptions,
GetUserAgentOptions,
CancelError
CancelError,
UploadcareError
} from '@uploadcare/api-client-utils'
export { Queue } from './tools/Queue'

Expand Down
3 changes: 2 additions & 1 deletion packages/upload-client/src/tools/errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UploadcareError } from '@uploadcare/api-client-utils'
import { Headers, ErrorRequestInfo } from '../request/types'

export type ErrorResponseInfo = {
Expand All @@ -8,7 +9,7 @@ export type ErrorResponseInfo = {
}
}

export class UploadClientError extends Error {
export class UploadClientError extends UploadcareError {
isCancel?: boolean

readonly code?: string
Expand Down

0 comments on commit 766586a

Please sign in to comment.