Alerting: Improve k8s error type (#98569)

* Add more checks to isApiMachineryError type guard

* Make the k8s error details property optional
This commit is contained in:
Konrad Lalik
2025-01-10 09:02:36 +01:00
committed by GitHub
parent e4b94fce8a
commit 557820df56
2 changed files with 6 additions and 7 deletions
@@ -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<ApiMachineryError> {
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;
}
@@ -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;