Skip to content

Commit 855e5b1

Browse files
authored
feat(parseResponse): set DetailedError.name (+ error tests) (#4344)
* chore(client): set `DetailedError.name` * chore: add tests for error responses
1 parent 82bdc4d commit 855e5b1

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/client/fetch-result-please.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class DetailedError extends Error {
5757
options: { detail?: any; code?: any; statusCode?: number; log?: any } = {}
5858
) {
5959
super(message)
60+
this.name = 'DetailedError'
6061
this.log = options.log
6162
this.detail = options.detail
6263
this.code = options.code

src/client/utils.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ describe('parseResponse', async () => {
151151
.get('/text', (c) => c.text('hi'))
152152
.get('/json', (c) => c.json({ message: 'hi' }))
153153
.get('/404', (c) => c.notFound())
154+
.get('/500', (c) => c.text('500 Internal Server Error', 500))
154155
.get('/raw', (c) => {
155156
c.header('content-type', '')
156157
return c.body('hello')
@@ -176,6 +177,9 @@ describe('parseResponse', async () => {
176177
http.get('http://localhost/404', () => {
177178
return HttpResponse.text('404 Not Found', { status: 404 })
178179
}),
180+
http.get('http://localhost/500', () => {
181+
return HttpResponse.text('500 Internal Server Error', { status: 500 })
182+
}),
179183
http.get('http://localhost/raw', () => {
180184
return HttpResponse.text('hello', {
181185
headers: {
@@ -237,5 +241,23 @@ describe('parseResponse', async () => {
237241
expect(result).toMatchInlineSnapshot('"hono"')
238242
type _verify = Expect<Equal<typeof result, string>>
239243
}),
244+
it('should throw error matching snapshots', async () => {
245+
// Defined 404 route
246+
await expect(parseResponse(client['404'].$get())).rejects.toThrowErrorMatchingInlineSnapshot(
247+
'[DetailedError: 404 Not Found]'
248+
)
249+
250+
// Defined 500 route
251+
await expect(parseResponse(client['500'].$get())).rejects.toThrowErrorMatchingInlineSnapshot(
252+
'[DetailedError: 500 Internal Server Error]'
253+
)
254+
255+
// Not defined route
256+
// Note: the error in this test case is thrown at the `fetch` call (.$get()), not during the `parseResponse` call, so I think `parseResponse` should not try to catch and wrap it into a structured error, which could be inconsistent, if the user awaited the fetch.
257+
await expect(
258+
// @ts-expect-error noRoute is not defined
259+
parseResponse(client['noRoute'].$get())
260+
).rejects.toThrowErrorMatchingInlineSnapshot('[TypeError: fetch failed]')
261+
}),
240262
])
241263
})

0 commit comments

Comments
 (0)