-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy patherrors.ts
More file actions
1072 lines (954 loc) · 27.3 KB
/
errors.ts
File metadata and controls
1072 lines (954 loc) · 27.3 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { z } from "zod";
import { DeploymentErrorData } from "./schemas/api.js";
import { ImportTaskFileErrors, WorkerManifest } from "./schemas/build.js";
import {
SerializedError,
TaskRunError,
TaskRunErrorCodes,
TaskRunInternalError,
} from "./schemas/common.js";
import { TaskMetadataFailedToParseData } from "./schemas/messages.js";
import { links } from "./links.js";
import { ExceptionEventProperties } from "./schemas/openTelemetry.js";
import { assertExhaustive } from "../utils.js";
/**
* If you throw this, it will get converted into an INTERNAL_ERROR
*/
export class InternalError extends Error {
public readonly code: TaskRunErrorCodes;
public readonly skipRetrying: boolean;
constructor({
code,
message,
showStackTrace = true,
skipRetrying = false,
}: {
code: TaskRunErrorCodes;
message?: string;
showStackTrace?: boolean;
skipRetrying?: boolean;
}) {
super(`${code}: ${message ?? "No message"}`);
this.name = "TriggerInternalError";
this.code = code;
this.message = message ?? "InternalError";
if (!showStackTrace) {
this.stack = undefined;
}
this.skipRetrying = skipRetrying;
}
}
export function isInternalError(error: unknown): error is InternalError {
return error instanceof Error && error.name === "TriggerInternalError";
}
export class AbortTaskRunError extends Error {
constructor(message: string) {
super(message);
this.name = "AbortTaskRunError";
}
}
const MANUAL_OOM_KILL_ERROR_MESSAGE = "MANUAL_OOM_KILL_ERROR";
/**
* This causes an Out Of Memory error on the run (if it's uncaught).
* This can be useful if you use a native package that detects it's run out of memory but doesn't kill Node.js
*/
export class OutOfMemoryError extends Error {
constructor() {
super(MANUAL_OOM_KILL_ERROR_MESSAGE);
this.name = "OutOfMemoryError";
}
}
export function isManualOutOfMemoryError(error: TaskRunError) {
if (error.type === "BUILT_IN_ERROR") {
if (error.message && error.message === MANUAL_OOM_KILL_ERROR_MESSAGE) {
return true;
}
}
return false;
}
export function isOOMRunError(error: TaskRunError) {
if (error.type === "INTERNAL_ERROR") {
if (
error.code === "TASK_PROCESS_OOM_KILLED" ||
error.code === "TASK_PROCESS_MAYBE_OOM_KILLED"
) {
return true;
}
// For the purposes of retrying on a larger machine, we're going to treat this is an OOM error.
// This is what they look like if we're executing using k8s. They then get corrected later, but it's too late.
// {"code": "TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE", "type": "INTERNAL_ERROR", "message": "Process exited with code -1 after signal SIGKILL."}
if (
error.code === "TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE" &&
error.message &&
error.message.includes("-1")
) {
if (error.message.includes("SIGKILL")) {
return true;
}
if (error.message.includes("SIGABRT") && error.stackTrace) {
const oomIndicators = [
"JavaScript heap out of memory",
"Reached heap limit",
"FATAL ERROR: Reached heap limit Allocation failed",
];
if (oomIndicators.some((indicator) => error.stackTrace!.includes(indicator))) {
return true;
}
}
}
}
if (error.type === "BUILT_IN_ERROR") {
// ffmpeg also does weird stuff
// { "name": "Error", "type": "BUILT_IN_ERROR", "message": "ffmpeg was killed with signal SIGKILL" }
if (error.message && error.message.includes("ffmpeg was killed with signal SIGKILL")) {
return true;
}
}
// Special `OutOfMemoryError` for doing a manual OOM kill.
// Useful if a native library does an OOM but doesn't actually crash the run and you want to manually
if (isManualOutOfMemoryError(error)) {
return true;
}
return false;
}
export class TaskPayloadParsedError extends Error {
public readonly cause: unknown;
constructor(cause: unknown) {
const causeMessage = cause instanceof Error ? cause.message : String(cause);
super("Parsing payload with schema failed: " + causeMessage);
this.name = "TaskPayloadParsedError";
this.cause = cause;
}
}
export class CompleteTaskWithOutput extends Error {
public readonly output: unknown;
constructor(output?: unknown) {
super("Complete task with output");
this.name = "CompleteTaskWithOutput";
this.output = output;
}
}
export function isCompleteTaskWithOutput(error: unknown): error is CompleteTaskWithOutput {
return error instanceof Error && error.name === "CompleteTaskWithOutput";
}
export function parseError(error: unknown): TaskRunError {
if (isInternalError(error)) {
return {
type: "INTERNAL_ERROR",
code: error.code,
message: error.message,
stackTrace: error.stack ?? "",
};
}
if (error instanceof Error) {
return {
type: "BUILT_IN_ERROR",
name: error.name,
message: error.message,
stackTrace: error.stack ?? "",
};
}
if (typeof error === "string") {
return {
type: "STRING_ERROR",
raw: error,
};
}
try {
return {
type: "CUSTOM_ERROR",
raw: JSON.stringify(error),
};
} catch (e) {
return {
type: "CUSTOM_ERROR",
raw: String(error),
};
}
}
export function createErrorTaskError(error: TaskRunError): any {
switch (error.type) {
case "BUILT_IN_ERROR": {
const e = new Error(error.message);
e.name = error.name;
e.stack = error.stackTrace;
return e;
}
case "STRING_ERROR": {
return error.raw;
}
case "CUSTOM_ERROR": {
return JSON.parse(error.raw);
}
case "INTERNAL_ERROR": {
const e = new Error(error.message ?? `Internal error (${error.code})`);
e.name = error.code;
e.stack = error.stackTrace;
return e;
}
}
}
export function createJsonErrorObject(error: TaskRunError): SerializedError {
const enhancedError = taskRunErrorEnhancer(error);
switch (enhancedError.type) {
case "BUILT_IN_ERROR": {
return {
name: enhancedError.name,
message: enhancedError.message,
stackTrace: enhancedError.stackTrace,
};
}
case "STRING_ERROR": {
return {
message: enhancedError.raw,
};
}
case "CUSTOM_ERROR": {
return {
message: enhancedError.raw,
};
}
case "INTERNAL_ERROR": {
return {
message: `trigger.dev internal error (${enhancedError.code})`,
};
}
}
}
// Removes any null characters from the error message
export function sanitizeError(error: TaskRunError): TaskRunError {
switch (error.type) {
case "BUILT_IN_ERROR": {
return {
type: "BUILT_IN_ERROR",
message: error.message?.replace(/\0/g, ""),
name: error.name?.replace(/\0/g, ""),
stackTrace: error.stackTrace?.replace(/\0/g, ""),
};
}
case "STRING_ERROR": {
return {
type: "STRING_ERROR",
raw: error.raw.replace(/\0/g, ""),
};
}
case "CUSTOM_ERROR": {
return {
type: "CUSTOM_ERROR",
raw: error.raw.replace(/\0/g, ""),
};
}
case "INTERNAL_ERROR": {
return {
type: "INTERNAL_ERROR",
code: error.code,
message: error.message?.replace(/\0/g, ""),
stackTrace: error.stackTrace?.replace(/\0/g, ""),
};
}
}
}
export function shouldRetryError(error: TaskRunError): boolean {
switch (error.type) {
case "INTERNAL_ERROR": {
switch (error.code) {
case "COULD_NOT_FIND_EXECUTOR":
case "COULD_NOT_FIND_TASK":
case "COULD_NOT_IMPORT_TASK":
case "CONFIGURED_INCORRECTLY":
case "TASK_ALREADY_RUNNING":
case "TASK_PROCESS_SIGKILL_TIMEOUT":
case "TASK_PROCESS_SIGSEGV":
case "TASK_PROCESS_OOM_KILLED":
case "TASK_PROCESS_MAYBE_OOM_KILLED":
case "TASK_RUN_CANCELLED":
case "MAX_DURATION_EXCEEDED":
case "DISK_SPACE_EXCEEDED":
case "OUTDATED_SDK_VERSION":
case "TASK_RUN_HEARTBEAT_TIMEOUT":
case "TASK_DID_CONCURRENT_WAIT":
case "RECURSIVE_WAIT_DEADLOCK":
// run engine errors
case "TASK_DEQUEUED_INVALID_STATE":
case "TASK_DEQUEUED_QUEUE_NOT_FOUND":
case "TASK_HAS_N0_EXECUTION_SNAPSHOT":
case "TASK_RUN_DEQUEUED_MAX_RETRIES":
return false;
//new heartbeat error
//todo
case "TASK_RUN_STALLED_EXECUTING":
case "TASK_RUN_STALLED_EXECUTING_WITH_WAITPOINTS":
case "GRACEFUL_EXIT_TIMEOUT":
case "HANDLE_ERROR_ERROR":
case "TASK_INPUT_ERROR":
case "TASK_OUTPUT_ERROR":
case "TASK_MIDDLEWARE_ERROR":
case "POD_EVICTED":
case "POD_UNKNOWN_ERROR":
case "TASK_EXECUTION_ABORTED":
case "TASK_EXECUTION_FAILED":
case "TASK_RUN_CRASHED":
case "TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE":
case "TASK_PROCESS_SIGTERM":
return true;
default:
assertExhaustive(error.code);
}
}
case "STRING_ERROR": {
return true;
}
case "BUILT_IN_ERROR": {
return true;
}
case "CUSTOM_ERROR": {
return true;
}
default: {
assertExhaustive(error);
}
}
}
export function shouldLookupRetrySettings(error: TaskRunError): boolean {
switch (error.type) {
case "INTERNAL_ERROR": {
switch (error.code) {
case "TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE":
case "TASK_PROCESS_SIGTERM":
return true;
default:
return false;
}
}
case "STRING_ERROR": {
return false;
}
case "BUILT_IN_ERROR": {
return false;
}
case "CUSTOM_ERROR": {
return false;
}
default: {
assertExhaustive(error);
}
}
}
export function correctErrorStackTrace(
stackTrace: string,
projectDir?: string,
options?: { removeFirstLine?: boolean; isDev?: boolean }
) {
const [errorLine, ...traceLines] = stackTrace.split("\n");
return [
options?.removeFirstLine ? undefined : errorLine,
...traceLines.map((line) => correctStackTraceLine(line, projectDir, options?.isDev)),
]
.filter(Boolean)
.join("\n");
}
const LINES_TO_IGNORE = [
/ConsoleInterceptor/,
/TriggerTracer/,
/TaskExecutor/,
/EXECUTE_TASK_RUN/,
/@trigger.dev\/core/,
/packages\/core\/src\/v3/,
/safeJsonProcess/,
/__entryPoint.ts/,
/ZodIpc/,
/startActiveSpan/,
/processTicksAndRejections/,
];
function correctStackTraceLine(line: string, projectDir?: string, isDev?: boolean) {
if (LINES_TO_IGNORE.some((regex) => regex.test(line))) {
return;
}
// Check to see if the path is inside the project directory
if (isDev && projectDir && !line.includes(projectDir)) {
return;
}
return line.trim();
}
export function groupTaskMetadataIssuesByTask(tasks: any, issues: z.ZodIssue[]) {
return issues.reduce(
(acc, issue) => {
if (issue.path.length === 0) {
return acc;
}
const taskIndex = issue.path[2];
if (typeof taskIndex !== "number") {
return acc;
}
const task = tasks[taskIndex];
if (!task) {
return acc;
}
const restOfPath = issue.path.slice(3);
const taskId = task.id;
const taskName = task.exportName;
const filePath = task.filePath;
const key = taskIndex;
const existing = acc[key] ?? {
id: taskId,
exportName: taskName,
filePath,
issues: [] as Array<{ message: string; path?: string }>,
};
existing.issues.push({
message: issue.message,
path: restOfPath.length === 0 ? undefined : restOfPath.join("."),
});
return {
...acc,
[key]: existing,
};
},
{} as Record<
number,
{
id: any;
exportName: string;
filePath: string;
issues: Array<{ message: string; path?: string }>;
}
>
);
}
export class UncaughtExceptionError extends Error {
constructor(
public readonly originalError: { name: string; message: string; stack?: string },
public readonly origin: "uncaughtException" | "unhandledRejection"
) {
super(`Uncaught exception: ${originalError.message}`);
this.name = "UncaughtExceptionError";
}
}
export class TaskMetadataParseError extends Error {
constructor(
public readonly zodIssues: z.ZodIssue[],
public readonly tasks: any
) {
super(`Failed to parse task metadata`);
this.name = "TaskMetadataParseError";
}
}
export class TaskIndexingImportError extends Error {
constructor(
public readonly importErrors: ImportTaskFileErrors,
public readonly manifest: WorkerManifest
) {
super(`Failed to import some task files`);
this.name = "TaskIndexingImportError";
}
}
export class UnexpectedExitError extends Error {
constructor(
public code: number,
public signal: NodeJS.Signals | null,
public stderr: string | undefined
) {
super(`Unexpected exit with code ${code} after signal ${signal}`);
this.name = "UnexpectedExitError";
}
}
export class CleanupProcessError extends Error {
constructor() {
super("Cancelled");
this.name = "CleanupProcessError";
}
}
export class SuspendedProcessError extends Error {
constructor() {
super("Suspended");
this.name = "SuspendedProcessError";
}
}
export class CancelledProcessError extends Error {
constructor() {
super("Cancelled");
this.name = "CancelledProcessError";
}
}
export class SigKillTimeoutProcessError extends Error {
constructor() {
super("Process kill timeout");
this.name = "SigKillTimeoutProcessError";
}
}
export class GracefulExitTimeoutError extends Error {
constructor() {
super("Graceful exit timeout");
this.name = "GracefulExitTimeoutError";
}
}
type ErrorLink = {
name: string;
href: string;
// This allows us to easily add more complex logic on the frontend, e.g. display a button to open a contact form modal
magic?: "CONTACT_FORM";
};
type EnhanceError<T extends TaskRunError | ExceptionEventProperties> = T & { link?: ErrorLink };
const prettyInternalErrors: Partial<
Record<
TaskRunInternalError["code"],
{
message: string;
link?: ErrorLink;
}
>
> = {
TASK_PROCESS_OOM_KILLED: {
message:
"Your task ran out of memory. Try increasing the machine specs. If this doesn't fix it there might be a memory leak.",
link: {
name: "Machines",
href: links.docs.machines.home,
},
},
TASK_PROCESS_MAYBE_OOM_KILLED: {
message:
"We think your task ran out of memory, but we can't be certain. If this keeps happening, try increasing the machine specs.",
link: {
name: "Machines",
href: links.docs.machines.home,
},
},
TASK_PROCESS_SIGSEGV: {
message:
"Your task crashed with a segmentation fault (SIGSEGV). Most likely there's a bug in a package or binary you're using. If this keeps happening and you're unsure why, please get in touch.",
link: {
name: "Contact us",
href: links.site.contact,
magic: "CONTACT_FORM",
},
},
TASK_PROCESS_SIGTERM: {
message:
"Your task exited after receiving SIGTERM but we don't know why. If this keeps happening, please get in touch so we can investigate.",
link: {
name: "Contact us",
href: links.site.contact,
magic: "CONTACT_FORM",
},
},
OUTDATED_SDK_VERSION: {
message:
"Your task is using an outdated version of the SDK. Please upgrade to the latest version.",
link: {
name: "Beta upgrade guide",
href: links.docs.upgrade.beta,
},
},
TASK_DID_CONCURRENT_WAIT: {
message:
"Parallel waits are not supported, e.g. using Promise.all() around our wait functions.",
link: {
name: "Read the docs for solutions",
href: links.docs.troubleshooting.concurrentWaits,
},
},
RECURSIVE_WAIT_DEADLOCK: {
message:
"This run will never execute because it was triggered recursively and the task has no remaining concurrency available.",
link: {
name: "See docs for help",
href: links.docs.concurrency.recursiveDeadlock,
},
},
};
const getPrettyTaskRunError = (code: TaskRunInternalError["code"]): TaskRunInternalError => {
return {
type: "INTERNAL_ERROR" as const,
code,
...prettyInternalErrors[code],
};
};
const getPrettyExceptionEvent = (code: TaskRunInternalError["code"]): ExceptionEventProperties => {
return {
type: code,
...prettyInternalErrors[code],
};
};
const findSignalInMessage = (message?: string, truncateLength = 100) => {
if (!message) {
return;
}
const trunc = truncateLength ? message.slice(0, truncateLength) : message;
if (trunc.includes("SIGTERM")) {
return "SIGTERM";
} else if (trunc.includes("SIGSEGV")) {
return "SIGSEGV";
} else if (trunc.includes("SIGKILL")) {
return "SIGKILL";
} else if (trunc.includes("SIGABRT")) {
return "SIGABRT";
} else {
return;
}
};
export function taskRunErrorEnhancer(error: TaskRunError): EnhanceError<TaskRunError> {
switch (error.type) {
case "BUILT_IN_ERROR": {
if (error.name === "UnexpectedExitError") {
if (error.message.startsWith("Unexpected exit with code -1")) {
const signal = findSignalInMessage(error.stackTrace);
switch (signal) {
case "SIGTERM":
return {
...getPrettyTaskRunError("TASK_PROCESS_SIGTERM"),
};
case "SIGSEGV":
return {
...getPrettyTaskRunError("TASK_PROCESS_SIGSEGV"),
};
case "SIGKILL":
return {
...getPrettyTaskRunError("TASK_PROCESS_MAYBE_OOM_KILLED"),
};
case "SIGABRT":
return {
...getPrettyTaskRunError("TASK_PROCESS_MAYBE_OOM_KILLED"),
};
default:
return {
...getPrettyTaskRunError("TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE"),
message: error.message,
stackTrace: error.stackTrace,
};
}
}
}
if (error.name === "Error") {
if (error.message === "ffmpeg was killed with signal SIGKILL") {
return {
...getPrettyTaskRunError("TASK_PROCESS_OOM_KILLED"),
};
}
}
if (isManualOutOfMemoryError(error)) {
return {
...getPrettyTaskRunError("TASK_PROCESS_OOM_KILLED"),
};
}
if (error.name === "TriggerApiError") {
if (error.message.startsWith("Deadlock detected:")) {
return {
type: "BUILT_IN_ERROR",
name: "Concurrency Deadlock Error",
message: error.message,
stackTrace: "",
link: {
name: "Read the docs",
href: links.docs.concurrency.deadlock,
},
};
}
}
break;
}
case "STRING_ERROR": {
break;
}
case "CUSTOM_ERROR": {
break;
}
case "INTERNAL_ERROR": {
if (error.code === TaskRunErrorCodes.TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE) {
const signal = findSignalInMessage(error.message);
switch (signal) {
case "SIGTERM":
return {
...getPrettyTaskRunError("TASK_PROCESS_SIGTERM"),
};
case "SIGSEGV":
return {
...getPrettyTaskRunError("TASK_PROCESS_SIGSEGV"),
};
case "SIGKILL":
return {
...getPrettyTaskRunError("TASK_PROCESS_MAYBE_OOM_KILLED"),
};
case "SIGABRT":
return {
...getPrettyTaskRunError("TASK_PROCESS_MAYBE_OOM_KILLED"),
};
default: {
return {
...getPrettyTaskRunError("TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE"),
message: error.message,
stackTrace: error.stackTrace,
};
}
}
}
return {
...error,
...getPrettyTaskRunError(error.code),
};
}
}
return error;
}
export function exceptionEventEnhancer(
exception: ExceptionEventProperties
): EnhanceError<ExceptionEventProperties> {
switch (exception.type) {
case "UnexpectedExitError": {
if (exception.message?.startsWith("Unexpected exit with code -1")) {
return {
...exception,
...prettyInternalErrors.TASK_PROCESS_MAYBE_OOM_KILLED,
};
}
break;
}
case "Internal error": {
if (exception.message?.startsWith(TaskRunErrorCodes.TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE)) {
const signal = findSignalInMessage(exception.message);
switch (signal) {
case "SIGTERM":
return {
...exception,
...getPrettyExceptionEvent("TASK_PROCESS_SIGTERM"),
};
case "SIGSEGV":
return {
...exception,
...getPrettyExceptionEvent("TASK_PROCESS_SIGSEGV"),
};
case "SIGKILL":
return {
...exception,
...getPrettyExceptionEvent("TASK_PROCESS_MAYBE_OOM_KILLED"),
};
case "SIGABRT":
return {
...exception,
...getPrettyExceptionEvent("TASK_PROCESS_MAYBE_OOM_KILLED"),
};
default:
return exception;
}
} else if (exception.message?.includes(TaskRunErrorCodes.RECURSIVE_WAIT_DEADLOCK)) {
return {
...exception,
...prettyInternalErrors.RECURSIVE_WAIT_DEADLOCK,
};
}
break;
}
case "Error": {
if (exception.message === "ffmpeg was killed with signal SIGKILL") {
return {
...exception,
...prettyInternalErrors.TASK_PROCESS_OOM_KILLED,
};
}
break;
}
case TaskRunErrorCodes.TASK_PROCESS_MAYBE_OOM_KILLED:
case TaskRunErrorCodes.TASK_PROCESS_OOM_KILLED:
case TaskRunErrorCodes.TASK_PROCESS_SIGTERM: {
return {
...exception,
...getPrettyExceptionEvent(exception.type),
};
}
}
return exception;
}
export function internalErrorFromUnexpectedExit(
error: UnexpectedExitError,
dockerMode = true
): TaskRunInternalError {
const internalError = {
type: "INTERNAL_ERROR",
code: TaskRunErrorCodes.TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE,
message: `Process exited with code ${error.code} after signal ${error.signal}.`,
stackTrace: error.stderr,
} satisfies TaskRunInternalError;
if (error.code === 137) {
if (dockerMode) {
return {
...internalError,
code: TaskRunErrorCodes.TASK_PROCESS_OOM_KILLED,
};
} else {
// Note: containerState reason and message could be checked to clarify the error, maybe the task monitor should be allowed to override these
return {
...internalError,
code: TaskRunErrorCodes.TASK_PROCESS_MAYBE_OOM_KILLED,
};
}
}
if (error.stderr?.includes("OOMErrorHandler")) {
return {
...internalError,
code: TaskRunErrorCodes.TASK_PROCESS_OOM_KILLED,
};
}
if (error.signal === "SIGTERM") {
return {
...internalError,
code: TaskRunErrorCodes.TASK_PROCESS_SIGTERM,
};
}
return {
...internalError,
code: TaskRunErrorCodes.TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE,
};
}
export function serializeIndexingError(error: unknown, stderr?: string): DeploymentErrorData {
if (error instanceof TaskMetadataParseError) {
return {
name: "TaskMetadataParseError",
message: "There was an error parsing the task metadata",
stack: JSON.stringify({ zodIssues: error.zodIssues, tasks: error.tasks }),
stderr,
};
} else if (error instanceof TaskIndexingImportError) {
return {
name: "TaskIndexingImportError",
message: "There was an error importing task files",
stack: JSON.stringify(error.importErrors),
stderr,
};
} else if (error instanceof UncaughtExceptionError) {
const originalError = error.originalError;
return {
name: originalError.name,
message: originalError.message,
stack: originalError.stack,
stderr,
};
} else if (error instanceof Error) {
return {
name: error.name,
message: error.message,
stack: error.stack,
stderr,
};
}
return {
name: "UnknownError",
message: String(error),
stderr,
};
}
export function prepareDeploymentError(
errorData: DeploymentErrorData
): DeploymentErrorData | undefined {
if (!errorData) {
return;
}
if (errorData.name === "TaskMetadataParseError") {
const errorJson = tryJsonParse(errorData.stack);
if (errorJson) {
const parsedError = TaskMetadataFailedToParseData.safeParse(errorJson);
if (parsedError.success) {
return {
name: errorData.name,
message: errorData.message,
stack: createTaskMetadataFailedErrorStack(parsedError.data),
stderr: errorData.stderr,
};
} else {
return {
name: errorData.name,
message: errorData.message,
stderr: errorData.stderr,
};
}
} else {
return {
name: errorData.name,
message: errorData.message,
stderr: errorData.stderr,
};
}
} else if (errorData.name === "TaskIndexingImportError") {
const errorJson = tryJsonParse(errorData.stack);
if (errorJson) {
const parsedError = ImportTaskFileErrors.safeParse(errorJson);
if (parsedError.success) {
return {
name: errorData.name,
message: errorData.message,
stack: parsedError.data
.map((error) => {
return `x ${error.message} in ${error.file}`;
})
.join("\n"),
stderr: errorData.stderr,
};
} else {
return {
name: errorData.name,
message: errorData.message,
stderr: errorData.stderr,
};
}
} else {
return {