Skip to content

Commit

Permalink
Merge pull request #52 from SumoLogic-Labs/dwojtowicz-display-errors-…
Browse files Browse the repository at this point in the history
…from-multiple-queries

Display errors even if some data is returned
  • Loading branch information
dwojtowiczSumo authored Sep 5, 2023
2 parents 3b85e4e + 12de4c1 commit 9d1a7a2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
5 changes: 0 additions & 5 deletions src/datasource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -435,10 +434,6 @@ describe('placeholder test', () => {
],
response: [
mockResponse.response[0],
{
rowId: 'B',
results: [],
},
],
},
})
Expand Down
14 changes: 12 additions & 2 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ export class DataSource extends DataSourceApi<SumoQuery> 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);
Expand All @@ -233,10 +241,12 @@ export class DataSource extends DataSourceApi<SumoQuery> 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;
Expand Down

0 comments on commit 9d1a7a2

Please sign in to comment.