// Copyright (C) 2026 Rob Colbert <rob.colbert@openplatform.us>
// Licensed under the Apache License, Version 2.0

export type ToolErrorCode =
  | "MISSING_PARAMETER"
  | "INVALID_PARAMETER"
  | "NOT_FOUND"
  | "PERMISSION_DENIED"
  | "OPERATION_FAILED"
  | "OPERATION_NOT_ALLOWED"
  | "VALIDATION_ERROR"
  | "RATE_LIMITED"
  | "LIMIT_EXCEEDED"
  | "INVALID_CRON_SPEC"
  | "INVALID_OPERATION"
  | "TIMEOUT"
  | "INVALID_TOOL_ARGUMENTS"
  | "SUBAGENT_FAILED"
  | "TOOL_EXECUTION_FAILED"
  | "UNAUTHORIZED"
  | "FORBIDDEN"
  | "RATE_LIMIT_EXCEEDED"
  | "NETWORK_ERROR"
  | "SECURITY_VIOLATION";

export interface IToolError {
  code: ToolErrorCode;
  message: string;
  parameter?: string;
  expected?: string;
  example?: string;
  recoveryHint?: string;
}

export function formatError(error: IToolError): string {
  const components: string[] = [`TOOL ERROR: ${error.code}`, error.message];
  if (error.parameter) {
    components.push(`PARAMETER: ${error.parameter}`);
  }
  if (error.expected) {
    components.push(`EXPECTED: ${error.expected}`);
  }
  if (error.example) {
    components.push(`EXAMPLE: ${error.example}`);
  }
  if (error.recoveryHint) {
    components.push(`RECOVERY HINT: ${error.recoveryHint}`);
  }
  return components.join("\n");
}
