Skip to content

Commit

Permalink
fix: Don't throw an error when parsing json response (#20747)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite authored Mar 7, 2024
1 parent d6650a5 commit bca5d25
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
15 changes: 6 additions & 9 deletions frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,11 @@ export function getCookie(name: string): string | null {
return cookieValue
}

export async function getJSONOrThrow(response: Response): Promise<any> {
if (response.status === 204) {
return null
}
export async function getJSONOrNull(response: Response): Promise<any> {
try {
return await response.json()
} catch (e) {
throw new ApiError('Failed to parse response JSON', response.status, response.statusText)
return null
}
}

Expand Down Expand Up @@ -2050,7 +2047,7 @@ const api = {
/** Fetch data from specified URL. The result already is JSON-parsed. */
async get<T = any>(url: string, options?: ApiMethodOptions): Promise<T> {
const res = await api.getResponse(url, options)
return await getJSONOrThrow(res)
return await getJSONOrNull(res)
},

async getResponse(url: string, options?: ApiMethodOptions): Promise<Response> {
Expand Down Expand Up @@ -2086,12 +2083,12 @@ const api = {
})
})

return await getJSONOrThrow(response)
return await getJSONOrNull(response)
},

async create(url: string, data?: any, options?: ApiMethodOptions): Promise<any> {
const res = await api.createResponse(url, data, options)
return await getJSONOrThrow(res)
return await getJSONOrNull(res)
},

async createResponse(url: string, data?: any, options?: ApiMethodOptions): Promise<Response> {
Expand Down Expand Up @@ -2172,7 +2169,7 @@ async function handleFetch(url: string, method: string, fetcher: () => Promise<R
const pathname = new URL(url, location.origin).pathname
posthog.capture('client_request_failure', { pathname, method, duration, status: response.status })

const data = await getJSONOrThrow(response)
const data = await getJSONOrNull(response)
throw new ApiError('Non-OK response', response.status, data)
}

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/lib/internalMetrics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api, { getJSONOrThrow } from 'lib/api'
import api, { getJSONOrNull } from 'lib/api'
import posthog from 'posthog-js'
import { getResponseBytes } from 'scenes/insights/utils'

Expand Down Expand Up @@ -53,7 +53,7 @@ export async function apiGetWithTimeToSeeDataTracking<T>(
const requestStartMs = performance.now()
try {
response = await api.getResponse(url)
responseData = await getJSONOrThrow(response)
responseData = await getJSONOrNull(response)
} catch (e) {
error = e
}
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/scenes/dashboard/dashboardLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from 'kea'
import { loaders } from 'kea-loaders'
import { router, urlToAction } from 'kea-router'
import api, { ApiMethodOptions, getJSONOrThrow } from 'lib/api'
import api, { ApiMethodOptions, getJSONOrNull } from 'lib/api'
import {
AUTO_REFRESH_DASHBOARD_THRESHOLD_HOURS,
DashboardPrivilegeLevel,
Expand Down Expand Up @@ -214,7 +214,7 @@ export const dashboardLogic = kea<dashboardLogicType>([
// :TODO: Send dashboardQueryId forward as well if refreshing
const apiUrl = values.apiUrl(refresh)
const dashboardResponse: Response = await api.getResponse(apiUrl)
const dashboard: DashboardType = await getJSONOrThrow(dashboardResponse)
const dashboard: DashboardType = await getJSONOrNull(dashboardResponse)

actions.setInitialLoadResponseBytes(getResponseBytes(dashboardResponse))

Expand Down Expand Up @@ -981,7 +981,7 @@ export const dashboardLogic = kea<dashboardLogicType>([
breakpoint()

const refreshedInsightResponse: Response = await api.getResponse(apiUrl, methodOptions)
const refreshedInsight: InsightModel = await getJSONOrThrow(refreshedInsightResponse)
const refreshedInsight: InsightModel = await getJSONOrNull(refreshedInsightResponse)
breakpoint()
updateExistingInsightState({ cachedInsight: insight, dashboardId, refreshedInsight })
dashboardsModel.actions.updateDashboardInsight(
Expand Down

0 comments on commit bca5d25

Please sign in to comment.