Skip to content

Commit 360202a

Browse files
refactor(express-wrapper): avoid using codec type-guard
This refactor changes the response code path to try encoding the service function's response value with the apiSpec's response codec, and if this errors returns a 500. Previously we used the codec's type-guard as a test if the codec can encode the service function's response value, but this step will take extra time and there's no hard contract that guarantees us the type-guard returning `true` really means that the codec will be able to encode a value. Instead, we just try the dangerous deed and do our best to catch errors.
1 parent 67c9267 commit 360202a

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

packages/express-wrapper/src/response.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,12 @@ export const defaultResponseEncoder: ResponseEncoder = (
3838
return;
3939
}
4040
const responseCodec = route.response[status];
41-
if (responseCodec === undefined || !responseCodec.is(payload)) {
41+
try {
42+
expressRes.status(status).json(responseCodec!.encode(payload)).end();
43+
} catch {
4244
console.warn(
4345
"Unable to encode route's return value, did you return the expected type?",
4446
);
4547
expressRes.status(500).end();
46-
return;
4748
}
48-
49-
expressRes.status(status).json(responseCodec.encode(payload)).end();
5049
};

0 commit comments

Comments
 (0)