/**
* Error codes for tool failures and validation issues.
*
* - CONFIG_MISSING: Required environment variables are not configured
* - VALIDATION_ERROR: Input validation failed (e.g., invalid email)
* - SEND_DISABLED: Sending is disabled by server policy
* - POLICY_VIOLATION: Message violates policy limits (size, recipients, etc.)
* - ATTACHMENT_ERROR: Attachment validation failed (size, format, etc.)
* - SMTP_ERROR: SMTP transport or server error
* - UNKNOWN_ERROR: Unexpected or unclassified error
*/
export type ErrorCode =
| "CONFIG_MISSING"
| "VALIDATION_ERROR"
| "SEND_DISABLED"
| "POLICY_VIOLATION"
| "ATTACHMENT_ERROR"
| "SMTP_ERROR"
| "UNKNOWN_ERROR";
/**
* Custom error class for tool-specific failures with machine-readable error codes.
*
* This error type is used throughout the MCP tool implementations to provide
* structured error information to clients, allowing for programmatic error handling.
*/
export class ToolError extends Error {
/**
* The machine-readable error code indicating the type of failure.
*/
public readonly code: ErrorCode;
constructor(code: ErrorCode, message: string) {
super(message);
this.code = code;
}
}
/**
* Return a safe error message for tool consumers.
*/
export function formatToolError(error: unknown): { code: ErrorCode; message: string } {
if (error instanceof ToolError) {
return { code: error.code, message: error.message };
}
if (error instanceof Error) {
return { code: "UNKNOWN_ERROR", message: error.message };
}
return { code: "UNKNOWN_ERROR", message: "Unexpected error." };
}