Skip to content

Commit a33c06d

Browse files
fix(common): Improve error messages for failureConverters (#1373)
1 parent a16ab49 commit a33c06d

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

packages/common/src/internal-non-workflow/data-converter-helpers.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,37 @@ import { FailureConverter } from '../converter/failure-converter';
44
import { errorCode, hasOwnProperty, isRecord } from '../type-helpers';
55
import { ValueError } from '../errors';
66

7-
const isValidPayloadConverter = (converter: unknown): converter is PayloadConverter =>
8-
typeof converter === 'object' &&
9-
converter !== null &&
10-
['toPayload', 'fromPayload'].every((method) => typeof (converter as Record<string, unknown>)[method] === 'function');
7+
const isValidPayloadConverter = (converter: unknown, path: string): asserts converter is PayloadConverter => {
8+
const isValid =
9+
typeof converter === 'object' &&
10+
converter !== null &&
11+
['toPayload', 'fromPayload'].every(
12+
(method) => typeof (converter as Record<string, unknown>)[method] === 'function'
13+
);
14+
if (!isValid) {
15+
throw new ValueError(`payloadConverter export at ${path} must be an object with toPayload and fromPayload methods`);
16+
}
17+
};
1118

12-
const isValidFailureConverter = (converter: unknown): converter is FailureConverter =>
13-
typeof converter === 'object' &&
14-
converter !== null &&
15-
['errorToFailure', 'failureToError'].every(
16-
(method) => typeof (converter as Record<string, unknown>)[method] === 'function'
17-
);
19+
const isValidFailureConverter = (converter: unknown, path: string): asserts converter is FailureConverter => {
20+
const isValid =
21+
typeof converter === 'object' &&
22+
converter !== null &&
23+
['errorToFailure', 'failureToError'].every(
24+
(method) => typeof (converter as Record<string, unknown>)[method] === 'function'
25+
);
26+
if (!isValid) {
27+
throw new ValueError(
28+
`failureConverter export at ${path} must be an object with errorToFailure and failureToError methods`
29+
);
30+
}
31+
};
1832

19-
function requireConverter<T>(path: string, type: string, validator: (converter: unknown) => converter is T): T {
33+
function requireConverter<T>(
34+
path: string,
35+
type: string,
36+
validator: (converter: unknown, path: string) => asserts converter is T
37+
): T {
2038
let module;
2139
try {
2240
module = require(path); // eslint-disable-line @typescript-eslint/no-var-requires
@@ -29,15 +47,10 @@ function requireConverter<T>(path: string, type: string, validator: (converter:
2947

3048
if (isRecord(module) && hasOwnProperty(module, type)) {
3149
const converter = module[type];
32-
if (validator(converter)) {
33-
return converter;
34-
} else {
35-
throw new ValueError(
36-
`payloadConverter export at ${path} must be an object with toPayload and fromPayload methods`
37-
);
38-
}
50+
validator(converter, path);
51+
return converter;
3952
} else {
40-
throw new ValueError(`Module ${path} does not have a \`payloadConverter\` named export`);
53+
throw new ValueError(`Module ${path} does not have a \`${type}\` named export`);
4154
}
4255
}
4356

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const failureConverter = { toPayload: Function.prototype };

packages/test/src/test-custom-payload-converter.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,28 @@ test('Worker throws on invalid payloadConverterPath', async (t) => {
106106
message: /payloadConverter export at .* must be an object with toPayload and fromPayload methods/,
107107
}
108108
);
109+
110+
t.throws(
111+
() =>
112+
isolateFreeWorker({
113+
...defaultOptions,
114+
dataConverter: { failureConverterPath: require.resolve('./payload-converters/payload-converter-bad-export') },
115+
}),
116+
{
117+
message: /Module .* does not have a `failureConverter` named export/,
118+
}
119+
);
120+
121+
t.throws(
122+
() =>
123+
isolateFreeWorker({
124+
...defaultOptions,
125+
dataConverter: { failureConverterPath: require.resolve('./payload-converters/failure-converter-bad-export') },
126+
}),
127+
{
128+
message: /failureConverter export at .* must be an object with errorToFailure and failureToError methods/,
129+
}
130+
);
109131
});
110132

111133
test('Worker with proto data converter runs an activity and reports completion', async (t) => {

0 commit comments

Comments
 (0)