Skip to content

Commit

Permalink
Handle per-step errors and returning error data
Browse files Browse the repository at this point in the history
  • Loading branch information
jpwilliams committed Nov 15, 2023
1 parent aebc2c4 commit fb83eb0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
12 changes: 11 additions & 1 deletion packages/inngest/src/components/InngestCommHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,17 @@ export class InngestCommHandler<

return {
status: 206,
headers: { "Content-Type": "application/json" },
headers: {
"Content-Type": "application/json",
...(typeof result.retriable !== "undefined"
? {
[headerKeys.NoRetry]: result.retriable ? "false" : "true",
...(typeof result.retriable === "string"
? { [headerKeys.RetryAfter]: result.retriable }
: {}),
}
: {}),
},
body: stringify([step]),
version,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { type AnyInngestFunction } from "../InngestFunction";
*/
export interface ExecutionResults {
"function-resolved": { data: unknown };
"step-ran": { step: OutgoingOp };
"step-ran": { step: OutgoingOp; retriable?: boolean | string };
"function-rejected": { error: unknown; retriable: boolean | string };
"steps-found": { steps: [OutgoingOp, ...OutgoingOp[]] };
"step-not-found": { step: OutgoingOp };
Expand Down
46 changes: 44 additions & 2 deletions packages/inngest/src/components/execution/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ class V1InngestExecution extends InngestExecution implements IInngestExecution {
data: { data: transformResult.data },
}),
};
// }
} else if (transformResult.type === "function-rejected") {
return {
type: "step-ran",
step: _internals.hashOp({
...stepResult,
error: transformResult.error,
}),
retriable: transformResult.retriable,
};
}

return transformResult;
Expand Down Expand Up @@ -439,8 +449,20 @@ class V1InngestExecution extends InngestExecution implements IInngestExecution {
): Promise<ExecutionResult> {
const output = { ...dataOrError };

/**
* If we've been given an error and it's one that we just threw from a step,
* we should return a `NonRetriableError` to stop execution.
*/
if (typeof output.error !== "undefined") {
output.data = serializeError(output.error);
const serializedError = serializeError(output.error);
output.data = serializedError;

if (output.error === this.#state.recentlyRejectedStepError) {
output.error = new NonRetriableError(serializedError.message, {
cause: output.error,
});
output.data = serializeError(output.error);
}
}

const transformedOutput = await this.#state.hooks?.transformOutput?.({
Expand Down Expand Up @@ -742,7 +764,11 @@ class V1InngestExecution extends InngestExecution implements IInngestExecution {
if (typeof stepState.data !== "undefined") {
resolve(stepState.data);
} else {
reject(stepState.error);
this.#state.recentlyRejectedStepError = deserializeError(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
stepState.error
);
reject(this.#state.recentlyRejectedStepError);
}
}

Expand Down Expand Up @@ -927,6 +953,22 @@ export interface V1ExecutionState {
* execution was completed.
*/
stepCompletionOrder: string[];

/**
* If defined, this is the error that purposefully thrown when memoizing step
* state in order to support per-step errors.
*
* We use this so that if the function itself rejects with the same error, we
* know that it was entirely uncaught (or at the very least rethrown), so we
* should send a `NonRetriableError` to stop needless execution of a function
* that will continue to fail.
*
* TODO This is imperfect, as this state is currently kept around for longer
* than it needs to be. It should disappear as soon as we've seen that the
* error did not immediately throw. It may need to be refactored to work a
* little more smoothly with the core loop.
*/
recentlyRejectedStepError?: unknown;
}

const hashId = (id: string): string => {
Expand Down

0 comments on commit fb83eb0

Please sign in to comment.