Skip to content

Commit af047ae

Browse files
committed
Extract checkForReportingAPIError
1 parent cf720e1 commit af047ae

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

library/agent/Agent.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,6 @@ t.test("it logs when failed to report event", async () => {
666666
t.same(logger.getMessages(), [
667667
"Starting agent...",
668668
"Found token, reporting enabled!",
669-
"Failed to start agent",
670669
"Heartbeat...",
671670
]);
672671
});

library/agent/Agent.ts

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,27 @@ export class Agent {
113113
this.timeoutInMS
114114
);
115115

116-
if (!result.success) {
117-
if (result.error === "invalid_token") {
118-
console.error(
119-
"Aikido: We were unable to connect to the Aikido platform. Please verify that your token is correct."
120-
);
121-
} else {
122-
console.error(
123-
`Aikido: Failed to connect to the Aikido platform: ${result.error}`
124-
);
125-
}
126-
}
127-
116+
this.checkForReportingAPIError(result);
128117
this.updateServiceConfig(result);
129118

130119
await this.updateBlockedLists();
131120
}
132121
}
133122

123+
checkForReportingAPIError(result: ReportingAPIResponse) {
124+
if (!result.success) {
125+
if (result.error === "invalid_token") {
126+
console.error(
127+
"Aikido: We were unable to connect to the Aikido platform. Please verify that your token is correct."
128+
);
129+
} else {
130+
console.error(
131+
`Aikido: Failed to connect to the Aikido platform: ${result.error}`
132+
);
133+
}
134+
}
135+
}
136+
134137
onErrorThrownByInterceptor({
135138
error,
136139
module,
@@ -205,11 +208,18 @@ export class Agent {
205208
this.attackLogger.log(attack);
206209

207210
if (this.token) {
208-
this.api.report(this.token, attack, this.timeoutInMS).catch((err) => {
209-
console.error(
210-
`Aikido: Failed to report attack event to Aikido platform: ${err.message}`
211-
);
212-
});
211+
this.api
212+
.report(this.token, attack, this.timeoutInMS)
213+
.catch((err) => {
214+
console.error(
215+
`Aikido: Failed to report attack event to Aikido platform: ${err.message}`
216+
);
217+
})
218+
.then((response) => {
219+
if (response) {
220+
this.checkForReportingAPIError(response);
221+
}
222+
});
213223
}
214224
}
215225

@@ -307,12 +317,7 @@ export class Agent {
307317
timeoutInMS
308318
);
309319

310-
if (!response.success && response.error === "invalid_token") {
311-
console.error(
312-
"Aikido: Unable to access the Aikido platform, please check your token."
313-
);
314-
}
315-
320+
this.checkForReportingAPIError(response);
316321
this.updateServiceConfig(response);
317322
}
318323
}

0 commit comments

Comments
 (0)