From 557820df56ad349d06f9b8ffa37b2dfda6477e79 Mon Sep 17 00:00:00 2001 From: Konrad Lalik Date: Fri, 10 Jan 2025 09:02:36 +0100 Subject: [PATCH] Alerting: Improve k8s error type (#98569) * Add more checks to isApiMachineryError type guard * Make the k8s error details property optional --- public/app/features/alerting/unified/utils/k8s/errors.ts | 6 +----- public/app/features/alerting/unified/utils/misc.ts | 7 +++++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/public/app/features/alerting/unified/utils/k8s/errors.ts b/public/app/features/alerting/unified/utils/k8s/errors.ts index cc0da5edcfd..856b2c66eee 100644 --- a/public/app/features/alerting/unified/utils/k8s/errors.ts +++ b/public/app/features/alerting/unified/utils/k8s/errors.ts @@ -23,7 +23,7 @@ export type ApiMachineryError = { kind: 'Status'; apiVersion: string; code: number; - details: { + details?: { uid: string; name?: string; group?: string; @@ -40,7 +40,3 @@ export type ApiMachineryError = { export function isApiMachineryError(error: unknown): error is FetchError { return isFetchError(error) && get(error.data, 'kind') === 'Status' && get(error.data, 'status') === 'Failure'; } - -export function matchesApiMachineryError(error: unknown, uid: string) { - return isApiMachineryError(error) && error.data.details.uid === uid; -} diff --git a/public/app/features/alerting/unified/utils/misc.ts b/public/app/features/alerting/unified/utils/misc.ts index d2a12957938..c136c2aa883 100644 --- a/public/app/features/alerting/unified/utils/misc.ts +++ b/public/app/features/alerting/unified/utils/misc.ts @@ -260,7 +260,10 @@ export function isErrorLike(error: unknown): error is Error { } export function getErrorCode(error: Error): unknown { - return isApiMachineryError(error) ? error.data.details.uid : error.cause; + if (isApiMachineryError(error) && error.data.details) { + return error.data.details.uid; + } + return error.cause; } /* this function will check if the error passed as the first argument contains an error code */ @@ -275,7 +278,7 @@ export function isErrorMatchingCode(error: Error | undefined, code: SupportedErr export function stringifyErrorLike(error: unknown): string { const fetchError = isFetchError(error); if (fetchError) { - if (isApiMachineryError(error)) { + if (isApiMachineryError(error) && error.data.details) { const message = getErrorMessageFromCode(error.data.details.uid); if (message) { return message;