UNPKG

@ratley/react-native-apple-foundation-models

Version:
55 lines 1.97 kB
const TEXT_GENERATION_ERROR_CODES = new Set([ "ERR_TEXT_GENERATION_UNSUPPORTED", "ERR_TEXT_PROMPT_INVALID", "ERR_TEXT_GENERATION_INVALID_ARGUMENT", "ERR_TEXT_GENERATION_CANCELED", "ERR_TEXT_GENERATION_TIMEOUT", "ERR_TEXT_GENERATION_RUNTIME", "ERR_TEXT_GENERATION_MODEL_UNAVAILABLE", ]); export class TextGenerationError extends Error { code; nativeCode; nativeDomain; cause; constructor({ code, message, cause, nativeCode, nativeDomain, }) { super(message); this.name = "TextGenerationError"; this.code = code; this.cause = cause; this.nativeCode = nativeCode; this.nativeDomain = nativeDomain; } } function isNativeErrorLike(error) { if (typeof error !== "object" || error == null) { return false; } const candidate = error; return (typeof candidate.code === "string" && typeof candidate.message === "string"); } export function toTextGenerationError(error) { if (error instanceof TextGenerationError) { return error; } if (isNativeErrorLike(error) && TEXT_GENERATION_ERROR_CODES.has(error.code)) { return new TextGenerationError({ code: error.code, message: String(error.message ?? "Text generation failed."), cause: error.cause, nativeCode: typeof error.nativeCode === "number" ? error.nativeCode : undefined, nativeDomain: typeof error.nativeDomain === "string" ? error.nativeDomain : undefined, }); } return new TextGenerationError({ code: "ERR_TEXT_GENERATION_RUNTIME", message: (isNativeErrorLike(error) && error.message) || (error instanceof Error ? error.message : "Text generation failed."), cause: error instanceof Error ? error : undefined, }); } export function isTextGenerationError(error) { return error instanceof TextGenerationError; } //# sourceMappingURL=errors.js.map