Log error responses from API when reporting state

This adds the error message from the API to journal logs to better
identify those cases where patching to the API fails.

Change-type: patch
Relates-to: #1680
This commit is contained in:
Felipe Lalanne 2021-05-03 18:08:24 +00:00
parent e745648b5e
commit 2203f78d51

View File

@ -34,8 +34,8 @@ const stateForReport: DeviceStatus = {
let reportPending = false;
class StatusError extends Error {
constructor(public statusCode: number) {
super();
constructor(public statusCode: number, public statusMessage?: string) {
super(statusMessage);
}
}
@ -86,13 +86,13 @@ const sendReportPatch = async (
body,
};
const [{ statusCode }] = await request
const [{ statusCode, body: statusMessage }] = await request
.patchAsync(endpoint, params)
.timeout(apiTimeout);
if (statusCode < 200 || statusCode >= 300) {
log.error(`Error from the API: ${statusCode}`);
throw new StatusError(statusCode);
throw new StatusError(statusCode, JSON.stringify(statusMessage, null, 2));
}
};