mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: update response text assertion (#43291)
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
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 { getDatasourceAPIId, GRAFANA_RULES_SOURCE_NAME } from '../utils/datasource';
|
||||
import { RULER_NOT_SUPPORTED_MSG } from '../utils/constants';
|
||||
|
||||
interface ErrorResponseMessage {
|
||||
message?: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
// upsert a rule group. use this to update rules
|
||||
export async function setRulerRuleGroup(
|
||||
dataSourceName: string,
|
||||
@@ -97,28 +102,47 @@ async function rulerGetRequest<T>(url: string, empty: T, params?: Record<string,
|
||||
})
|
||||
);
|
||||
return response.data;
|
||||
} catch (e) {
|
||||
if (e?.status === 404) {
|
||||
if (e?.data?.message?.includes('group does not exist') || e?.data?.message?.includes('no rule groups found')) {
|
||||
} catch (error) {
|
||||
if (!isResponseError(error)) {
|
||||
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;
|
||||
}
|
||||
// any other 404 should throw an exception
|
||||
throw new Error('404 from rules config endpoint. Perhaps ruler API is not enabled?');
|
||||
} else if (
|
||||
e?.status === 500 &&
|
||||
e?.data?.message?.includes('unexpected content type from upstream. expected YAML, got text/html')
|
||||
) {
|
||||
} else if (rulerNotSupported) {
|
||||
// assert if the endoint is not supported at all
|
||||
throw {
|
||||
...e,
|
||||
...error,
|
||||
data: {
|
||||
...e?.data,
|
||||
...error.data,
|
||||
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> {
|
||||
await lastValueFrom(
|
||||
getBackendSrv().fetch<unknown>({
|
||||
|
||||
Reference in New Issue
Block a user