From 12de4c18ce8ab09f945f3629c37587ea897d7cf2 Mon Sep 17 00:00:00 2001 From: Dariusz Wojtowicz Date: Mon, 4 Sep 2023 11:41:38 +0200 Subject: [PATCH] Display errors even if some data is returned --- CHANGELOG.md | 6 ++++++ src/datasource.test.ts | 5 ----- src/datasource.ts | 14 ++++++++++++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 984daa6..e906c63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 1.1.1 + +### Fixes: +- Fix displaying error for invalid queries even if other returns data + + ## 1.1.0 Sumo Grafana Datasource Plugin ### Highlights: diff --git a/src/datasource.test.ts b/src/datasource.test.ts index a7d54a7..95e7599 100644 --- a/src/datasource.test.ts +++ b/src/datasource.test.ts @@ -417,7 +417,6 @@ describe('placeholder test', () => { it('should handle metrics warning for multiple queries', async () => { const mockedFetch = getBackendSrv().fetch as jest.Mock; - // const queryMockResponse(); const mockResponse = queryMockResponse(); mockedFetch.mockReturnValue( of({ @@ -435,10 +434,6 @@ describe('placeholder test', () => { ], response: [ mockResponse.response[0], - { - rowId: 'B', - results: [], - }, ], }, }) diff --git a/src/datasource.ts b/src/datasource.ts index 6022dea..2fafa59 100644 --- a/src/datasource.ts +++ b/src/datasource.ts @@ -221,6 +221,14 @@ export class DataSource extends DataSourceApi implements DataSourceWi const { data: { response, keyedErrors, error, errorKey, errorMessage }, } = res; + + const rowsWithNoData = []; + for (const query of queries) { + if (!response.some((rowResponse) => rowResponse.rowId === query.rowId)) { + rowsWithNoData.push({ rowId: query.rowId, results: [] }); + } + } + if (error && !response.length) { if (errorMessage) { throw new Error(errorMessage); @@ -233,10 +241,12 @@ export class DataSource extends DataSourceApi implements DataSourceWi const firstKeyedError = keyedErrors.find((keyedError) => keyedError.values?.type === 'error'); const firstWarningError = keyedErrors.find((keyedError) => keyedError.values?.type === 'warning'); - throw new Error(mapKeyedErrorMessage(firstKeyedError ?? firstWarningError)); + const errorOrWarning = firstKeyedError ?? firstWarningError; + const rowId = errorOrWarning?.values?.rowId; + throw new Error(mapKeyedErrorMessage(errorOrWarning, rowId)); } - const data = response.flatMap(({ rowId, results }) => { + const data = [...response, ...rowsWithNoData].flatMap(({ rowId, results }) => { const responseSpecificErrors = hasMultipleQueries ? keyedErrors.filter(({ values }) => values?.rowId === rowId) : keyedErrors;