Skip to content
File

Blob: src/worker/dispatch/shared/execution-errors.ts

typescript48 lines
1import {
2 logger,
3 type RunExecutionContext,
4 type RunExecutionOutcome,
5} from "@/worker/dispatch/shared/run-execution-context";
6import { RunOwnershipLostError } from "@/worker/dispatch/shared/run-lease";
7 
8type RunErrorContext = Pick<RunExecutionContext, "logs" | "scope" | "state">;
9 
10export const appendFailureLogBestEffort = async (
11 context: Pick<RunErrorContext, "logs" | "scope">,
12 message: string,
13): Promise<void> => {
14 try {
15 await context.logs.appendSystemLog(message);
16 } catch (error) {
17 logger.warn("run_error_log_append_failed", {
18 ...context.scope.logContext,
19 error: error instanceof Error ? error.message : String(error),
20 });
21 }
22};
23 
24export const mapExecutionErrorToOutcome = async (
25 context: RunErrorContext,
26 error: unknown,
27): Promise<RunExecutionOutcome> => {
28 if (error instanceof RunOwnershipLostError) {
29 return {
30 kind: "ownership_lost",
31 observedStatus: error.observedStatus,
32 };
33 }
34 
35 if (context.state.cancelRequestedAt !== null) {
36 return {
37 kind: "canceled",
38 };
39 }
40 
41 const errorMessage = context.logs.redactMessage(error instanceof Error ? error.message : String(error));
42 return {
43 kind: "failed",
44 exitCode: 1,
45 errorMessage,
46 };
47};