Skip to content

TryPatch โ€‹

@ssandir/trypatch

Production-ready error recovery. When a method fails, @trypatch investigates the error immediately at runtime and returns the value the method should have returned, by running your tools or having the AI build it. Sensitive data stays local. If no correct value can be produced, the method rethrows its original error. Fully customizable behavior.

  • ๐Ÿ” On-the-fly recovery: the AI investigates the failed call and returns its value. How recovery works
  • ๐Ÿ›ก๏ธ Schema-checked values: a value the AI builds is validated against your resultSchema (Zod or JSON Schema). Schemas
  • ๐Ÿ” Redaction: secrets are replaced with placeholders before anything leaves your process. Redaction
  • ๐Ÿ”ง Tools: investigation tools to look around, result tools to recover (retries, fallbacks, cache reads). Tools, MCP servers
  • ๐Ÿงพ No silent fallbacks: without a correct value, callers get the original error. How recovery works
  • ๐Ÿค– Providers: OpenAI, Claude, any OpenAI-compatible endpoint, or Cursor Cloud Agents. Providers

Install โ€‹

bash
npm install @ssandir/trypatch zod

Requires Node.js 22.12 or later. Zod schemas work with zod ^3.25.76 or ^4.1.8, imported from zod/v4. Schemas

Quick start โ€‹

typescript
import { z } from 'zod/v4'
import { trypatch } from '@ssandir/trypatch'

export const QuoteSchema = z.object({
    price: z.number().describe('Price in the smallest currency unit, e.g. cents'),
    currency: z.string().describe('ISO 4217 currency code'),
})
export type Quote = z.infer<typeof QuoteSchema>

export class PricingService {
    @trypatch({
        aiInvestigation: {
            resultSchema: QuoteSchema,
            investigationProvider: {
                provider: 'openai',
                apiKey: process.env.OPENAI_API_KEY!,
            },
        },
    })
    async getQuote (productId: string): Promise<Quote> {
        const response = await fetch(`https://pricing.example.com/products/${productId}/quote`)
        if (!response.ok) throw new Error(`Pricing API returned ${response.status}`)
        // Throws if the upstream changes its response shape
        return QuoteSchema.parse(await response.json())
    }
}

When getQuote fails, the AI works out the Quote it should have returned. The value is validated against QuoteSchema and returned as if getQuote had succeeded; if there's no correct Quote, getQuote rethrows its original error.

Documentation โ€‹

Every option is documented on its type, so your editor shows it on hover. Guides and the API reference are also on trypatch.dev.