Skip to content

Commit a627bfd

Browse files
committed
Fail requests with BAD_REQUEST if payload decoding fails
1 parent ab2645a commit a627bfd

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

packages/test/src/test-nexus-handler.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,18 @@ test('start operation handler errors', async (t) => {
390390
at ServiceRegistry.start (/Users/bergundy/temporal/nexus-sdk-typescript/src/operation.ts)`
391391
);
392392
}
393+
{
394+
const res = await fetch(`http://localhost:${httpPort}/nexus/endpoints/${endpointId}/services/testService/op`, {
395+
method: 'POST',
396+
body: 'invalid',
397+
headers: {
398+
'Content-Type': 'application/json',
399+
},
400+
});
401+
t.is(res.status, 400);
402+
const { message } = await res.json() as { message: string };
403+
t.is(message, 'Failed to deserialize input: SyntaxError: Unexpected token \'i\', "invalid" is not valid JSON');
404+
}
393405
});
394406
});
395407

packages/worker/src/nexus/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,15 @@ export class NexusHandler {
124124
options: nexus.StartOperationOptions
125125
): Promise<coresdk.nexus.INexusTaskCompletion> {
126126
try {
127-
const decoded = await decodeOptionalSingle(this.dataConverter.payloadCodecs, payload);
127+
let decoded: Payload | undefined | null;
128+
try {
129+
decoded = await decodeOptionalSingle(this.dataConverter.payloadCodecs, payload);
130+
} catch (err) {
131+
throw new nexus.HandlerError({
132+
type: 'BAD_REQUEST',
133+
message: `Failed to decode payload: ${err}`,
134+
});
135+
}
128136
// Nexus headers have string values and Temporal Payloads have binary values. Instead of converting Payload
129137
// instances into Content instances, we embed the Payload in the serializer and pretend we are deserializing an
130138
// empty Content.
@@ -452,7 +460,14 @@ class PayloadSerializer implements nexus.Serializer {
452460
if (this.payload == null) {
453461
return undefined as T;
454462
}
463+
try {
455464
return this.payloadConverter.fromPayload(this.payload);
465+
} catch (err) {
466+
throw new nexus.HandlerError({
467+
type: 'BAD_REQUEST',
468+
message: `Failed to deserialize input: ${err}`,
469+
});
470+
}
456471
}
457472

458473
/** Not used in this path */

0 commit comments

Comments
 (0)