Alerting: update response text assertion (#43291)
This commit is contained in:
@@ -1,10 +1,15 @@
|
|||||||
import { lastValueFrom } from 'rxjs';
|
import { lastValueFrom } from 'rxjs';
|
||||||
import { getBackendSrv } from '@grafana/runtime';
|
import { FetchResponse, getBackendSrv } from '@grafana/runtime';
|
||||||
|
|
||||||
import { PostableRulerRuleGroupDTO, RulerRuleGroupDTO, RulerRulesConfigDTO } from 'app/types/unified-alerting-dto';
|
import { PostableRulerRuleGroupDTO, RulerRuleGroupDTO, RulerRulesConfigDTO } from 'app/types/unified-alerting-dto';
|
||||||
import { getDatasourceAPIId, GRAFANA_RULES_SOURCE_NAME } from '../utils/datasource';
|
import { getDatasourceAPIId, GRAFANA_RULES_SOURCE_NAME } from '../utils/datasource';
|
||||||
import { RULER_NOT_SUPPORTED_MSG } from '../utils/constants';
|
import { RULER_NOT_SUPPORTED_MSG } from '../utils/constants';
|
||||||
|
|
||||||
|
interface ErrorResponseMessage {
|
||||||
|
message?: string;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
// upsert a rule group. use this to update rules
|
// upsert a rule group. use this to update rules
|
||||||
export async function setRulerRuleGroup(
|
export async function setRulerRuleGroup(
|
||||||
dataSourceName: string,
|
dataSourceName: string,
|
||||||
@@ -97,28 +102,47 @@ async function rulerGetRequest<T>(url: string, empty: T, params?: Record<string,
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (e) {
|
} catch (error) {
|
||||||
if (e?.status === 404) {
|
if (!isResponseError(error)) {
|
||||||
if (e?.data?.message?.includes('group does not exist') || e?.data?.message?.includes('no rule groups found')) {
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
const notFoundError = error.status === 404;
|
||||||
|
const rulerNotSupported =
|
||||||
|
error.status === 500 &&
|
||||||
|
error.data.error?.includes('unexpected content type from upstream. expected YAML, got text/html');
|
||||||
|
|
||||||
|
if (notFoundError) {
|
||||||
|
// the endpoint will return 404 but confirm that it's a Cortex endpoint
|
||||||
|
if (isCortexErrorResponse(error)) {
|
||||||
return empty;
|
return empty;
|
||||||
}
|
}
|
||||||
|
// any other 404 should throw an exception
|
||||||
throw new Error('404 from rules config endpoint. Perhaps ruler API is not enabled?');
|
throw new Error('404 from rules config endpoint. Perhaps ruler API is not enabled?');
|
||||||
} else if (
|
} else if (rulerNotSupported) {
|
||||||
e?.status === 500 &&
|
// assert if the endoint is not supported at all
|
||||||
e?.data?.message?.includes('unexpected content type from upstream. expected YAML, got text/html')
|
|
||||||
) {
|
|
||||||
throw {
|
throw {
|
||||||
...e,
|
...error,
|
||||||
data: {
|
data: {
|
||||||
...e?.data,
|
...error.data,
|
||||||
message: RULER_NOT_SUPPORTED_MSG,
|
message: RULER_NOT_SUPPORTED_MSG,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
throw e;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isResponseError(error: unknown): error is FetchResponse<ErrorResponseMessage> {
|
||||||
|
const hasErrorMessage = (error as FetchResponse<ErrorResponseMessage>).data != null;
|
||||||
|
const hasErrorCode = Number.isFinite((error as FetchResponse<ErrorResponseMessage>).status);
|
||||||
|
return hasErrorCode && hasErrorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isCortexErrorResponse(error: FetchResponse<ErrorResponseMessage>) {
|
||||||
|
return error.data.error?.includes('group does not exist') || error.data.error?.includes('no rule groups found');
|
||||||
|
}
|
||||||
|
|
||||||
export async function deleteNamespace(dataSourceName: string, namespace: string): Promise<void> {
|
export async function deleteNamespace(dataSourceName: string, namespace: string): Promise<void> {
|
||||||
await lastValueFrom(
|
await lastValueFrom(
|
||||||
getBackendSrv().fetch<unknown>({
|
getBackendSrv().fetch<unknown>({
|
||||||
|
|||||||
Reference in New Issue
Block a user