Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: Ensure data in response is always empty on error #24971

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ function getObjectProperty<T>(object: KeyValue, key: string, defaultValue: NoInf
}

function getResponse(request: KeyValue | string, data: KeyValue, error?: string): MQTTResponse {
const response: MQTTResponse = {data, status: error ? 'error' : 'ok'};
// On `error`, always return an empty `data` payload.
const response: MQTTResponse = {data: error ? {} : data, status: error ? 'error' : 'ok'};

if (error) {
response.error = error;
Expand Down
6 changes: 3 additions & 3 deletions test/extensions/configure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('Extension: Configure', () => {
await flushPromises();
expect(mockMQTT.publishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/configure',
stringify({data: {id: 'not_existing_device'}, status: 'error', error: "Device 'not_existing_device' does not exist"}),
stringify({data: {}, status: 'error', error: "Device 'not_existing_device' does not exist"}),
{retain: false, qos: 0},
);
});
Expand All @@ -175,7 +175,7 @@ describe('Extension: Configure', () => {
await flushPromises();
expect(mockMQTT.publishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/configure',
stringify({data: {id: 'remote'}, status: 'error', error: 'Failed to configure (Bind timeout after 10s)'}),
stringify({data: {}, status: 'error', error: 'Failed to configure (Bind timeout after 10s)'}),
{retain: false, qos: 0},
);
});
Expand All @@ -185,7 +185,7 @@ describe('Extension: Configure', () => {
await flushPromises();
expect(mockMQTT.publishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/configure',
stringify({data: {id: '0x0017882104a44559'}, status: 'error', error: "Device 'TS0601_thermostat' cannot be configured", transaction: 20}),
stringify({data: {}, status: 'error', error: "Device 'TS0601_thermostat' cannot be configured", transaction: 20}),
{retain: false, qos: 0},
);
});
Expand Down
10 changes: 5 additions & 5 deletions test/extensions/otaUpdate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('Extension: OTAUpdate', () => {
expect(mockMQTT.publishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb', stringify({update: {state: 'available'}}), {retain: true, qos: 0});
expect(mockMQTT.publishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/ota_update/update',
stringify({data: {id: 'bulb'}, status: 'error', error: "Update of 'bulb' failed (Update failed)"}),
stringify({data: {}, status: 'error', error: "Update of 'bulb' failed (Update failed)"}),
{retain: false, qos: 0},
);
});
Expand Down Expand Up @@ -222,7 +222,7 @@ describe('Extension: OTAUpdate', () => {
expect(mockMQTT.publishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/ota_update/check',
stringify({
data: {id: 'bulb'},
data: {},
status: 'error',
error: `Failed to check if update available for 'bulb' (RF signals disturbed because of dogs barking)`,
}),
Expand All @@ -235,7 +235,7 @@ describe('Extension: OTAUpdate', () => {
await flushPromises();
expect(mockMQTT.publishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/ota_update/check',
stringify({data: {id: 'not_existing_deviceooo'}, status: 'error', error: `Device 'not_existing_deviceooo' does not exist`}),
stringify({data: {}, status: 'error', error: `Device 'not_existing_deviceooo' does not exist`}),
{retain: false, qos: 0},
);
});
Expand All @@ -245,7 +245,7 @@ describe('Extension: OTAUpdate', () => {
await flushPromises();
expect(mockMQTT.publishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/ota_update/check',
stringify({data: {id: 'dimmer_wall_switch'}, status: 'error', error: `Device 'dimmer_wall_switch' does not support OTA updates`}),
stringify({data: {}, status: 'error', error: `Device 'dimmer_wall_switch' does not support OTA updates`}),
{retain: false, qos: 0},
);
});
Expand All @@ -268,7 +268,7 @@ describe('Extension: OTAUpdate', () => {
await flushPromises();
expect(mockMQTT.publishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/ota_update/check',
stringify({data: {id: 'bulb'}, status: 'error', error: `Update or check for update already in progress for 'bulb'`}),
stringify({data: {}, status: 'error', error: `Update or check for update already in progress for 'bulb'`}),
{retain: false, qos: 0},
);
});
Expand Down