Skip to content

Commit

Permalink
feat: add status and statusText to fetch errors (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Oct 27, 2022
1 parent 456081c commit 784a7c0
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export class FetchError<T = any> extends Error {
request?: FetchRequest
response?: FetchResponse<T>
data?: T
status?: number
statusText?: string
statusCode?: number
statusMessage?: string
}

export function createFetchError<T = any> (request: FetchRequest, error?: Error, response?: FetchResponse<T>): FetchError<T> {
Expand All @@ -21,6 +25,10 @@ export function createFetchError<T = any> (request: FetchRequest, error?: Error,
Object.defineProperty(fetchError, 'request', { get () { return request } })
Object.defineProperty(fetchError, 'response', { get () { return response } })
Object.defineProperty(fetchError, 'data', { get () { return response && response._data } })
Object.defineProperty(fetchError, 'status', { get () { return response && response.status } })
Object.defineProperty(fetchError, 'statusText', { get () { return response && response.statusText } })
Object.defineProperty(fetchError, 'statusCode', { get () { return response && response.status } })
Object.defineProperty(fetchError, 'statusMessage', { get () { return response && response.statusText } })

return fetchError
}

0 comments on commit 784a7c0

Please sign in to comment.