mirror of https://github.com/buster-so/buster.git
Refactor message post-processing task to improve error handling and logging. Updated database update logic to include error logging and added type definitions for branch results. Adjusted integration test with a new message ID.
This commit is contained in:
parent
3b01e5727b
commit
51c85c4384
|
@ -125,7 +125,7 @@ describe.skipIf(skipIntegrationTests)('messagePostProcessingTask integration', (
|
|||
|
||||
it('should handle message with no conversation history', async () => {
|
||||
// Use prepopulated message ID
|
||||
const messageId = 'a3206f20-35d1-4a6c-84a7-48f8f222c39f';
|
||||
const messageId = '203744bd-439f-4b3c-9ea2-ddfe243c5afe';
|
||||
|
||||
// Execute task
|
||||
const result = await tasks.triggerAndPoll<typeof messagePostProcessingTask>(
|
||||
|
|
|
@ -180,7 +180,14 @@ export const messagePostProcessingTask: ReturnType<
|
|||
|
||||
// Handle branch results - the result will have one of the branch step IDs as a key
|
||||
let validatedOutput: PostProcessingWorkflowOutput;
|
||||
const branchResult = workflowResult.result as any; // Type assertion needed for branch results
|
||||
|
||||
// Define the expected shape of branch results
|
||||
type BranchResult = {
|
||||
'format-follow-up-message'?: PostProcessingWorkflowOutput;
|
||||
'format-initial-message'?: PostProcessingWorkflowOutput;
|
||||
};
|
||||
|
||||
const branchResult = workflowResult.result as BranchResult;
|
||||
|
||||
if ('format-follow-up-message' in branchResult && branchResult['format-follow-up-message']) {
|
||||
validatedOutput = branchResult['format-follow-up-message'] as PostProcessingWorkflowOutput;
|
||||
|
@ -195,6 +202,11 @@ export const messagePostProcessingTask: ReturnType<
|
|||
resultKeys: Object.keys(branchResult),
|
||||
result: branchResult,
|
||||
});
|
||||
console.error('Unexpected workflow result structure:', {
|
||||
messageId: payload.messageId,
|
||||
resultKeys: Object.keys(branchResult),
|
||||
result: branchResult,
|
||||
});
|
||||
throw new Error('Post-processing workflow returned unexpected result structure');
|
||||
}
|
||||
|
||||
|
@ -214,9 +226,10 @@ export const messagePostProcessingTask: ReturnType<
|
|||
messageId: payload.messageId,
|
||||
});
|
||||
|
||||
const db = getDb();
|
||||
|
||||
const dbData = extractDbFields(validatedOutput, messageContext.userName);
|
||||
|
||||
try {
|
||||
const db = getDb();
|
||||
await db
|
||||
.update(messages)
|
||||
.set({
|
||||
|
@ -225,7 +238,28 @@ export const messagePostProcessingTask: ReturnType<
|
|||
})
|
||||
.where(eq(messages.id, payload.messageId));
|
||||
|
||||
logger.log('Database update successful', {
|
||||
messageId: payload.messageId,
|
||||
});
|
||||
} catch (dbError) {
|
||||
const errorMessage = dbError instanceof Error ? dbError.message : 'Unknown database error';
|
||||
logger.error('Failed to update database with post-processing result', {
|
||||
messageId: payload.messageId,
|
||||
error: errorMessage,
|
||||
});
|
||||
console.error('Failed to update database with post-processing result:', {
|
||||
messageId: payload.messageId,
|
||||
error: errorMessage,
|
||||
stack: dbError instanceof Error ? dbError.stack : undefined,
|
||||
});
|
||||
// Throw the error to ensure the task fails when database update fails
|
||||
throw new Error(`Database update failed: ${errorMessage}`);
|
||||
}
|
||||
|
||||
// Step 6: Send Slack notification if conditions are met
|
||||
let slackNotificationSent = false;
|
||||
|
||||
try {
|
||||
logger.log('Checking Slack notification conditions', {
|
||||
messageId: payload.messageId,
|
||||
organizationId: messageContext.organizationId,
|
||||
|
@ -246,6 +280,7 @@ export const messagePostProcessingTask: ReturnType<
|
|||
});
|
||||
|
||||
if (slackResult.sent) {
|
||||
slackNotificationSent = true;
|
||||
logger.log('Slack notification sent successfully', {
|
||||
messageId: payload.messageId,
|
||||
organizationId: messageContext.organizationId,
|
||||
|
@ -257,10 +292,27 @@ export const messagePostProcessingTask: ReturnType<
|
|||
reason: slackResult.error,
|
||||
});
|
||||
}
|
||||
} catch (slackError) {
|
||||
const errorMessage =
|
||||
slackError instanceof Error ? slackError.message : 'Unknown Slack error';
|
||||
logger.error('Failed to send Slack notification', {
|
||||
messageId: payload.messageId,
|
||||
organizationId: messageContext.organizationId,
|
||||
error: errorMessage,
|
||||
});
|
||||
console.error('Failed to send Slack notification:', {
|
||||
messageId: payload.messageId,
|
||||
organizationId: messageContext.organizationId,
|
||||
error: errorMessage,
|
||||
stack: slackError instanceof Error ? slackError.stack : undefined,
|
||||
});
|
||||
// Don't throw - this is a non-critical error
|
||||
}
|
||||
|
||||
logger.log('Message post-processing completed successfully', {
|
||||
messageId: payload.messageId,
|
||||
executionTimeMs: Date.now() - startTime,
|
||||
slackNotificationSent,
|
||||
});
|
||||
|
||||
// Wait 500ms to allow Braintrust to clean up its trace before completing
|
||||
|
@ -285,6 +337,13 @@ export const messagePostProcessingTask: ReturnType<
|
|||
executionTimeMs: Date.now() - startTime,
|
||||
});
|
||||
|
||||
console.error('Post-processing task execution failed:', {
|
||||
messageId: payload.messageId,
|
||||
error: errorMessage,
|
||||
stack: error instanceof Error ? error.stack : undefined,
|
||||
executionTimeMs: Date.now() - startTime,
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
messageId: payload.messageId,
|
||||
|
|
Loading…
Reference in New Issue