Skip to content

Commit 1e50757

Browse files
authored
fix(studio): log drains api response in self-hosted (supabase#41935)
The self-hosted version of the /platform/projects/[ref]/analytics/log-drains endpoint is always returning a 200 even if the upstream errors, which causes the frontend to crash because the returned data is not a JSON array. Updated to properly return a 500 for errors.
1 parent 1602abd commit 1e50757

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

  • apps/studio/pages/api/platform/projects/[ref]/analytics

apps/studio/pages/api/platform/projects/[ref]/analytics/log-drains.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { NextApiRequest, NextApiResponse } from 'next'
21
import apiWrapper from 'lib/api/apiWrapper'
32
import { PROJECT_ANALYTICS_URL } from 'lib/constants/api'
3+
import { NextApiRequest, NextApiResponse } from 'next'
44

55
export default (req: NextApiRequest, res: NextApiResponse) => apiWrapper(req, res, handler)
66

77
async function handler(req: NextApiRequest, res: NextApiResponse) {
88
const { method } = req
9-
const { ref } = req.query
109

1110
const missingEnvVars = envVarsSet()
1211

@@ -29,17 +28,29 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
2928
url.search = new URLSearchParams({
3029
'metadata[type]': 'log-drain',
3130
}).toString()
32-
const resp = await fetch(url, {
31+
const upstream = await fetch(url, {
3332
method: 'GET',
3433
headers: {
3534
Authorization: `Bearer ${process.env.LOGFLARE_PRIVATE_ACCESS_TOKEN}`,
3635
'Content-Type': 'application/json',
3736
Accept: 'application/json',
3837
},
39-
}).then((res) => {
40-
return res.json()
4138
})
4239

40+
if (!upstream.ok) {
41+
return res
42+
.status(500)
43+
.json({ error: { message: 'Failed to fetch log drains from upstream' } })
44+
}
45+
46+
const resp = await upstream.json()
47+
48+
if (!Array.isArray(resp)) {
49+
return res
50+
.status(500)
51+
.json({ error: { message: 'Unexpected response format from upstream' } })
52+
}
53+
4354
return res.status(200).json(resp)
4455
case 'POST':
4556
// create the log drain

0 commit comments

Comments
 (0)