From 42024a9ab3024e9043b91527791103edae63a971 Mon Sep 17 00:00:00 2001 From: Dominik Broj Date: Tue, 9 Apr 2024 12:36:15 +0200 Subject: [PATCH] Fix incident error handling (#4185) # What this PR does Throw full fetch response to allow existing error handlers to work properly ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes. --- .../PageErrorHandlingWrapper.helpers.tsx | 2 +- .../src/network/oncall-api/http-client.test.ts | 14 +++++++------- .../src/network/oncall-api/http-client.ts | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/grafana-plugin/src/components/PageErrorHandlingWrapper/PageErrorHandlingWrapper.helpers.tsx b/grafana-plugin/src/components/PageErrorHandlingWrapper/PageErrorHandlingWrapper.helpers.tsx index 7653be75..a9a4432a 100644 --- a/grafana-plugin/src/components/PageErrorHandlingWrapper/PageErrorHandlingWrapper.helpers.tsx +++ b/grafana-plugin/src/components/PageErrorHandlingWrapper/PageErrorHandlingWrapper.helpers.tsx @@ -4,7 +4,7 @@ export function initErrorDataState(): Partial { return { isUnknownError: false, isWrongTeamError: false, wrongTeamNoPermissions: false }; } -export function getWrongTeamResponseInfo({ response }): Partial { +export function getWrongTeamResponseInfo(response): Partial { if (response) { if (response.status === 404) { return { isNotFoundError: true }; diff --git a/grafana-plugin/src/network/oncall-api/http-client.test.ts b/grafana-plugin/src/network/oncall-api/http-client.test.ts index 83d659f5..1d1fd31a 100644 --- a/grafana-plugin/src/network/oncall-api/http-client.test.ts +++ b/grafana-plugin/src/network/oncall-api/http-client.test.ts @@ -28,7 +28,7 @@ const REQUEST_CONFIG = { }; const URL = 'https://someurl.com'; const SUCCESSFUL_RESPONSE_MOCK = { ok: true }; -const ERROR_MOCK = 'error'; +const FAILING_RESPONSE_MOCK = { ok: false, json: () => 'ERROR' }; const customFetch = getCustomFetchFn({ withGlobalErrorHandler: true }); describe('customFetch', () => { @@ -53,10 +53,10 @@ describe('customFetch', () => { describe('if response is not successful', () => { it('should push event and error to faro', async () => { (FaroHelper.faro.api.getOTEL as unknown as jest.Mock).mockReturnValueOnce(undefined); - fetchMock.mockResolvedValueOnce({ ok: false, json: () => ERROR_MOCK }); - await expect(customFetch(URL, REQUEST_CONFIG)).rejects.toEqual(ERROR_MOCK); + fetchMock.mockResolvedValueOnce(FAILING_RESPONSE_MOCK); + await expect(customFetch(URL, REQUEST_CONFIG)).rejects.toEqual(FAILING_RESPONSE_MOCK); expect(FaroHelper.faro.api.pushEvent).toHaveBeenCalledWith('Request failed', { url: URL }); - expect(FaroHelper.faro.api.pushError).toHaveBeenCalledWith(ERROR_MOCK); + expect(FaroHelper.faro.api.pushError).toHaveBeenCalledWith(FAILING_RESPONSE_MOCK.json()); }); }); }); @@ -108,10 +108,10 @@ describe('customFetch', () => { describe('if response is not successful', () => { it('should reject Promise, push event to faro, set span status to error and end span', async () => { - fetchMock.mockResolvedValueOnce({ ok: false, json: () => ERROR_MOCK }); - await expect(customFetch(URL, REQUEST_CONFIG)).rejects.toEqual(ERROR_MOCK); + fetchMock.mockResolvedValueOnce(FAILING_RESPONSE_MOCK); + await expect(customFetch(URL, REQUEST_CONFIG)).rejects.toEqual(FAILING_RESPONSE_MOCK); expect(FaroHelper.faro.api.pushEvent).toHaveBeenCalledWith('Request failed', { url: URL }); - expect(FaroHelper.faro.api.pushError).toHaveBeenCalledWith(ERROR_MOCK); + expect(FaroHelper.faro.api.pushError).toHaveBeenCalledWith(FAILING_RESPONSE_MOCK.json()); expect(spanEndMock).toHaveBeenCalledTimes(1); }); }); diff --git a/grafana-plugin/src/network/oncall-api/http-client.ts b/grafana-plugin/src/network/oncall-api/http-client.ts index ef0d630e..15a983a9 100644 --- a/grafana-plugin/src/network/oncall-api/http-client.ts +++ b/grafana-plugin/src/network/oncall-api/http-client.ts @@ -62,7 +62,7 @@ export const getCustomFetchFn = if (withGlobalErrorHandler) { showApiError(res); } - reject(errorData); + reject(res); } }); }); @@ -78,7 +78,7 @@ export const getCustomFetchFn = if (withGlobalErrorHandler) { showApiError(res); } - throw errorData; + throw res; } } };