If an exception is thrown inside makeCommandResponse the error will be simply logged in onHandleRequestException the connection will be closed and the client will receive a network error instead of the original one.
I think this behavior is misleading and makes very difficult to debug even simple errors.
For instance, if the size of the generated response for the command is bigger than 16MiB, we will throw a BSONObjectTooLarge when we try to serialize it. In this case I would expect this error to be propagated to the client rather than simply closing the connection.
Example:
[js_test:shard_collection_basic] d22020| 2022-07-19T13:06:48.464+00:00 E COMMAND 4879802 [conn1] Failed to handle request{"error":"BSONObjectTooLarge: BSON size limit hit while building Message. Size: 26214739 (0x1900153); maxSize: 16793600(16MB)"} [js_test:shard_collection_basic] d22020| 2022-07-19T13:06:48.528+00:00 I NETWORK 22944 [conn1] Connection ended{"remote":"127.0.0.1:41766","uuid":"7307a252-7dfb-46ac-af96-f07b460bb18b","connectionId":1,"connectionCount":13} [js_test:shard_collection_basic] uncaught exception: Error: error doing query: failed: network error while attempting to run command 'autoSplitVector' on host '127.0.0.1:22020' :