Skip to content

Commit

Permalink
Wrap entire packet processing method in try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
Hal-9k1 committed Feb 8, 2025
1 parent b2b7765 commit c36a447
Showing 1 changed file with 60 additions and 50 deletions.
110 changes: 60 additions & 50 deletions src/main/network/RuntimeComms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,57 +266,67 @@ export default class RuntimeComms {
*/
#handlePacket(packet: Packet) {
const { type, data } = packet;
switch (type) {
case MsgType.LOG:
this.#commsListener.onReceiveRobotLogs(
protos.Text.decode(data).payload,
);
break;
case MsgType.TIME_STAMPS:
this.#commsListener.onReceiveLatency(
(Date.now() - Number(protos.TimeStamps.decode(data).dawnTimestamp)) /
2,
);
break;
case MsgType.DEVICE_DATA:
// Convert decoded Devices to DeviceInfoStates before passing to onReceiveDevices
this.#commsListener.onReceiveDevices(
protos.DevData.decode(data).devices.map(
(
deviceProps: protos.IDevice,
_devIdx: number,
_devArr: protos.IDevice[],
) => {
const device = new protos.Device(deviceProps);
return {
id: `${device.type.toString()}_${device.uid.toString()}`,
...Object.fromEntries(
device.params.map(
(
paramProps: protos.IParam,
_paramIdx: number,
_paramArr: protos.IParam[],
) => {
const param = new protos.Param(paramProps);
return param.val
? [param.name, param[param.val].toString()]
: [];
},
try {
switch (type) {
case MsgType.LOG:
this.#commsListener.onReceiveRobotLogs(
protos.Text.decode(data).payload,
);
break;
case MsgType.TIME_STAMPS:
this.#commsListener.onReceiveLatency(
(Date.now() - Number(protos.TimeStamps.decode(data).dawnTimestamp)) /

Check failure on line 278 in src/main/network/RuntimeComms.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Insert `⏎·············`
2,
);
break;
case MsgType.DEVICE_DATA:
// Convert decoded Devices to DeviceInfoStates before passing to onReceiveDevices
this.#commsListener.onReceiveDevices(
protos.DevData.decode(data).devices.map(
(
deviceProps: protos.IDevice,
_devIdx: number,
_devArr: protos.IDevice[],
) => {
const device = new protos.Device(deviceProps);
return {
id: `${device.type.toString()}_${device.uid.toString()}`,
...Object.fromEntries(
device.params.map(
(
paramProps: protos.IParam,
_paramIdx: number,
_paramArr: protos.IParam[],
) => {
const param = new protos.Param(paramProps);
return param.val
? [param.name, param[param.val].toString()]
: [];
},
),
),
),
};
},
),
);
break;
default:
this.#commsListener.onRuntimeError(
new Error(
`Received unexpected packet MsgType ${type}.\nPacket: ${JSON.stringify(
packet,
)}`,
),
);
};
},
),
);
break;
default:
this.#commsListener.onRuntimeError(
new Error(
`Received unexpected packet MsgType ${type}.\nPacket: ${JSON.stringify(
packet,
)}`,
),
);
}
} catch (e) {
this.#commsListener.onRuntimeError(
new Error(
`Uncaught error when reading packet.\nPacket: ${JSON.stringify(
packet,
)}`,
),
);
}
}

Expand Down

0 comments on commit c36a447

Please sign in to comment.