error-factory-plan.json•12.8 kB
{
  "schema": "memory_document_v2",
  "metadata": {
    "id": "6a5b4c3d-2e1f-0d9c-8b7a-6c5d4e3f2b1a",
    "title": "エラーファクトリー改善計画",
    "documentType": "plan",
    "path": "error-factory-plan.json",
    "tags": [
      "improvement",
      "error-handling",
      "mcp"
    ],
    "lastModified": "2025-03-30T05:50:32.360Z",
    "createdAt": "2025-03-30T05:50:32.360Z",
    "version": 1
  },
  "content": {
    "overview": {
      "title": "MCP-2・MCP-3: エラーファクトリーメソッド改善計画",
      "description": "エラーファクトリーメソッドの一貫した使用と、不足しているファクトリーメソッドの追加に関する詳細実装計画。直接エラーコンストラクタを使用している箇所を適切なファクトリーメソッドに置き換え、必要に応じて新しいファクトリーメソッドを追加する。"
    },
    "currentState": {
      "description": "現在のコードベースでは、エラー生成の方法が統一されていない。一部のコードではファクトリーメソッドを使用しているが、他の箇所では直接エラーコンストラクタを使用している。また、特定のエラーパターンに対するファクトリーメソッドが実装されていないため、直接コンストラクタを使用せざるを得ない状況もある。",
      "examples": [
        "// 直接コンストラクタを使用(非推奨)\nthrow new DomainError('VALIDATION_ERROR', 'Files must be provided as an object');\n\n// ファクトリーメソッドを使用(推奨)\nthrow DomainErrors.documentNotFound(path);"
      ],
      "affectedFiles": [
        "BranchController.ts",
        "FileSystemBranchMemoryBankRepository.ts",
        "InfrastructureError.ts",
        "DomainError.ts",
        "ApplicationError.ts"
      ]
    },
    "missingFactoryMethods": [
      {
        "errorType": "InfrastructureError",
        "methods": [
          {
            "name": "permissionDenied",
            "description": "ファイルシステム権限エラー",
            "parameters": [
              "message: string",
              "details?: OperationDetails"
            ]
          },
          {
            "name": "fileSystemError",
            "description": "一般的なファイルシステムエラー",
            "parameters": [
              "message: string",
              "details?: OperationDetails"
            ]
          },
          {
            "name": "invalidFileContent",
            "description": "無効なファイル内容エラー",
            "parameters": [
              "message: string",
              "details?: OperationDetails"
            ]
          }
        ]
      },
      {
        "errorType": "DomainError",
        "methods": [
          {
            "name": "validationError",
            "description": "バリデーションエラー",
            "parameters": [
              "message: string",
              "details?: OperationDetails"
            ]
          },
          {
            "name": "invalidOperation",
            "description": "不正な操作エラー",
            "parameters": [
              "operation: string",
              "message: string",
              "details?: OperationDetails"
            ]
          }
        ]
      },
      {
        "errorType": "ApplicationError",
        "methods": [
          {
            "name": "configurationError",
            "description": "設定エラー",
            "parameters": [
              "message: string",
              "originalError?: Error",
              "details?: OperationDetails"
            ]
          },
          {
            "name": "validationFailed",
            "description": "ユースケース入力検証エラー",
            "parameters": [
              "useCaseName: string",
              "message: string",
              "details?: OperationDetails"
            ]
          }
        ]
      }
    ],
    "implementationPlan": {
      "newFactoryMethods": [
        {
          "path": "/packages/mcp/src/shared/errors/InfrastructureError.ts",
          "additions": [
            {
              "method": "permissionDenied",
              "code": "/**\n * ファイルシステム権限エラーを作成します\n * @param message エラーメッセージ\n * @param details 操作の詳細情報\n * @returns InfrastructureError\n */\npermissionDenied: (message: string, details?: OperationDetails) => {\n  return new InfrastructureError(\n    InfrastructureErrorCodes.FILE_PERMISSION_ERROR,\n    message,\n    details\n  );\n}"
            },
            {
              "method": "fileSystemError",
              "code": "/**\n * 一般的なファイルシステムエラーを作成します\n * @param message エラーメッセージ\n * @param details 操作の詳細情報\n * @returns InfrastructureError\n */\nfileSystemError: (message: string, details?: OperationDetails) => {\n  return new InfrastructureError(\n    InfrastructureErrorCodes.FILE_SYSTEM_ERROR,\n    message,\n    details\n  );\n}"
            },
            {
              "method": "invalidFileContent",
              "code": "/**\n * 無効なファイル内容エラーを作成します\n * @param message エラーメッセージ\n * @param details 操作の詳細情報\n * @returns InfrastructureError\n */\ninvalidFileContent: (message: string, details?: OperationDetails) => {\n  return new InfrastructureError(\n    InfrastructureErrorCodes.INVALID_FILE_CONTENT,\n    message,\n    details\n  );\n}"
            }
          ]
        },
        {
          "path": "/packages/mcp/src/shared/errors/DomainError.ts",
          "additions": [
            {
              "method": "validationError",
              "code": "/**\n * バリデーションエラーを作成します\n * @param message エラーメッセージ\n * @param details 操作の詳細情報\n * @returns DomainError\n */\nvalidationError: (message: string, details?: OperationDetails) => {\n  return new DomainError(\n    DomainErrorCodes.VALIDATION_ERROR,\n    message,\n    details\n  );\n}"
            },
            {
              "method": "invalidOperation",
              "code": "/**\n * 不正な操作エラーを作成します\n * @param operation 操作名\n * @param message エラーメッセージ\n * @param details 操作の詳細情報\n * @returns DomainError\n */\ninvalidOperation: (operation: string, message: string, details?: OperationDetails) => {\n  return new DomainError(\n    DomainErrorCodes.INVALID_OPERATION,\n    `Invalid operation '${operation}': ${message}`,\n    details\n  );\n}"
            }
          ]
        },
        {
          "path": "/packages/mcp/src/shared/errors/ApplicationError.ts",
          "additions": [
            {
              "method": "configurationError",
              "code": "/**\n * 設定エラーを作成します\n * @param message エラーメッセージ\n * @param originalError 元のエラー\n * @param details 操作の詳細情報\n * @returns ApplicationError\n */\nconfigurationError: (message: string, originalError?: Error, details?: OperationDetails) => {\n  return new ApplicationError(\n    ApplicationErrorCodes.CONFIGURATION_ERROR,\n    message,\n    originalError,\n    details\n  );\n}"
            },
            {
              "method": "validationFailed",
              "code": "/**\n * ユースケース入力検証エラーを作成します\n * @param useCaseName ユースケース名\n * @param message エラーメッセージ\n * @param details 操作の詳細情報\n * @returns ApplicationError\n */\nvalidationFailed: (useCaseName: string, message: string, details?: OperationDetails) => {\n  return new ApplicationError(\n    ApplicationErrorCodes.VALIDATION_FAILED,\n    `Validation failed in ${useCaseName}: ${message}`,\n    undefined,\n    details\n  );\n}"
            }
          ]
        }
      ],
      "errorCodeUpdates": [
        {
          "path": "/packages/mcp/src/shared/errors/InfrastructureError.ts",
          "additions": [
            "FILE_PERMISSION_ERROR = 'INFRASTRUCTURE_FILE_PERMISSION_ERROR'",
            "INVALID_FILE_CONTENT = 'INFRASTRUCTURE_INVALID_FILE_CONTENT'"
          ]
        },
        {
          "path": "/packages/mcp/src/shared/errors/DomainError.ts",
          "additions": [
            "VALIDATION_ERROR = 'DOMAIN_ERROR.VALIDATION_ERROR'",
            "INVALID_OPERATION = 'DOMAIN_ERROR.INVALID_OPERATION'"
          ]
        },
        {
          "path": "/packages/mcp/src/shared/errors/ApplicationError.ts",
          "additions": [
            "CONFIGURATION_ERROR = 'APPLICATION_ERROR.CONFIGURATION_ERROR'",
            "VALIDATION_FAILED = 'APPLICATION_ERROR.VALIDATION_FAILED'"
          ]
        }
      ],
      "factoryUsageUpdates": [
        {
          "path": "/packages/mcp/src/interface/controllers/BranchController.ts",
          "replacements": [
            {
              "original": "throw new DomainError('VALIDATION_ERROR', 'Files must be provided as an object');",
              "replacement": "throw DomainErrors.validationError('Files must be provided as an object');"
            },
            {
              "original": "throw new DomainError('VALIDATION_ERROR', `Content must be provided for path: ${path}`);",
              "replacement": "throw DomainErrors.validationError(`Content must be provided for path: ${path}`);"
            }
          ]
        },
        {
          "path": "/packages/mcp/src/infrastructure/repositories/file-system/FileSystemBranchMemoryBankRepository.ts",
          "replacements": [
            {
              "original": "throw new InfrastructureError('FILE_NOT_FOUND', `Branch not found: ${branchName}`);",
              "replacement": "throw InfrastructureErrors.fileNotFound(`Branch not found: ${branchName}`);"
            },
            {
              "original": "throw new InfrastructureError('FILE_SYSTEM_ERROR', `Failed to read file: ${fullPath}`, { error: error.message });",
              "replacement": "throw InfrastructureErrors.fileSystemError(`Failed to read file: ${fullPath}`, { error: error.message });"
            },
            {
              "original": "throw new InfrastructureError('INFRASTRUCTURE_INVALID_FILE_CONTENT', `Invalid JSON structure at: ${fullPath}`);",
              "replacement": "throw InfrastructureErrors.invalidFileContent(`Invalid JSON structure at: ${fullPath}`);"
            }
          ]
        }
      ]
    },
    "testingPlan": {
      "approach": "ユニットテストとエラー検証",
      "newTests": [
        {
          "path": "/packages/mcp/tests/unit/shared/errors/InfrastructureError.test.ts",
          "tests": [
            "permissionDenied()ファクトリーメソッドが正しいエラーコードとメッセージで作成されること",
            "fileSystemError()ファクトリーメソッドが正しいエラーコードとメッセージで作成されること",
            "invalidFileContent()ファクトリーメソッドが正しいエラーコードとメッセージで作成されること"
          ]
        },
        {
          "path": "/packages/mcp/tests/unit/shared/errors/DomainError.test.ts",
          "tests": [
            "validationError()ファクトリーメソッドが正しいエラーコードとメッセージで作成されること",
            "invalidOperation()ファクトリーメソッドが正しいエラーコードとメッセージで作成されること"
          ]
        },
        {
          "path": "/packages/mcp/tests/unit/shared/errors/ApplicationError.test.ts",
          "tests": [
            "configurationError()ファクトリーメソッドが正しいエラーコードとメッセージで作成されること",
            "validationFailed()ファクトリーメソッドが正しいエラーコードとメッセージで作成されること"
          ]
        }
      ],
      "verificationTests": [
        "BranchControllerが正しいエラーファクトリーメソッドを使用していることを確認",
        "FileSystemBranchMemoryBankRepositoryが正しいエラーファクトリーメソッドを使用していることを確認"
      ]
    },
    "benefits": [
      "エラー処理の一貫性向上",
      "バグの早期発見",
      "トラブルシューティングの効率化",
      "コードの読みやすさ向上"
    ],
    "dependencies": [
      "BaseError.ts - 基底エラークラス",
      "ErrorUtils.ts - エラーユーティリティ",
      "共通のエラーフォーマットとHTTPステータスコード対応"
    ]
  }
}