apierror

A structured error type for failures returned by Google's Generative AI service. It records details such as the error message, status, and code from a failed HTTP request.

In plain words
What is it for?
Use it when catching and handling failed Google Generative AI calls, logging what went wrong, or choosing a response based on the service's error status.
Why use it?
It helps distinguish service failures—such as an invalid key, bad request, missing resource, or outage—from network or ordinary program errors.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/altaidevorg/rules-for-ai/apierror
Clone the repo
git clone --depth 1 https://github.com/altaidevorg/rules-for-ai
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 3,465 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.03465
Opus 5 $0.00000 $0.01733
Sonnet 5 $0.00000 $0.00693
Haiku 4.5 $0.00000 $0.00347

Measured 2d ago against content hash ee0cf5d4b332, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

apierror scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

examples/google-genai/apierror.mdc · 259 lines

How it starts

The opening of the file, as written. The whole thing — 259 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Chapter 9: APIError

In Chapter 8: BaseApiClient, we examined the core component responsible for handling HTTP communication with the Google Generative AI APIs. We saw that BaseApiClient sends requests and receives responses. But what happens when the API service itself encounters an error and returns a non-successful HTTP status code (like 400 Bad Request or 500 Internal Server Error)? This chapter introduces APIError, the structured exception class designed specifically for handling these API-level errors.

Motivation and Use Case

When interacting with any external service, robust error handling is critical. Network issues, invalid inputs, temporary service outages, or permission problems can all lead to failed API calls. Simply relying on generic Python exceptions (like httpx.HTTPStatusError) wouldn't provide enough specific information about why the API call failed from the service's perspective. Developers need a way to:

  1. Distinguish API Service Errors: Differentiate errors originating from the Google GenAI service (e.g., invalid API key, malformed request, resource not found) from general network or Python runtime errors.
  2. Access Structured Error Details: Retrieve specific error information provided by the API, such as an error code, a descriptive message, and a status identifier (e.g., 'INVALID_ARGUMENT', 'PERMISSION_DENIED').
  3. Implement Targeted Error Handling: Write try...except blocks that specifically catch API-related issues and potentially implement different logic based on the error details (e.g., retrying on transient server errors, logging specific client errors).

APIError and its subclasses (ClientError, ServerError) fulfill these needs by providing a dedicated exception hierarchy for errors returned by the Google GenAI API.

Central Use Case: Imagine you make an API call with an invalid parameter, such as referencing a non-existent model. The API will return a 4xx error. Your code should be able to catch this specific type of error and extract the details provided by the API.

Read the full file on GitHub · 259 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 259 lines · 0 tokens per session scan A ee0cf5d4b332

Subscribe to this mod's changes

apierror is a cursor rule published in the GitHub repository altaidevorg/rules-for-ai (2 stars, last pushed 1y ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 3,465 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.