Skip to content

fix : added proper error handling to failure logs #42

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 0 additions & 34 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,6 @@ export function sanitizeUrlParam(param: string): string {
return param.replace(/[;&|`$(){}[\]<>]/g, "");
}

export interface HarFile {
log: {
entries: HarEntry[];
};
}

export interface HarEntry {
startedDateTime: string;
request: {
method: string;
url: string;
queryString?: { name: string; value: string }[];
};
response: {
status: number;
statusText?: string;
_error?: string;
};
serverIPAddress?: string;
time?: number;
}

const ONE_MB = 1048576;

//Compresses a base64 image intelligently to keep it under 1 MB if needed.
Expand Down Expand Up @@ -56,15 +34,3 @@ export async function assertOkResponse(response: Response, action: string) {
);
}
}

export function filterLinesByKeywords(
logText: string,
keywords: string[],
): string[] {
return logText
.split(/\r?\n/)
.map((line) => line.trim())
.filter((line) =>
keywords.some((keyword) => line.toLowerCase().includes(keyword)),
);
}
37 changes: 26 additions & 11 deletions src/tools/failurelogs-utils/app-automate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import config from "../../config.js";
import { assertOkResponse, filterLinesByKeywords } from "../../lib/utils.js";
import {
filterLinesByKeywords,
validateLogResponse,
LogResponse,
} from "./utils.js";

const auth = Buffer.from(
`${config.browserstackUsername}:${config.browserstackAccessKey}`,
Expand All @@ -9,7 +13,7 @@ const auth = Buffer.from(
export async function retrieveDeviceLogs(
sessionId: string,
buildId: string,
): Promise<string[]> {
): Promise<string> {
const url = `https://api.browserstack.com/app-automate/builds/${buildId}/sessions/${sessionId}/deviceLogs`;

const response = await fetch(url, {
Expand All @@ -19,17 +23,21 @@ export async function retrieveDeviceLogs(
},
});

await assertOkResponse(response, "device logs");
const validationError = validateLogResponse(response, "device logs");
if (validationError) return validationError.message!;

const logText = await response.text();
return filterDeviceFailures(logText);
const logs = filterDeviceFailures(logText);
return logs.length > 0
? `Device Failures (${logs.length} found):\n${JSON.stringify(logs, null, 2)}`
: "No device failures found";
}

// APPIUM LOGS
export async function retrieveAppiumLogs(
sessionId: string,
buildId: string,
): Promise<string[]> {
): Promise<string> {
const url = `https://api.browserstack.com/app-automate/builds/${buildId}/sessions/${sessionId}/appiumlogs`;

const response = await fetch(url, {
Expand All @@ -39,17 +47,21 @@ export async function retrieveAppiumLogs(
},
});

await assertOkResponse(response, "Appium logs");
const validationError = validateLogResponse(response, "Appium logs");
if (validationError) return validationError.message!;

const logText = await response.text();
return filterAppiumFailures(logText);
const logs = filterAppiumFailures(logText);
return logs.length > 0
? `Appium Failures (${logs.length} found):\n${JSON.stringify(logs, null, 2)}`
: "No Appium failures found";
}

// CRASH LOGS
export async function retrieveCrashLogs(
sessionId: string,
buildId: string,
): Promise<string[]> {
): Promise<string> {
const url = `https://api.browserstack.com/app-automate/builds/${buildId}/sessions/${sessionId}/crashlogs`;

const response = await fetch(url, {
Expand All @@ -59,14 +71,17 @@ export async function retrieveCrashLogs(
},
});

await assertOkResponse(response, "crash logs");
const validationError = validateLogResponse(response, "crash logs");
if (validationError) return validationError.message!;

const logText = await response.text();
return filterCrashFailures(logText);
const logs = filterCrashFailures(logText);
return logs.length > 0
? `Crash Failures (${logs.length} found):\n${JSON.stringify(logs, null, 2)}`
: "No crash failures found";
}

// FILTER HELPERS

export function filterDeviceFailures(logText: string): string[] {
const keywords = [
"error",
Expand Down
83 changes: 47 additions & 36 deletions src/tools/failurelogs-utils/automate.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import config from "../../config.js";
import { HarEntry, HarFile } from "../../lib/utils.js";
import { assertOkResponse, filterLinesByKeywords } from "../../lib/utils.js";
import {
HarEntry,
HarFile,
filterLinesByKeywords,
validateLogResponse,
} from "./utils.js";

const auth = Buffer.from(
`${config.browserstackUsername}:${config.browserstackAccessKey}`,
).toString("base64");

// NETWORK LOGS
export async function retrieveNetworkFailures(sessionId: string): Promise<any> {
export async function retrieveNetworkFailures(
sessionId: string,
): Promise<string> {
const url = `https://api.browserstack.com/automate/sessions/${sessionId}/networklogs`;

const response = await fetch(url, {
Expand All @@ -18,43 +24,40 @@ export async function retrieveNetworkFailures(sessionId: string): Promise<any> {
},
});

await assertOkResponse(response, "network logs");
const validationError = validateLogResponse(response, "network logs");
if (validationError) return validationError.message!;

const networklogs: HarFile = await response.json();

// Filter for failure logs
const failureEntries: HarEntry[] = networklogs.log.entries.filter(
(entry: HarEntry) => {
return (
entry.response.status === 0 ||
entry.response.status >= 400 ||
entry.response._error !== undefined
);
},
(entry: HarEntry) =>
entry.response.status === 0 ||
entry.response.status >= 400 ||
entry.response._error !== undefined
);

// Return only the failure entries with some context
return failureEntries.map((entry: any) => ({
startedDateTime: entry.startedDateTime,
request: {
method: entry.request?.method,
url: entry.request?.url,
queryString: entry.request?.queryString,
},
response: {
status: entry.response?.status,
statusText: entry.response?.statusText,
_error: entry.response?._error,
},
serverIPAddress: entry.serverIPAddress,
time: entry.time,
}));

return failureEntries.length > 0
? `Network Failures (${failureEntries.length} found):\n${JSON.stringify(failureEntries.map((entry: any) => ({
startedDateTime: entry.startedDateTime,
request: {
method: entry.request?.method,
url: entry.request?.url,
queryString: entry.request?.queryString,
},
response: {
status: entry.response?.status,
statusText: entry.response?.statusText,
_error: entry.response?._error,
},
serverIPAddress: entry.serverIPAddress,
time: entry.time,
})), null, 2)}`
: "No network failures found";
}

// SESSION LOGS
export async function retrieveSessionFailures(
sessionId: string,
): Promise<string[]> {
): Promise<string> {
const url = `https://api.browserstack.com/automate/sessions/${sessionId}/logs`;

const response = await fetch(url, {
Expand All @@ -64,16 +67,20 @@ export async function retrieveSessionFailures(
},
});

await assertOkResponse(response, "session logs");
const validationError = validateLogResponse(response, "session logs");
if (validationError) return validationError.message!;

const logText = await response.text();
return filterSessionFailures(logText);
const logs = filterSessionFailures(logText);
return logs.length > 0
? `Session Failures (${logs.length} found):\n${JSON.stringify(logs, null, 2)}`
: "No session failures found";
}

// CONSOLE LOGS
export async function retrieveConsoleFailures(
sessionId: string,
): Promise<string[]> {
): Promise<string> {
const url = `https://api.browserstack.com/automate/sessions/${sessionId}/consolelogs`;

const response = await fetch(url, {
Expand All @@ -83,10 +90,14 @@ export async function retrieveConsoleFailures(
},
});

await assertOkResponse(response, "console logs");
const validationError = validateLogResponse(response, "console logs");
if (validationError) return validationError.message!;

const logText = await response.text();
return filterConsoleFailures(logText);
const logs = filterConsoleFailures(logText);
return logs.length > 0
? `Console Failures (${logs.length} found):\n${JSON.stringify(logs, null, 2)}`
: "No console failures found";
}

// FILTER: session logs
Expand Down
56 changes: 56 additions & 0 deletions src/tools/failurelogs-utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export interface LogResponse {
logs?: any[];
message?: string;
}

export interface HarFile {
log: {
entries: HarEntry[];
};
}

export interface HarEntry {
startedDateTime: string;
request: {
method: string;
url: string;
queryString?: { name: string; value: string }[];
};
response: {
status: number;
statusText?: string;
_error?: string;
};
serverIPAddress?: string;
time?: number;
}

export function validateLogResponse(
response: Response,
logType: string,
): LogResponse | null {
if (!response.ok) {
if (response.status === 404) {
return { message: `No ${logType} available for this session` };
}
if (response.status === 401 || response.status === 403) {
return {
message: `Unable to access ${logType} - please check your credentials`,
};
}
return { message: `Unable to fetch ${logType} at this time` };
}
return null;
}

export function filterLinesByKeywords(
logText: string,
keywords: string[],
): string[] {
return logText
.split(/\r?\n/)
.map((line) => line.trim())
.filter((line) =>
keywords.some((keyword) => line.toLowerCase().includes(keyword)),
);
}
Loading