forked from temporalio/sdk-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.ts
70 lines (59 loc) · 2.12 KB
/
errors.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
64
65
66
67
68
69
70
import { IllegalStateError } from '@temporalio/common';
const isShutdownError: unique symbol = Symbol.for('__temporal_isShutdownError');
/**
* The worker has been shut down
*/
export class ShutdownError extends Error {
public readonly name = 'ShutdownError';
/**
* Marker to determine whether an error is an instance of TerminatedFailure.
*/
protected readonly [isShutdownError] = true;
/**
* Instanceof check that works when multiple versions of @temporalio/core-bridge are installed.
*/
static is(error: unknown): error is ShutdownError {
return error instanceof ShutdownError || (error as any)?.[isShutdownError] === true;
}
}
/**
* Thrown after shutdown was requested as a response to a poll function, JS should stop polling
* once this error is encountered
*/
export class TransportError extends Error {
public readonly name = 'TransportError';
}
/**
* Something unexpected happened, considered fatal
*/
export class UnexpectedError extends Error {
public readonly name = 'UnexpectedError';
}
export { IllegalStateError };
export function convertFromNamedError(e: unknown, keepStackTrace: boolean): unknown {
// Check if the error's class is exactly Error (not a descendant of it).
// The instanceof check both ensure that e is indeed an object AND avoid
// TypeScript from complaining on accessing Error properties.
if (e instanceof Error && Object.getPrototypeOf(e).name === 'Error') {
let newerr: Error;
switch (e.name) {
case 'TransportError':
newerr = new TransportError(e.message);
newerr.stack = keepStackTrace ? e.stack : undefined;
return newerr;
case 'IllegalStateError':
newerr = new IllegalStateError(e.message);
newerr.stack = keepStackTrace ? e.stack : undefined;
return newerr;
case 'ShutdownError':
newerr = new ShutdownError(e.message);
newerr.stack = keepStackTrace ? e.stack : undefined;
return newerr;
case 'UnexpectedError':
newerr = new UnexpectedError(e.message);
newerr.stack = keepStackTrace ? e.stack : undefined;
return newerr;
}
}
return e;
}