Skip to content

Comments

refactor(core): remove unsafe type assertions in error utils (Phase 1.1)#19750

Merged
mattKorwel merged 2 commits intomainfrom
fix-unsafe-assertions-phase-1-1
Feb 21, 2026
Merged

refactor(core): remove unsafe type assertions in error utils (Phase 1.1)#19750
mattKorwel merged 2 commits intomainfrom
fix-unsafe-assertions-phase-1-1

Conversation

@mattKorwel
Copy link
Collaborator

Summary

Disallow and remove unsafe type assertions in core error utilities. This replaces brittle as Type casts with runtime type guards to improve system reliability and crash reporting.

Details

  • Introduced isGaxiosError, isResponseData, and isErrorShape type guards in errors.ts and googleErrors.ts.
  • Refactored toFriendlyError and parseGoogleApiError to use these guards before accessing properties.
  • Added targeted linter suppressions only where TypeScript requires a cast to narrow the type after a manual property check.
  • This is part of Phase 1.1 of the systemic type safety cleanup.

Related Issues

Fixes #19709

How to Validate

  1. Run unit tests for error utilities:
    npm test -w @google/gemini-cli-core -- src/utils/errors.test.ts src/utils/googleErrors.test.ts
  2. All 34 tests should pass.
  3. Run lint to ensure no new unsafe assertions were introduced without suppression:
    npm run lint -w @google/gemini-cli-core

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
    • Windows
    • Linux
      • npm run

Replaces 'as Type' assertions with runtime type guards (isGaxiosError, isResponseData, isErrorShape) and targeted suppressions where narrowing is required in errors.ts and googleErrors.ts.

Fixes #19709
@mattKorwel mattKorwel requested a review from a team as a code owner February 20, 2026 22:36
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @mattKorwel, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the robustness of error handling within the core utilities by replacing potentially brittle as Type type assertions with explicit runtime type guards. This change ensures that error objects are structurally validated before their properties are accessed, leading to more reliable crash reporting and overall system stability.

Highlights

  • Replaced unsafe type assertions: Eliminated brittle as Type casts in core error utilities to improve reliability.
  • Introduced runtime type guards: Added isGaxiosError, isResponseData, and isErrorShape functions for robust type checking at runtime.
  • Refactored error handling functions: Updated toFriendlyError and parseGoogleApiError to leverage the newly introduced type guards.
  • Applied targeted linter suppressions: Ensured necessary type narrowing after manual property checks with appropriate linter suppressions.
  • Advanced type safety cleanup: This change is part of Phase 1.1 of a systemic effort to enhance type safety across the codebase.
Changelog
  • packages/core/src/utils/errors.ts
    • Added isGaxiosError function to safely check for Gaxios error structure.
    • Added isResponseData function to validate response data objects.
    • Updated toFriendlyError to utilize the new isGaxiosError type guard.
    • Modified parseResponseData to use isResponseData for safer data parsing.
    • Enhanced isAuthenticationError with an explicit type check for the code property.
  • packages/core/src/utils/googleErrors.ts
    • Introduced a structural check for @type property before casting GoogleApiErrorDetail in parseGoogleApiError.
    • Added isErrorShape function to verify generic error object structures.
    • Refactored fromGaxiosError to apply isErrorShape when extracting outerError.
    • Updated fromApiError to use isErrorShape for validating outerError.
Activity
  • Unit tests for error utilities were run and passed.
  • Linting was performed to ensure no new unsafe assertions were introduced without suppression.
  • The change was validated on Linux.
  • No specific review comments or discussions were provided in the context.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a solid step towards improving type safety in the error handling utilities by replacing unsafe type assertions with type guards. The introduction of isGaxiosError, isResponseData, and isErrorShape is a good approach. However, I've identified a couple of critical issues in the new type guard implementations where they are either not fully safe or still rely on unsafe assertions, which undermines the goal of this refactoring. My review comments provide suggestions to make these guards more robust and truly type-safe.

Comment on lines 13 to 21
function isGaxiosError(error: unknown): error is GaxiosError {
return (
typeof error === 'object' &&
error !== null &&
'response' in error &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
typeof (error as GaxiosError).response === 'object'
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The type guard isGaxiosError is not safe because typeof null is 'object'. If error.response is null, this check will incorrectly pass. The GaxiosError interface expects response to be an object with a data property or undefined, but not null. Additionally, the type assertion as GaxiosError is unsafe and can be avoided, which aligns with the goal of this PR.

Suggested change
function isGaxiosError(error: unknown): error is GaxiosError {
return (
typeof error === 'object' &&
error !== null &&
'response' in error &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
typeof (error as GaxiosError).response === 'object'
);
}
function isGaxiosError(error: unknown): error is GaxiosError {
return (
typeof error === 'object' &&
error !== null &&
'response' in error &&
typeof (error as { response: unknown }).response === 'object' &&
(error as { response: unknown }).response !== null
);
}

Comment on lines 230 to 239
function isErrorShape(obj: unknown): obj is ErrorShape {
return (
typeof obj === 'object' &&
obj !== null &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
(('message' in obj && typeof (obj as ErrorShape).message === 'string') ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
('code' in obj && typeof (obj as ErrorShape).code === 'number'))
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The type guard isErrorShape uses an unsafe type assertion as ErrorShape which this pull request aims to remove. The assertions are suppressed with eslint comments, but they can be avoided altogether for a safer implementation.

You can make this type guard safer by using a more constrained type for the cast, based on what has already been verified, which also allows removing the eslint-disable comments.

function isErrorShape(obj: unknown): obj is ErrorShape {
  return (
    typeof obj === 'object' &&
    obj !== null &&
    (('message' in obj &&
      typeof (obj as { message: unknown }).message === 'string') ||
      ('code' in obj && typeof (obj as { code: unknown }).code === 'number'))
  );
}

@github-actions
Copy link

github-actions bot commented Feb 20, 2026

Size Change: +1.2 kB (0%)

Total Size: 25.2 MB

Filename Size Change
./bundle/gemini.js 25.2 MB +1.2 kB (0%)
ℹ️ View Unchanged
Filename Size
./bundle/sandbox-macos-permissive-open.sb 890 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB
./bundle/sandbox-macos-strict-open.sb 4.82 kB
./bundle/sandbox-macos-strict-proxied.sb 5.02 kB

compressed-size-action

@gemini-cli gemini-cli bot added the area/core Issues related to User Interface, OS Support, Core Functionality label Feb 20, 2026
@mattKorwel
Copy link
Collaborator Author

Addressed critical code review feedback:

  • Improved type guard with a check for and replaced unsafe cast with a more constrained type check.
  • Refactored to eliminate unsafe type assertions, using and for safer narrowing.
  • Verified all 4987 core tests pass and linting is clean.

@mattKorwel mattKorwel added this pull request to the merge queue Feb 21, 2026
Merged via the queue into main with commit 0921857 Feb 21, 2026
27 checks passed
@mattKorwel mattKorwel deleted the fix-unsafe-assertions-phase-1-1 branch February 21, 2026 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix unsafe type assertions in Core Utilities (Phase 1.1: Error Handling)

2 participants