Skip to content

Commit 7b92d74

Browse files
committed
default error type
1 parent a86fc42 commit 7b92d74

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ type _OpErrorType<T> = {
6969

7070
type Coalesce<T, D> = [T] extends [never] ? D : T
7171

72-
// coalesce default error type to unknown
72+
// coalesce default error type
7373
export type OpErrorType<OP> = Coalesce<
7474
_OpErrorType<OpResponseTypes<OP>>,
75-
unknown
75+
{ status: number; data: any }
7676
>
7777

7878
export type CustomRequestInit = Omit<RequestInit, 'headers'> & {

test/fetch.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@ describe('fetch', () => {
142142
other: true,
143143
})
144144
})
145+
it('default error type {status: number, data: any}', async () => {
146+
expect.assertions(2)
147+
148+
const fun = fetcher.path('/defaulterror').method('get').create()
149+
150+
try {
151+
await fun({})
152+
} catch (e) {
153+
if (e instanceof fun.Error) {
154+
const error = e.getActualType()
155+
expect(error.status).toBe(500)
156+
expect(error.data).toEqual('internal server error')
157+
}
158+
}
159+
})
145160

146161
it('network error', async () => {
147162
expect.assertions(1)

test/mocks/handlers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ const methods = {
6161
)
6262
: res(ctx.status(status))
6363
}),
64+
rest.get(`${HOST}/defaulterror`, (req, res, ctx) => {
65+
return res(ctx.status(500), ctx.body('internal server error'))
66+
}),
6467
rest.get(`${HOST}/networkerror`, (req, res) => {
6568
return res.networkError('failed to connect')
6669
}),

test/paths.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/ban-types */
12
export type Data = {
23
params: string[]
34
headers: Record<string, string>
@@ -65,9 +66,14 @@ export type paths = {
6566
}
6667
}
6768
}
69+
'/defaulterror': {
70+
get: {
71+
parameters: {}
72+
responses: {}
73+
}
74+
}
6875
'/networkerror': {
6976
get: {
70-
// eslint-disable-next-line @typescript-eslint/ban-types
7177
parameters: {}
7278
responses: {
7379
default: string

0 commit comments

Comments
 (0)