Skip to content

Commit aab7ea1

Browse files
committed
Add always visible logs on core connection errors
1 parent 25931e8 commit aab7ea1

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

library/agent/Agent.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,6 @@ t.test("it logs when failed to report event", async () => {
668668
"Found token, reporting enabled!",
669669
"Failed to start agent",
670670
"Heartbeat...",
671-
"Failed to do heartbeat",
672-
"Failed to report attack",
673671
]);
674672
});
675673

library/agent/Agent.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable max-lines-per-function */
1+
/* eslint-disable max-lines-per-function, no-console */
22
import { hostname, platform, release } from "os";
33
import { convertRequestBodyToString } from "../helpers/convertRequestBodyToString";
44
import { getAgentVersion } from "../helpers/getAgentVersion";
@@ -113,6 +113,18 @@ export class Agent {
113113
this.timeoutInMS
114114
);
115115

116+
if (!result.success) {
117+
if (result.error === "invalid_token") {
118+
console.error(
119+
"Aikido: Unable to access the Aikido platform, please check your token."
120+
);
121+
} else {
122+
console.error(
123+
`Aikido: Failed to connect to the Aikido platform: ${result.error}`
124+
);
125+
}
126+
}
127+
116128
this.updateServiceConfig(result);
117129

118130
await this.updateBlockedLists();
@@ -193,8 +205,10 @@ export class Agent {
193205
this.attackLogger.log(attack);
194206

195207
if (this.token) {
196-
this.api.report(this.token, attack, this.timeoutInMS).catch(() => {
197-
this.logger.log("Failed to report attack");
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+
);
198212
});
199213
}
200214
}
@@ -203,8 +217,10 @@ export class Agent {
203217
* Sends a heartbeat via the API to the server (only when not in serverless mode)
204218
*/
205219
private heartbeat(timeoutInMS = this.timeoutInMS) {
206-
this.sendHeartbeat(timeoutInMS).catch(() => {
207-
this.logger.log("Failed to do heartbeat");
220+
this.sendHeartbeat(timeoutInMS).catch((err) => {
221+
console.error(
222+
`Aikido: Failed to send heartbeat event to Aikido platform: ${err.message}`
223+
);
208224
});
209225
}
210226

@@ -291,6 +307,12 @@ export class Agent {
291307
timeoutInMS
292308
);
293309

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+
294316
this.updateServiceConfig(response);
295317
}
296318
}
@@ -354,7 +376,7 @@ export class Agent {
354376
this.serviceConfig.updateBlockedIPAddresses(blockedIPAddresses);
355377
this.serviceConfig.updateBlockedUserAgents(blockedUserAgents);
356378
} catch (error: any) {
357-
this.logger.log(`Failed to update blocked lists: ${error.message}`);
379+
console.error(`Aikido: Failed to update blocked lists: ${error.message}`);
358380
}
359381
}
360382

@@ -432,7 +454,6 @@ export class Agent {
432454
this.logger.log("No token provided, disabling reporting.");
433455

434456
if (!this.block && !isAikidoCI()) {
435-
// eslint-disable-next-line no-console
436457
console.log(
437458
"AIKIDO: Running in monitoring only mode without reporting to Aikido Cloud. Set AIKIDO_BLOCK=true to enable blocking."
438459
);

library/agent/api/fetchBlockedLists.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export async function fetchBlockedLists(token: Token): Promise<{
2525
});
2626

2727
if (statusCode !== 200) {
28+
if (statusCode === 401) {
29+
throw new Error(
30+
`Unable to access the Aikido platform, please check your token.`
31+
);
32+
}
2833
throw new Error(`Failed to fetch blocked lists: ${statusCode}`);
2934
}
3035

0 commit comments

Comments
 (0)