mirror of
https://github.com/Nikhil-Doye/workflow-builder.git
synced 2026-07-22 02:01:56 +02:00
Refactor Discord and Gmail processors for improved consistency and error handling
- Updated discordProcessor and gmailProcessor to utilize a standardized execution context and plan, enhancing maintainability and observability. - Improved error handling by throwing detailed errors with context for better debugging. - Enhanced configuration validation to ensure proper structure and provide clear error messages. - Standardized response format to include execution metadata, improving traceability and user feedback.
This commit is contained in:
@@ -1,54 +1,75 @@
|
||||
import { NodeData } from "../../types";
|
||||
import { ExecutionContext, ExecutionPlan } from "../executionEngine";
|
||||
import { DiscordService } from "../discord/DiscordService";
|
||||
import { DiscordOperation } from "../../types/discord";
|
||||
import { DiscordOperation, DiscordConfig } from "../../types/discord";
|
||||
|
||||
/**
|
||||
* Discord processor - handles Discord operations with full execution context
|
||||
* Refactored to match standard processor signature for consistency, observability, and maintainability
|
||||
*/
|
||||
export default async function discordProcessor(
|
||||
context: ExecutionContext,
|
||||
plan: ExecutionPlan
|
||||
): Promise<any> {
|
||||
const { config, nodeId, nodeType } = context;
|
||||
const startTime = Date.now();
|
||||
|
||||
async function discordProcessor(nodeData: NodeData): Promise<{
|
||||
success: boolean;
|
||||
data?: any;
|
||||
error?: string;
|
||||
}> {
|
||||
try {
|
||||
const { config } = nodeData;
|
||||
|
||||
// Validate configuration exists
|
||||
if (!config) {
|
||||
return {
|
||||
success: false,
|
||||
error: "Discord configuration is missing",
|
||||
};
|
||||
throw new Error("Discord configuration is missing");
|
||||
}
|
||||
|
||||
const discordService = new DiscordService();
|
||||
|
||||
// Validate configuration
|
||||
const validation = discordService.validateConfig(config as any);
|
||||
// Validate configuration structure
|
||||
const validation = discordService.validateConfig(config as DiscordConfig);
|
||||
if (!validation.isValid) {
|
||||
return {
|
||||
success: false,
|
||||
error: `Configuration validation failed: ${validation.errors.join(
|
||||
", "
|
||||
)}`,
|
||||
};
|
||||
throw new Error(
|
||||
`Configuration validation failed: ${validation.errors.join(", ")}`
|
||||
);
|
||||
}
|
||||
|
||||
// Execute Discord operation
|
||||
const result = await discordService.executeOperation(
|
||||
config.operation as DiscordOperation,
|
||||
config as any
|
||||
config as DiscordConfig
|
||||
);
|
||||
|
||||
const executionTime = Date.now() - startTime;
|
||||
|
||||
// Return standardized response with execution metadata
|
||||
return {
|
||||
success: true,
|
||||
operation: config.operation,
|
||||
data: result,
|
||||
executionTime,
|
||||
type: "discord",
|
||||
nodeId,
|
||||
nodeType,
|
||||
metadata: {
|
||||
channelId: (config as DiscordConfig).channelId,
|
||||
guildId: (config as DiscordConfig).botToken ? "configured" : undefined,
|
||||
timestamp: new Date().toISOString(),
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "Unknown Discord operation error",
|
||||
};
|
||||
const executionTime = Date.now() - startTime;
|
||||
|
||||
// Throw error with context for proper error tracing in execution engine
|
||||
const errorMessage =
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "Unknown Discord operation error";
|
||||
const enhancedError = new Error(
|
||||
`Discord operation failed (node: ${nodeId}): ${errorMessage}`
|
||||
);
|
||||
|
||||
// Attach metadata for debugging and observability
|
||||
(enhancedError as any).nodeId = nodeId;
|
||||
(enhancedError as any).nodeType = nodeType;
|
||||
(enhancedError as any).executionTime = executionTime;
|
||||
(enhancedError as any).operation = config?.operation;
|
||||
|
||||
throw enhancedError;
|
||||
}
|
||||
}
|
||||
|
||||
export default discordProcessor;
|
||||
|
||||
@@ -1,54 +1,75 @@
|
||||
import { NodeData } from "../../types";
|
||||
import { ExecutionContext, ExecutionPlan } from "../executionEngine";
|
||||
import { GmailService } from "../gmail/GmailService";
|
||||
import { GmailOperation } from "../../types/gmail";
|
||||
import { GmailOperation, GmailConfig } from "../../types/gmail";
|
||||
|
||||
/**
|
||||
* Gmail processor - handles Gmail operations with full execution context
|
||||
* Refactored to match standard processor signature for consistency, observability, and maintainability
|
||||
*/
|
||||
export default async function gmailProcessor(
|
||||
context: ExecutionContext,
|
||||
plan: ExecutionPlan
|
||||
): Promise<any> {
|
||||
const { config, nodeId, nodeType } = context;
|
||||
const startTime = Date.now();
|
||||
|
||||
async function gmailProcessor(nodeData: NodeData): Promise<{
|
||||
success: boolean;
|
||||
data?: any;
|
||||
error?: string;
|
||||
}> {
|
||||
try {
|
||||
const { config } = nodeData;
|
||||
|
||||
// Validate configuration exists
|
||||
if (!config) {
|
||||
return {
|
||||
success: false,
|
||||
error: "Gmail configuration is missing",
|
||||
};
|
||||
throw new Error("Gmail configuration is missing");
|
||||
}
|
||||
|
||||
const gmailService = new GmailService();
|
||||
|
||||
// Validate configuration
|
||||
const validation = gmailService.validateConfig(config as any);
|
||||
// Validate configuration structure
|
||||
const validation = gmailService.validateConfig(config as GmailConfig);
|
||||
if (!validation.isValid) {
|
||||
return {
|
||||
success: false,
|
||||
error: `Configuration validation failed: ${validation.errors.join(
|
||||
", "
|
||||
)}`,
|
||||
};
|
||||
throw new Error(
|
||||
`Configuration validation failed: ${validation.errors.join(", ")}`
|
||||
);
|
||||
}
|
||||
|
||||
// Execute Gmail operation
|
||||
const result = await gmailService.executeOperation(
|
||||
config.operation as GmailOperation,
|
||||
config as any
|
||||
config as GmailConfig
|
||||
);
|
||||
|
||||
const executionTime = Date.now() - startTime;
|
||||
|
||||
// Return standardized response with execution metadata
|
||||
return {
|
||||
success: true,
|
||||
operation: config.operation,
|
||||
data: result,
|
||||
executionTime,
|
||||
type: "gmail",
|
||||
nodeId,
|
||||
nodeType,
|
||||
metadata: {
|
||||
messageId: (config as GmailConfig).messageId,
|
||||
threadId: (config as GmailConfig).threadId,
|
||||
to: (config as GmailConfig).to,
|
||||
subject: (config as GmailConfig).subject,
|
||||
timestamp: new Date().toISOString(),
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "Unknown Gmail operation error",
|
||||
};
|
||||
const executionTime = Date.now() - startTime;
|
||||
|
||||
// Throw error with context for proper error tracing in execution engine
|
||||
const errorMessage =
|
||||
error instanceof Error ? error.message : "Unknown Gmail operation error";
|
||||
const enhancedError = new Error(
|
||||
`Gmail operation failed (node: ${nodeId}): ${errorMessage}`
|
||||
);
|
||||
|
||||
// Attach metadata for debugging and observability
|
||||
(enhancedError as any).nodeId = nodeId;
|
||||
(enhancedError as any).nodeType = nodeType;
|
||||
(enhancedError as any).executionTime = executionTime;
|
||||
(enhancedError as any).operation = config?.operation;
|
||||
|
||||
throw enhancedError;
|
||||
}
|
||||
}
|
||||
|
||||
export default gmailProcessor;
|
||||
|
||||
Reference in New Issue
Block a user