-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patherror.ts
63 lines (59 loc) · 1.75 KB
/
error.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { QstashError } from "@upstash/qstash";
import type { FailureFunctionPayload, Step } from "./types";
/**
* Error raised during Workflow execution
*/
export class WorkflowError extends QstashError {
constructor(message: string) {
super(message);
this.name = "WorkflowError";
}
}
/**
* Raised when the workflow executes a function successfully
* and aborts to end the execution
*/
export class WorkflowAbort extends Error {
public stepInfo?: Step;
public stepName: string;
/**
* whether workflow is to be canceled on abort
*/
public cancelWorkflow: boolean;
/**
*
* @param stepName name of the aborting step
* @param stepInfo step information
* @param cancelWorkflow
*/
constructor(stepName: string, stepInfo?: Step, cancelWorkflow = false) {
super(
"This is an Upstash Workflow error thrown after a step executes. It is expected to be raised." +
" Make sure that you await for each step. Also, if you are using try/catch blocks, you should not wrap context.run/sleep/sleepUntil/call methods with try/catch." +
` Aborting workflow after executing step '${stepName}'.`
);
this.name = "WorkflowAbort";
this.stepName = stepName;
this.stepInfo = stepInfo;
this.cancelWorkflow = cancelWorkflow;
}
}
/**
* Formats an unknown error to match the FailureFunctionPayload format
*
* @param error
* @returns
*/
export const formatWorkflowError = (error: unknown): FailureFunctionPayload => {
return error instanceof Error
? {
error: error.name,
message: error.message,
}
: {
error: "Error",
message:
"An error occured while executing workflow: " +
`'${typeof error === "string" ? error : JSON.stringify(error)}'`,
};
};