Skip to content

Commit 7ca43d8

Browse files
authored
Chore/fix error handling in generate v3 (supabase#30913)
* Fix error handling in generate-v3 endpoint * Lint
1 parent d4b1a7c commit 7ca43d8

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

apps/studio/pages/api/ai/sql/generate-v3.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
import { openai } from '@ai-sdk/openai'
2-
import { streamText } from 'ai'
3-
import { getTools } from './tools'
42
import pgMeta from '@supabase/pg-meta'
5-
import { executeSql } from 'data/sql/execute-sql-query'
3+
import { streamText } from 'ai'
64
import { NextApiRequest, NextApiResponse } from 'next'
75

6+
import { executeSql } from 'data/sql/execute-sql-query'
7+
import { getTools } from './tools'
8+
89
export const maxDuration = 30
910
const openAiKey = process.env.OPENAI_API_KEY
1011
const pgMetaSchemasList = pgMeta.schemas.list()
1112

1213
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
1314
if (!openAiKey) {
14-
return new Response(
15-
JSON.stringify({
16-
error: 'No OPENAI_API_KEY set. Create this environment variable to use AI features.',
17-
}),
18-
{
19-
status: 500,
20-
headers: { 'Content-Type': 'application/json' },
21-
}
22-
)
15+
return res.status(400).json({
16+
error: 'No OPENAI_API_KEY set. Create this environment variable to use AI features.',
17+
})
2318
}
2419

2520
const { method } = req
@@ -28,13 +23,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
2823
case 'POST':
2924
return handlePost(req, res)
3025
default:
31-
return new Response(
32-
JSON.stringify({ data: null, error: { message: `Method ${method} Not Allowed` } }),
33-
{
34-
status: 405,
35-
headers: { 'Content-Type': 'application/json', Allow: 'POST' },
36-
}
37-
)
26+
res.setHeader('Allow', ['POST'])
27+
res.status(405).json({ data: null, error: { message: `Method ${method} Not Allowed` } })
3828
}
3929
}
4030

0 commit comments

Comments
 (0)