refactor: centralized AI error handling into a dedicated module
- Migrated 288 lines of scattered error classification logic from `utils/error-id.ts` into a cohesive `packages/ai/src/error/` module with 13 specialized submodules covering flags, classes, OAuth, providers, rate-limiting, and finalization. - Replaced 100+ generic `Error` throws across 60+ provider and registry files with semantic `AIError.*` classes (e.g., `AIError.MissingApiKeyError`, `AIError.OAuthError`, `AIError.ProviderResponseError`), improving error diagnostics and retry logic. - Consolidated error utility imports from `pi-utils` and scattered classification functions into a single `AIError` namespace, reducing coupling and simplifying error handling across all packages.
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
* for each target.
|
||||
*/
|
||||
import { logger } from "@oh-my-pi/pi-utils";
|
||||
import * as AIError from "../../error";
|
||||
import { dereferenceJsonSchema } from "./dereference";
|
||||
import { upgradeJsonSchemaTo202012 } from "./draft";
|
||||
import { areJsonValuesEqual, mergeCompatibleEnumSchemas, mergePropertySchemas } from "./equality";
|
||||
@@ -1711,7 +1712,7 @@ export function enforceStrictSchema(
|
||||
cache: WeakMap<Record<string, unknown>, Record<string, unknown>> = new WeakMap(),
|
||||
): Record<string, unknown> {
|
||||
if (!enter(schema)) {
|
||||
throw new Error("Schema contains a circular object graph — cannot enforce strict mode");
|
||||
throw new AIError.ValidationError("Schema contains a circular object graph — cannot enforce strict mode");
|
||||
}
|
||||
try {
|
||||
const cached = cache.get(schema);
|
||||
@@ -1857,7 +1858,7 @@ function enforceStrictSchemaBody(
|
||||
!COMBINATOR_KEYS.some(key => Array.isArray(result[key])) &&
|
||||
!isJsonObject(result.not)
|
||||
) {
|
||||
throw new Error("Schema node has no type, combinator, or $ref — cannot enforce strict mode");
|
||||
throw new AIError.ValidationError("Schema node has no type, combinator, or $ref — cannot enforce strict mode");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user