Skip to content

Commit a375381

Browse files
committed
feat: error handlings
1 parent 70f1de2 commit a375381

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/lib/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// VSCode client ID
12
const GITHUB_CLIENT_ID = "01ab8ac9400c4e429b23"
23
const GITHUB_OAUTH_SCOPES = [
34
"read:org",

src/routes/chat-completions/route.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import consola from "consola"
12
import { Hono } from "hono"
23
import { streamSSE } from "hono/streaming"
4+
import { FetchError } from "ofetch"
35

46
import type { ChatCompletionsPayload } from "~/services/copilot-vscode/chat-completions/types"
57
import type { ChatCompletionsChunk } from "~/services/copilot-vscode/chat-completions/types.streaming"
@@ -13,7 +15,16 @@ chatCompletionsRoutes.post("/chat/completions", async (c) => {
1315

1416
payload.stream = false
1517

16-
const response = await chatCompletions(payload)
18+
const response = await chatCompletions(payload).catch((error: unknown) => {
19+
if (error instanceof FetchError) {
20+
consola.error(
21+
// eslint-disable-next-line @typescript-eslint/no-base-to-string, @typescript-eslint/restrict-template-expressions
22+
`Request failed: ${JSON.stringify(payload)} \n ${error} \n ${error.response?._data}`,
23+
)
24+
}
25+
26+
throw error
27+
})
1728

1829
const segmenter = new Intl.Segmenter("en", { granularity: "word" })
1930

src/services/api-instance.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ofetch } from "ofetch"
1+
import consola from "consola"
2+
import { FetchError, ofetch } from "ofetch"
23

34
import { TOKENS } from "~/lib/tokens"
45

@@ -11,6 +12,24 @@ export const copilot = ofetch.create({
1112
onRequest({ options }) {
1213
options.headers.set("authorization", `Bearer ${TOKENS.COPILOT_TOKEN}`)
1314
},
15+
16+
onRequestError({ error, options }) {
17+
if (error instanceof FetchError) {
18+
consola.error(
19+
// eslint-disable-next-line @typescript-eslint/no-base-to-string, @typescript-eslint/restrict-template-expressions
20+
`Request failed: ${options.body} \n ${error}`,
21+
)
22+
}
23+
},
24+
25+
onResponseError({ error, response, options }) {
26+
if (error instanceof FetchError) {
27+
consola.error(
28+
// eslint-disable-next-line @typescript-eslint/no-base-to-string, @typescript-eslint/restrict-template-expressions
29+
`Request failed: ${options.body} \n ${error} \n with response: ${JSON.stringify(response)}`,
30+
)
31+
}
32+
},
1433
})
1534

1635
export const github = ofetch.create({

0 commit comments

Comments
 (0)