Improve workflow error messaging
This commit is contained in:
@@ -3,6 +3,7 @@ import { getRun, start } from 'workflow/api';
|
||||
import type { WorkflowRunStatus } from '@workflow/world';
|
||||
import type { Task, TaskStatus, TaskTimeline, TaskType } from '@/lib/types';
|
||||
import { runTaskWorkflow } from '@/app/workflows/task-runner';
|
||||
import { describeTaskFailure } from '@/lib/server/task-errors';
|
||||
import {
|
||||
countTasksByStatus,
|
||||
createTaskRunRecord,
|
||||
@@ -65,21 +66,24 @@ async function reconcileTaskWithWorkflow(task: Task) {
|
||||
return task;
|
||||
}
|
||||
|
||||
const nextError = nextStatus === 'failed'
|
||||
? workflowStatus === 'cancelled'
|
||||
? 'Workflow run cancelled'
|
||||
: 'Workflow run failed'
|
||||
const failure = nextStatus === 'failed'
|
||||
? describeTaskFailure(task, workflowStatus === 'cancelled' ? 'Workflow run cancelled' : 'Workflow run failed')
|
||||
: null;
|
||||
|
||||
const updated = await setTaskStatusFromWorkflow(task.id, nextStatus, nextError);
|
||||
const updated = await setTaskStatusFromWorkflow(
|
||||
task.id,
|
||||
nextStatus,
|
||||
failure?.detail ?? null,
|
||||
failure?.summary ?? null
|
||||
);
|
||||
|
||||
const fallbackTask = {
|
||||
...task,
|
||||
status: nextStatus,
|
||||
stage: nextStatus,
|
||||
stage_detail: nextStatus === 'failed' ? nextError : 'Workflow run completed.',
|
||||
stage_detail: nextStatus === 'failed' ? (failure?.summary ?? 'The background workflow stopped unexpectedly.') : 'Workflow run completed.',
|
||||
stage_context: null,
|
||||
error: nextError,
|
||||
error: failure?.detail ?? null,
|
||||
finished_at: nextStatus === 'queued' || nextStatus === 'running'
|
||||
? null
|
||||
: task.finished_at ?? new Date().toISOString()
|
||||
@@ -114,11 +118,14 @@ export async function enqueueTask(input: EnqueueTaskInput) {
|
||||
workflow_run_id: run.runId
|
||||
} satisfies Task;
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error
|
||||
? error.message
|
||||
: 'Failed to start workflow';
|
||||
await markTaskFailure(task.id, reason, 'failed');
|
||||
throw error;
|
||||
const failure = describeTaskFailure(task, 'Failed to start workflow');
|
||||
await markTaskFailure(task.id, failure.detail, 'failed', {
|
||||
detail: failure.summary
|
||||
});
|
||||
|
||||
const wrapped = new Error(failure.detail);
|
||||
(wrapped as Error & { cause?: unknown }).cause = error;
|
||||
throw wrapped;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user