All files error-handler.ts

94.23% Statements 49/52
87.5% Branches 42/48
100% Functions 12/12
94.23% Lines 49/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136    2x 2x                     2x   9x 9x   9x 8x   8x 8x 8x     1x 1x         15x 7x     8x 7x     1x       7x   2x       2x   1x   1x   1x                 2x 1x 1x   1x       13x     13x 2x     11x 2x     9x 1x     8x 1x     7x 1x     6x 1x     5x           1x       1x       1x 1x       1x       1x       1x       1x      
// src/error-handler.ts
 
import { Notice } from 'obsidian';
import {
  OllamaError,
  ErrorType,
  NetworkError,
  ApiError,
  ValidationError,
  StreamingError,
  ToolExecutionError,
  PathValidationError,
} from './types';
 
export class ErrorHandler {
  static handleError(error: unknown, context?: string): void {
    const message = this.getUserFriendlyMessage(error);
    new Notice(message);
 
    if (error instanceof Error) {
      const ctx = context ? ` [${context}]` : '';
      // Use console.error instead of ErrorHandler.error for fatal errors
      console.error(`Ollama Plugin Error${ctx}: ${error.message}`);
      if (error.stack) {
        console.error(error.stack);
      }
    } else {
      const ctx = context ? ` [${context}]` : '';
      console.error(`Ollama Plugin Error${ctx}:`, error);
    }
  }
 
  private static getUserFriendlyMessage(error: unknown): string {
    if (error instanceof OllamaError) {
      return this.getUserFriendlyMessageFromOllamaError(error);
    }
 
    if (error instanceof Error) {
      return this.getUserFriendlyMessageFromError(error);
    }
 
    return 'An unexpected error occurred';
  }
 
  private static getUserFriendlyMessageFromOllamaError(error: OllamaError): string {
    switch (error.type) {
      case ErrorType.NETWORK_ERROR:
        return 'Connection error. Please check if Ollama is running.';
      case ErrorType.API_ERROR:
        return `API error: ${error.message}`;
      case ErrorType.VALIDATION_ERROR:
        return this.getUserFriendlyValidationMessage(error);
      case ErrorType.STREAMING_ERROR:
        return 'Response too long. Please try a shorter request.';
      case ErrorType.TOOL_EXECUTION_ERROR:
        return `Tool error for ${(error as ToolExecutionError).toolName}. ${error.message}`;
      case ErrorType.PATH_VALIDATION_ERROR:
        return `Invalid file path: ${(error as PathValidationError).path}`;
      case ErrorType.UNKNOWN_ERROR:
        return 'An unexpected error occurred';
      default:
        return 'An unexpected error occurred';
    }
  }
 
  private static getUserFriendlyValidationMessage(error: OllamaError): string {
    if (error instanceof ValidationError && error.details?.field) {
      const fieldMsg = error.details.field.charAt(0).toUpperCase() + error.details.field.slice(1);
      return `Invalid ${fieldMsg.toLowerCase()}. ${error.details.message ?? error.message}`;
    }
    return 'Input validation error. Please correct your input.';
  }
 
  private static getUserFriendlyMessageFromError(error: Error): string {
    const msg = error.message.toLowerCase();
 
    // Check timeout BEFORE network (more specific matches first)
    if (msg.includes('timeout') || msg.includes('timed out') || msg.includes('time out')) {
      return 'Request timed out. Please check your Ollama connection.';
    }
 
    if (msg.includes('network') || msg.includes('connection') || msg.includes('fetch')) {
      return 'Connection error. Please check if Ollama is running.';
    }
 
    if (msg.includes('validation') || msg.includes('invalid')) {
      return 'Invalid input. Please correct your input.';
    }
 
    if (msg.includes('stream') || msg.includes('chunk')) {
      return 'Response too long. Please try a shorter request.';
    }
 
    if (msg.includes('tool') || msg.includes('function')) {
      return 'Tool error. Please try again.';
    }
 
    if (msg.includes('path') || msg.includes('file')) {
      return 'Invalid file path. Please check the path and try again.';
    }
 
    return 'An unexpected error occurred';
  }
 
  // -- Factory methods --
 
  static createNetworkError(message: string, statusCode?: number): NetworkError {
    return new NetworkError(message, statusCode);
  }
 
  static createApiError(message: string, statusCode?: number): ApiError {
    return new ApiError(message, statusCode);
  }
 
  static createValidationError(message: string, field?: string): ValidationError {
    const details = field ? { field, message } : undefined;
    return new ValidationError(message, details);
  }
 
  static createStreamingError(message: string): StreamingError {
    return new StreamingError(message);
  }
 
  static createToolExecutionError(message: string, toolName?: string): ToolExecutionError {
    return new ToolExecutionError(message, toolName ?? 'unknown');
  }
 
  static createPathValidationError(message: string, path?: string): PathValidationError {
    return new PathValidationError(message, path ?? '');
  }
 
  static createUnknownError(message: string): OllamaError {
    return new OllamaError(message, ErrorType.UNKNOWN_ERROR);
  }
}