Skip to content

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

Merged
DavidAPierce merged 3 commits intogoogle-gemini:mainfrom
himanshu748:fix/remove-unsafe-type-assertions-error-utils
May 5, 2026
Merged

fix(core): remove unsafe type assertion suppressions in error utils#19881
DavidAPierce merged 3 commits intogoogle-gemini:mainfrom
himanshu748:fix/remove-unsafe-type-assertions-error-utils

Conversation

@himanshu748
Copy link
Copy Markdown
Contributor

@himanshu748 himanshu748 commented Feb 22, 2026

Related to #19708

Summary

Remove 4 eslint-disable comments for @typescript-eslint/no-unsafe-type-assertion in error utility files by replacing unsafe as casts with proper runtime type narrowing.

Changes

packages/core/src/utils/quotaErrorDetection.ts (3 suppressions removed):

  • isApiError(): Replaced (error as ApiError).error with early-return pattern and intermediate errorProp variable
  • isStructuredError(): Replaced (error as StructuredError).message with (error as { message: unknown }).message

packages/core/src/utils/errors.ts (1 suppression removed):

  • isAuthenticationError(): Used Record<string, unknown> index access with explicit unknown typing for safe narrowing

Testing

  • npx eslint passes on both files (0 errors)
  • npm run test passes (all tests green)
  • Pre-commit hooks (Prettier + ESLint) pass

@himanshu748 himanshu748 requested a review from a team as a code owner February 22, 2026 06:09
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @himanshu748, 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 focuses on enhancing the type safety of error utility functions by eliminating unsafe type assertions. The changes involve replacing direct as casts with more robust runtime type narrowing checks, which in turn allowed for the removal of several @typescript-eslint/no-unsafe-type-assertion ESLint suppressions. This improves code quality and maintainability by ensuring more reliable type inference during error object processing.

Highlights

  • ESLint Suppression Removal: Removed four instances of @typescript-eslint/no-unsafe-type-assertion eslint-disable comments across error utility files.
  • Improved Type Safety: Replaced unsafe as type assertions with proper runtime type narrowing techniques to ensure safer error handling.
  • Refactored isApiError: The isApiError function in quotaErrorDetection.ts was updated to use an early-return pattern and an intermediate variable for safer property access.
  • Refined isStructuredError: The isStructuredError function in quotaErrorDetection.ts now uses a more specific type assertion for the message property.
  • Enhanced isAuthenticationError: The isAuthenticationError function in errors.ts was refactored to use Record<string, unknown> for index access, improving type safety.
Changelog
  • packages/core/src/utils/errors.ts
    • Refactored isAuthenticationError to use Record<string, unknown> for safer type narrowing of the code property, removing an eslint-disable comment.
  • packages/core/src/utils/quotaErrorDetection.ts
    • Updated isApiError to use an early-return pattern and an intermediate errorProp variable for safer access to the error property, eliminating two eslint-disable comments.
    • Modified isStructuredError to use (error as { message: unknown }).message for type narrowing, removing an eslint-disable comment.
Activity
  • The author confirmed that npx eslint passes on both modified files with 0 errors.
  • The author verified that npm run test passes, with all tests green.
  • Pre-commit hooks (Prettier + ESLint) were confirmed to pass.
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
Copy Markdown
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 successfully removes several eslint-disable comments for unsafe type assertions by refactoring the type-checking logic in error utility functions. The changes in errors.ts and quotaErrorDetection.ts improve code safety and readability.

I've added a couple of suggestions to further strengthen the type guards in quotaErrorDetection.ts. The current implementations for isApiError and isStructuredError are a bit weak and don't fully validate against their respective interfaces, which could lead to runtime issues. The suggested changes make these type guards more robust.

Comment thread packages/core/src/utils/quotaErrorDetection.ts
@gemini-cli gemini-cli Bot added area/platform Issues related to Build infra, Release mgmt, Testing, Eval infra, Capacity, Quota mgmt help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! labels Feb 22, 2026
@himanshu748
Copy link
Copy Markdown
Contributor Author

Enhanced isApiError and isStructuredError type guards with more robust validation of nested fields and types.

himanshu748 added a commit to himanshu748/gemini-cli that referenced this pull request Feb 27, 2026
Reverts errors.ts and quotaErrorDetection.ts to main branch versions.
These type guard improvements belong in PR google-gemini#19881 which is dedicated
to error utility type safety.
Remove 4 eslint-disable comments for @typescript-eslint/no-unsafe-type-assertion
in quotaErrorDetection.ts and errors.ts by replacing unsafe `as` casts with
proper runtime type narrowing using `in` operator checks and intermediate
typed variables.

Part of google-gemini#19440
isApiError now validates code (number), message (string), and status
(string) properties instead of only checking for message existence.
isStructuredError now validates status is a number when present.
Both use 'in' operator checks to avoid unsafe type assertions.
@himanshu748 himanshu748 force-pushed the fix/remove-unsafe-type-assertions-error-utils branch from 836600f to df4b407 Compare February 27, 2026 16:00
himanshu748 added a commit to himanshu748/gemini-cli that referenced this pull request Feb 27, 2026
Reverts errors.ts and quotaErrorDetection.ts to main branch versions.
These type guard improvements belong in PR google-gemini#19881 which is dedicated
to error utility type safety.
Copy link
Copy Markdown
Contributor

@DavidAPierce DavidAPierce left a comment

Choose a reason for hiding this comment

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

LGTM.

Thank you for your patience and contribution!

@DavidAPierce DavidAPierce enabled auto-merge May 5, 2026 19:42
@DavidAPierce DavidAPierce added this pull request to the merge queue May 5, 2026
Merged via the queue into google-gemini:main with commit d8f2a89 May 5, 2026
27 checks passed
kimjune01 pushed a commit to kimjune01/gemini-cli-claude that referenced this pull request May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/platform Issues related to Build infra, Release mgmt, Testing, Eval infra, Capacity, Quota mgmt help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants