From 22ad487c4be215c23ef4594aacbfad0920ef2466 Mon Sep 17 00:00:00 2001 From: Sonia Aguilar <33540275+soniaAguilarPeiron@users.noreply.github.com> Date: Thu, 22 Dec 2022 14:30:27 +0100 Subject: [PATCH] =?UTF-8?q?Use=20queryFn=20instead=20of=20query=20to=20be?= =?UTF-8?q?=20able=20to=20catch=20errors=20in=20onCall=20req=E2=80=A6=20(#?= =?UTF-8?q?60685)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use queryFn instead of query to be able to catch errors in onCall request --- .../alerting/unified/api/onCallApi.ts | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/public/app/features/alerting/unified/api/onCallApi.ts b/public/app/features/alerting/unified/api/onCallApi.ts index 87cc97b33d0..259adfdb517 100644 --- a/public/app/features/alerting/unified/api/onCallApi.ts +++ b/public/app/features/alerting/unified/api/onCallApi.ts @@ -1,3 +1,7 @@ +import { lastValueFrom } from 'rxjs'; + +import { getBackendSrv } from '@grafana/runtime'; + import { alertingApi } from './alertingApi'; export interface OnCallIntegration { integration_url: string; @@ -8,13 +12,26 @@ export type OnCallIntegrationsUrls = string[]; export const onCallApi = alertingApi.injectEndpoints({ endpoints: (build) => ({ getOnCallIntegrations: build.query({ - query: () => ({ - headers: {}, - url: '/api/plugin-proxy/grafana-oncall-app/api/internal/v1/alert_receive_channels/', - }), + queryFn: async () => { + const integrations = await fetchOnCallIntegrations(); + return { data: integrations }; + }, providesTags: ['AlertmanagerChoice'], - transformResponse: (response: OnCallIntegrationsResponse) => response.map((result) => result.integration_url), }), }), }); +export async function fetchOnCallIntegrations(): Promise { + try { + const response = await lastValueFrom( + getBackendSrv().fetch({ + url: '/api/plugin-proxy/grafana-oncall-app/api/internal/v1/alert_receive_channels/', + showErrorAlert: false, + showSuccessAlert: false, + }) + ); + return response.data.map((result) => result.integration_url); + } catch (error) { + return []; + } +} export const { useGetOnCallIntegrationsQuery } = onCallApi;