Skip to content

Commit

Permalink
Handle and log BG challenge errors (#41)
Browse files Browse the repository at this point in the history
* Properly log BG creation error

* Handle exceptions from generatePoToken

* Fix logging more

* lint fix

* Pass error code to fetch

* Add error cause
  • Loading branch information
Brainicism authored Dec 26, 2024
1 parent 9237eec commit 8166ea8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
36 changes: 24 additions & 12 deletions server/src/generate_once.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,32 @@ const options = program.opts();
visitIdentifier = generatedVisitorData;
}

const sessionData = await sessionManager.generatePoToken(
visitIdentifier,
proxy,
);

try {
fs.writeFileSync(
CACHE_PATH,
JSON.stringify(sessionManager.getYoutubeSessionDataCaches(true)),
"utf8",
const sessionData = await sessionManager.generatePoToken(
visitIdentifier,
proxy,
);

try {
fs.writeFileSync(
CACHE_PATH,
JSON.stringify(
sessionManager.getYoutubeSessionDataCaches(true),
),
"utf8",
);
} catch (e) {
console.warn(
`Error writing cache. err.name = ${e.name}. err.message = ${e.message}. err.stack = ${e.stack}`,
);
} finally {
console.log(JSON.stringify(sessionData));
}
} catch (e) {
console.warn(`Error writing cache. e = ${e}`);
} finally {
console.log(JSON.stringify(sessionData));
console.error(
`Failed while generating POT. err.name = ${e.name}. err.message = ${e.message}. err.stack = ${e.stack}`,
);
console.log(JSON.stringify({}));
process.exit(1);
}
})();
24 changes: 16 additions & 8 deletions server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,22 @@ httpServer.post("/get_pot", async (request, response) => {
visitIdentifier = generatedVisitorData;
}

const sessionData = await sessionManager.generatePoToken(
visitIdentifier,
proxy,
);
response.send({
po_token: sessionData.poToken,
visit_identifier: sessionData.visitIdentifier,
});
try {
const sessionData = await sessionManager.generatePoToken(
visitIdentifier,
proxy,
);

response.send({
po_token: sessionData.poToken,
visit_identifier: sessionData.visitIdentifier,
});
} catch (e) {
console.error(
`Failed while generating POT. err.name = ${e.name}. err.message = ${e.message}. err.stack = ${e.stack}`,
);
response.status(500).send({ error: JSON.stringify(e) });
}
});

httpServer.post("/invalidate_caches", async (request, response) => {
Expand Down
8 changes: 5 additions & 3 deletions server/src/session_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ export class SessionManager {
return response.data;
},
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e) {
return {
ok: false,
json: async () => {
return null;
},
status: e.response?.status || e.code,
};
}
},
Expand All @@ -201,7 +201,8 @@ export class SessionManager {
challenge = await BG.Challenge.create(bgConfig);
} catch (e) {
throw new Error(
`Error while attempting to retrieve BG challenge. err = ${e}`,
`Error while attempting to retrieve BG challenge. err = ${JSON.stringify(e)}`,
{ cause: e },
);
}
if (!challenge) throw new Error("Could not get Botguard challenge");
Expand All @@ -221,7 +222,8 @@ export class SessionManager {
});
} catch (e) {
throw new Error(
`Error while trying to generate PO token. e = ${e}`,
`Error while trying to generate PO token. err.name = ${e.name}. err.message = ${e.message}. err.stack = ${e.stack}`,
{ cause: e },
);
}

Expand Down

0 comments on commit 8166ea8

Please sign in to comment.