Skip to content

Commit

Permalink
export error ids
Browse files Browse the repository at this point in the history
  • Loading branch information
honzabrecka committed Nov 22, 2017
1 parent 31f8a18 commit 4c30ea1
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,21 @@ const newError = (id: string, message: string, res?: Response) => {
return error
}

export const errorIds = {
TimeoutError: 'TimeoutError',
RetryError: 'RetryError',
DecodeResponseError: 'DecodeResponseError',
InvalidStatusCodeError: 'InvalidStatusCodeError',
}

export type Delay = (t: number) => Promise<void>

export const delay: Delay = (time) => new Promise((resolve, _) => {
setTimeout(() => resolve(), time)
})

const delayedFail = (timeout: number) => delay(timeout).then(() => {
throw newError('TimeoutError', 'Timeout')
throw newError(errorIds.TimeoutError, 'Timeout')
})

const tapAndDelay = (f: Function, time: number) => {
Expand Down Expand Up @@ -143,7 +150,7 @@ const withRetry = (max: number = 5, delay: Delay = delays.linear()) => (fetch: R
return new Promise((resolve, reject) => {
(function run(i: number, errors: Error[]) {
if (i === max + 1) {
const error = newError('RetryError', 'Retry failed');
const error = newError(errorIds.RetryError, 'Retry failed');
(error as any).errors = errors
reject(error)
} else
Expand Down Expand Up @@ -183,7 +190,7 @@ const decodeJSONResponse = async (res: Response) => {
(res as any).data = await res.json()
return res
} catch (e) {
throw newError('DecodeResponseError', e.message, res)
throw newError(errorIds.DecodeResponseError, e.message, res)
}
}

Expand All @@ -192,7 +199,7 @@ const decodeFormDataResponse = async (res: Response) => {
(res as any).data = await res.formData()
return res
} catch (e) {
throw newError('DecodeResponseError', e.message, res)
throw newError(errorIds.DecodeResponseError, e.message, res)
}
}

Expand All @@ -208,7 +215,7 @@ const decodeBlobResponse = async (res: Response) => {

const checkStatus = (res: Response) => {
if (res.status < 200 || res.status >= 400)
throw newError('InvalidStatusCodeError', 'Invalid status code: ' + res.status, res)
throw newError(errorIds.InvalidStatusCodeError, 'Invalid status code: ' + res.status, res)
return res
}

Expand Down

0 comments on commit 4c30ea1

Please sign in to comment.