fix(core): remove unsafe type assertion suppressions in error utils (#19881)

Co-authored-by: David Pierce <davidapierce@google.com>
This commit is contained in:
Himanshu Kumar
2026-05-06 01:22:29 +05:30
committed by GitHub
parent f29eb9a569
commit d8f2a89865
2 changed files with 27 additions and 24 deletions
+3 -10
View File
@@ -280,16 +280,9 @@ function parseResponseData(error: GaxiosError): ResponseData | undefined {
export function isAuthenticationError(error: unknown): boolean {
// Check for MCP SDK errors with code property
// (SseError and StreamableHTTPError both have numeric 'code' property)
if (
error &&
typeof error === 'object' &&
'code' in error &&
typeof (error as { code: unknown }).code === 'number'
) {
// Safe access after check
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
const errorCode = (error as { code: number }).code;
if (errorCode === 401) {
if (error && typeof error === 'object' && 'code' in error) {
const errorCode: unknown = (error as Record<string, unknown>)['code'];
if (typeof errorCode === 'number' && errorCode === 401) {
return true;
}
}
+24 -14
View File
@@ -16,23 +16,33 @@ export interface ApiError {
}
export function isApiError(error: unknown): error is ApiError {
if (typeof error !== 'object' || error === null || !('error' in error)) {
return false;
}
const errorProp = (error as { error: unknown }).error;
if (typeof errorProp !== 'object' || errorProp === null) {
return false;
}
return (
typeof error === 'object' &&
error !== null &&
'error' in error &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
typeof (error as ApiError).error === 'object' &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
'message' in (error as ApiError).error
'code' in errorProp &&
typeof errorProp.code === 'number' &&
'message' in errorProp &&
typeof errorProp.message === 'string' &&
'status' in errorProp &&
typeof errorProp.status === 'string'
);
}
export function isStructuredError(error: unknown): error is StructuredError {
return (
typeof error === 'object' &&
error !== null &&
'message' in error &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
typeof (error as StructuredError).message === 'string'
);
if (typeof error !== 'object' || error === null || !('message' in error)) {
return false;
}
if (typeof error.message !== 'string') {
return false;
}
if ('status' in error && typeof error.status !== 'number') {
return false;
}
return true;
}