UNPKG

@ai-growth/nextjs

Version:

Seamlessly integrate Sanity CMS with Next.js applications for automated blog routing and rendering

128 lines 4.68 kB
/** * @fileoverview Error handling utilities for Sanity CMS operations * * This module provides comprehensive error handling capabilities including * specific error types, context information, and utilities for determining * whether errors are retryable. * * @example * ```typescript * import { SanityError, isRetryableError, createSanityError } from '@ai-growth/nextjs/utils'; * * try { * const result = await sanityClient.fetch(query); * } catch (error) { * const sanityError = createSanityError(error, 'FETCH_FAILED', { * query, * timestamp: new Date().toISOString() * }); * * if (isRetryableError(sanityError)) { * // Retry the operation * } else { * // Handle non-retryable error * } * } * ``` */ /** * Standard error codes for Sanity operations */ export declare const SANITY_ERROR_CODES: { readonly NETWORK_ERROR: "NETWORK_ERROR"; readonly CONNECTION_TIMEOUT: "CONNECTION_TIMEOUT"; readonly DNS_RESOLUTION_FAILED: "DNS_RESOLUTION_FAILED"; readonly RATE_LIMITED: "RATE_LIMITED"; readonly SERVER_ERROR: "SERVER_ERROR"; readonly SERVICE_UNAVAILABLE: "SERVICE_UNAVAILABLE"; readonly GATEWAY_TIMEOUT: "GATEWAY_TIMEOUT"; readonly UNAUTHORIZED: "UNAUTHORIZED"; readonly FORBIDDEN: "FORBIDDEN"; readonly TOKEN_EXPIRED: "TOKEN_EXPIRED"; readonly BAD_REQUEST: "BAD_REQUEST"; readonly NOT_FOUND: "NOT_FOUND"; readonly INVALID_QUERY: "INVALID_QUERY"; readonly VALIDATION_ERROR: "VALIDATION_ERROR"; readonly FETCH_FAILED: "FETCH_FAILED"; readonly MUTATION_FAILED: "MUTATION_FAILED"; readonly CONNECTION_VERIFICATION_FAILED: "CONNECTION_VERIFICATION_FAILED"; readonly CLIENT_CREATION_FAILED: "CLIENT_CREATION_FAILED"; readonly CONFIGURATION_ERROR: "CONFIGURATION_ERROR"; readonly MISSING_CREDENTIALS: "MISSING_CREDENTIALS"; readonly INVALID_PROJECT_CONFIG: "INVALID_PROJECT_CONFIG"; readonly UNKNOWN_ERROR: "UNKNOWN_ERROR"; }; export type SanityErrorCode = (typeof SANITY_ERROR_CODES)[keyof typeof SANITY_ERROR_CODES]; /** * Context information for Sanity errors */ export interface SanityErrorContext { /** The operation that was being performed */ operation?: string; /** GROQ query that was being executed */ query?: string; /** Parameters passed to the query */ params?: Record<string, unknown>; /** HTTP status code if applicable */ statusCode?: number; /** Request URL if applicable */ url?: string; /** Request method if applicable */ method?: string; /** Timestamp when the error occurred */ timestamp?: string; /** Number of retry attempts made */ retryAttempt?: number; /** Total time elapsed for the operation */ duration?: number; /** Additional context specific to the error */ [key: string]: unknown; } /** * Enhanced error class for Sanity operations with detailed context */ export declare class SanityError extends Error { readonly code: SanityErrorCode; readonly context: SanityErrorContext; readonly retryable: boolean; readonly originalError?: Error | undefined; readonly name = "SanityError"; constructor(message: string, code: SanityErrorCode, context?: SanityErrorContext, retryable?: boolean, originalError?: Error | undefined); /** * Convert error to JSON for logging or serialization */ toJSON(): Record<string, unknown>; /** * Get a human-readable description of the error */ getDescription(): string; } /** * Determine if an error is retryable based on its type and status code */ export declare function isRetryableError(error: Error | SanityError): boolean; /** * Determine if an HTTP status code indicates a retryable error */ export declare function isRetryableStatusCode(statusCode: number): boolean; /** * Create a SanityError from various error types */ export declare function createSanityError(error: unknown, code: SanityErrorCode, context?: SanityErrorContext): SanityError; /** * Create a SanityError from an HTTP response */ export declare function createHttpError(statusCode: number, statusText: string, context?: SanityErrorContext): SanityError; /** * Wrap an async operation with error handling */ export declare function withErrorHandling<T>(operation: () => Promise<T>, operationName: string, context?: SanityErrorContext): Promise<T>; /** * Type guard to check if an error is a SanityError */ export declare function isSanityError(error: unknown): error is SanityError; /** * Extract error details for logging */ export declare function getErrorDetails(error: unknown): Record<string, unknown>; //# sourceMappingURL=error-handling.d.ts.map