feat: add logs

This commit is contained in:
Zamitto 2024-07-12 21:17:20 -03:00
parent 46b12f2bc2
commit 198a283752
3 changed files with 26 additions and 5 deletions

View file

@ -24,12 +24,18 @@ const getMe = async (
return me; return me;
}) })
.catch((err) => { .catch(async (err) => {
if (err instanceof UserNotLoggedInError) { if (err instanceof UserNotLoggedInError) {
return null; return null;
} }
return userAuthRepository.findOne({ where: { id: 1 } }); const loggedUser = await userAuthRepository.findOne({ where: { id: 1 } });
if (loggedUser) {
return { ...loggedUser, id: loggedUser.userId };
}
return null;
}); });
}; };

View file

@ -20,6 +20,8 @@ autoUpdater.setFeedURL({
autoUpdater.logger = logger; autoUpdater.logger = logger;
logger.log("Init Hydra");
const gotTheLock = app.requestSingleInstanceLock(); const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) app.quit(); if (!gotTheLock) app.quit();
@ -121,6 +123,7 @@ app.on("window-all-closed", () => {
app.on("before-quit", () => { app.on("before-quit", () => {
/* Disconnects libtorrent */ /* Disconnects libtorrent */
PythonInstance.kill(); PythonInstance.kill();
logger.log("Quit Hydra");
}); });
app.on("activate", () => { app.on("activate", () => {

View file

@ -10,7 +10,7 @@ import { UserNotLoggedInError } from "@shared";
export class HydraApi { export class HydraApi {
private static instance: AxiosInstance; private static instance: AxiosInstance;
private static readonly EXPIRATION_OFFSET_IN_MS = 1000 * 60 * 5; private static readonly EXPIRATION_OFFSET_IN_MS = 1000 * 60 * 5; // 5 minutes
private static secondsToMilliseconds = (seconds: number) => seconds * 1000; private static secondsToMilliseconds = (seconds: number) => seconds * 1000;
@ -45,6 +45,8 @@ export class HydraApi {
expirationTimestamp: tokenExpirationTimestamp, expirationTimestamp: tokenExpirationTimestamp,
}; };
logger.log("Sign in received", this.userAuth);
await userAuthRepository.upsert( await userAuthRepository.upsert(
{ {
id: 1, id: 1,
@ -74,7 +76,7 @@ export class HydraApi {
return request; return request;
}, },
(error) => { (error) => {
logger.log("request error", error); logger.error("request error", error);
return Promise.reject(error); return Promise.reject(error);
} }
); );
@ -95,7 +97,13 @@ export class HydraApi {
const { config } = error; const { config } = error;
logger.error(config.method, config.baseURL, config.url, config.headers); logger.error(
config.method,
config.baseURL,
config.url,
config.headers,
config.data
);
if (error.response) { if (error.response) {
logger.error("Response", error.response.status, error.response.data); logger.error("Response", error.response.status, error.response.data);
@ -146,6 +154,8 @@ export class HydraApi {
this.userAuth.authToken = accessToken; this.userAuth.authToken = accessToken;
this.userAuth.expirationTimestamp = tokenExpirationTimestamp; this.userAuth.expirationTimestamp = tokenExpirationTimestamp;
logger.log("Token refreshed", this.userAuth);
userAuthRepository.upsert( userAuthRepository.upsert(
{ {
id: 1, id: 1,
@ -170,6 +180,8 @@ export class HydraApi {
private static handleUnauthorizedError = (err) => { private static handleUnauthorizedError = (err) => {
if (err instanceof AxiosError && err.response?.status === 401) { if (err instanceof AxiosError && err.response?.status === 401) {
logger.error("401 - Current credentials:", this.userAuth);
this.userAuth = { this.userAuth = {
authToken: "", authToken: "",
expirationTimestamp: 0, expirationTimestamp: 0,