Skip to content

Commit 37cc4ce

Browse files
committed
move error message rewrite to source of errors
before "explore" and the graph panel did show the original (non rewritten) error message. currently we have two problems with that implementation: 1. the error message is translated, so this only works in english instances of checkmk 2. we duplicated the code between rest and web api. for 1. we plan to introduce a feature in the REST-API (but this will only solve the problem in `src/backend/rest.ts` not for the webapi!), we probably have to live with the code duplication.
1 parent 7aa1145 commit 37cc4ce

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

src/backend/rest.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ import {
1010
TimeRange,
1111
dateTime,
1212
} from '@grafana/data';
13-
import { BackendSrvRequest, FetchResponse, getBackendSrv } from '@grafana/runtime';
13+
import { BackendSrvRequest, FetchError, FetchResponse, getBackendSrv } from '@grafana/runtime';
1414
import { Aggregation, GraphType } from 'RequestSpec';
1515

1616
import { CmkQuery } from '../types';
1717
import { createCmkContext, updateQuery } from '../utils';
1818
import { Backend, DatasourceOptions } from './types';
1919

20+
type RestApiError = {
21+
detail: string;
22+
status: number;
23+
title: string;
24+
};
25+
2026
type RestApiGraphResponse = {
2127
time_range: {
2228
start: string;
@@ -118,7 +124,8 @@ export default class RestApiBackend implements Backend {
118124
try {
119125
result = await getBackendSrv().fetch<T>(request).toPromise();
120126
// eslint-disable-next-line @typescript-eslint/no-explicit-any
121-
} catch (error: any) {
127+
} catch (e) {
128+
const error = e as FetchError<RestApiError>;
122129
// grafana error handling is only showing status code and status message,
123130
// but we may have a more detailed error message
124131
if (error.status === 404) {
@@ -128,6 +135,12 @@ export default class RestApiBackend implements Backend {
128135
}
129136

130137
if (error.data && error.data.title && error.data.detail) {
138+
// TODO: error message is translated, see CMK-12886
139+
if (error.data.detail === 'Sorry, you cannot create combined graphs for more than 100 objects') {
140+
throw new Error(
141+
'Result size limit reached. Please add more filters to reduce the number of elements in the result.'
142+
);
143+
}
131144
throw new Error(`${error.data.title} ${error.data.detail}`);
132145
}
133146
throw error;

src/backend/web.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DataQueryRequest, DataQueryResponse, FieldType, MutableDataFrame } from '@grafana/data';
2-
import { BackendSrvRequest, FetchResponse, getBackendSrv } from '@grafana/runtime';
2+
import { BackendSrvRequest, FetchError, FetchResponse, getBackendSrv } from '@grafana/runtime';
33
import { defaults, get, isUndefined, zip } from 'lodash';
44

55
import { CmkQuery, defaultQuery } from '../types';
@@ -77,11 +77,13 @@ export default class WebApiBackend implements Backend {
7777
data: buildRequestBody(data),
7878
});
7979
}
80+
8081
async cmkRequest<T>(request: BackendSrvRequest): Promise<FetchResponse<WebApiResponse<T>>> {
8182
const result = await getBackendSrv()
8283
.fetch<WebApiResponse<T>>(request)
8384
.toPromise()
84-
.catch((error) => {
85+
.catch((e) => {
86+
const error = e as FetchError<unknown>;
8587
if (error.cancelled) {
8688
throw new Error(
8789
`API request was cancelled. This has either happened because no 'Access-Control-Allow-Origin' header is present, or because of a ssl protocol error. Make sure you are running at least Checkmk version 2.0.`
@@ -101,10 +103,17 @@ export default class WebApiBackend implements Backend {
101103
if (typeof result.data === 'string') {
102104
throw new Error(`${result.data}`);
103105
} else if (result.data.result_code !== 0) {
106+
// error handling in web api: error is not transported via http-status codes :-/
104107
let message = `${result.data}`;
105108
if (result.data.result !== undefined) {
106109
message = `${result.data.result}`;
107110
}
111+
// TODO: error message is translated, see CMK-12886
112+
if (message === 'Sorry, you cannot create combined graphs for more than 100 objects') {
113+
throw new Error(
114+
'Result size limit reached. Please add more filters to reduce the number of elements in the result.'
115+
);
116+
}
108117
throw new Error(message);
109118
} else {
110119
return result;

src/ui/components.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,7 @@ export const CheckMkGenericAsyncSelect = function <Value extends string | (strin
9696
} catch (e) {
9797
const error = e as Error;
9898
if (error && error.message) {
99-
if (error.message === 'Sorry, you cannot create combined graphs for more than 100 objects') {
100-
setAutocompleteError(
101-
'Result size limit reached. Please add more filters to reduce the number of elements in the result.'
102-
);
103-
} else {
104-
setAutocompleteError(error.message);
105-
}
99+
setAutocompleteError(error.message);
106100
}
107101
throw error;
108102
}

0 commit comments

Comments
 (0)