Merge pull request #718 from hydralauncher/feat/better-api-logs-and-handle-401

feat: better api logs and handle 401
This commit is contained in:
Zamitto 2024-06-28 16:42:42 -03:00 committed by GitHub
commit c12f16f59e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 59 additions and 31 deletions

View file

@ -24,7 +24,7 @@ const getMe = async (
return me;
})
.catch((err) => {
logger.error("getMe", err);
logger.error("getMe", err.message);
return userAuthRepository.findOne({ where: { id: 1 } });
});
};

View file

@ -11,20 +11,22 @@ const autoLaunch = async (
) => {
if (!app.isPackaged) return;
const appLauncher = new AutoLaunch({
name: app.getName(),
});
if (process.platform == "win32") {
const destination = path.join(windowsStartupPath, "hydralauncher.vbs");
const destination = path.join(windowsStartupPath, "Hydra.vbs");
if (enabled) {
const scriptPath = path.join(process.resourcesPath, "hydralauncher.vbs");
fs.copyFileSync(scriptPath, destination);
} else {
appLauncher.disable().catch();
fs.rmSync(destination);
}
} else {
const appLauncher = new AutoLaunch({
name: app.getName(),
});
if (enabled) {
appLauncher.enable().catch();
} else {

View file

@ -90,7 +90,21 @@ export class HydraApi {
return response;
},
(error) => {
logger.error("response error", error);
logger.error(" ---- RESPONSE ERROR -----");
const { config } = error;
logger.error(config.method, config.baseURL, config.url, config.headers);
if (error.response) {
logger.error(error.response.status, error.response.data);
} else if (error.request) {
logger.error(error.request);
} else {
logger.error("Error", error.message);
}
logger.error(" ----- END RESPONSE ERROR -------");
return Promise.reject(error);
}
);
@ -146,24 +160,7 @@ export class HydraApi {
["id"]
);
} catch (err) {
if (
err instanceof AxiosError &&
(err?.response?.status === 401 || err?.response?.status === 403)
) {
this.userAuth = {
authToken: "",
expirationTimestamp: 0,
refreshToken: "",
};
userAuthRepository.delete({ id: 1 });
this.sendSignOutEvent();
logger.log("user refresh token expired");
}
throw err;
this.handleUnauthorizedError(err);
}
}
}
@ -176,28 +173,54 @@ export class HydraApi {
};
}
private static handleUnauthorizedError = (err) => {
if (err instanceof AxiosError && err.response?.status === 401) {
this.userAuth = {
authToken: "",
expirationTimestamp: 0,
refreshToken: "",
};
userAuthRepository.delete({ id: 1 });
this.sendSignOutEvent();
}
throw err;
};
static async get(url: string) {
await this.revalidateAccessTokenIfExpired();
return this.instance.get(url, this.getAxiosConfig());
return this.instance
.get(url, this.getAxiosConfig())
.catch(this.handleUnauthorizedError);
}
static async post(url: string, data?: any) {
await this.revalidateAccessTokenIfExpired();
return this.instance.post(url, data, this.getAxiosConfig());
return this.instance
.post(url, data, this.getAxiosConfig())
.catch(this.handleUnauthorizedError);
}
static async put(url: string, data?: any) {
await this.revalidateAccessTokenIfExpired();
return this.instance.put(url, data, this.getAxiosConfig());
return this.instance
.put(url, data, this.getAxiosConfig())
.catch(this.handleUnauthorizedError);
}
static async patch(url: string, data?: any) {
await this.revalidateAccessTokenIfExpired();
return this.instance.patch(url, data, this.getAxiosConfig());
return this.instance
.patch(url, data, this.getAxiosConfig())
.catch(this.handleUnauthorizedError);
}
static async delete(url: string) {
await this.revalidateAccessTokenIfExpired();
return this.instance.delete(url, this.getAxiosConfig());
return this.instance
.delete(url, this.getAxiosConfig())
.catch(this.handleUnauthorizedError);
}
}

View file

@ -64,7 +64,7 @@ export const mergeWithRemoteGames = async () => {
}
} catch (err) {
if (err instanceof AxiosError) {
logger.error("getRemoteGames", err.response, err.message);
logger.error("getRemoteGames", err.message);
} else {
logger.error("getRemoteGames", err);
}

View file

@ -1,6 +1,6 @@
import { UserProfile } from "@types";
import { useCallback, useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import { useNavigate, useParams } from "react-router-dom";
import { setHeaderTitle } from "@renderer/features";
import { useAppDispatch } from "@renderer/hooks";
import { UserSkeleton } from "./user-skeleton";
@ -12,6 +12,7 @@ import * as styles from "./user.css";
export const User = () => {
const { userId } = useParams();
const [userProfile, setUserProfile] = useState<UserProfile>();
const navigate = useNavigate();
const dispatch = useAppDispatch();
@ -20,6 +21,8 @@ export const User = () => {
if (userProfile) {
dispatch(setHeaderTitle(userProfile.displayName));
setUserProfile(userProfile);
} else {
navigate(-1);
}
});
}, [dispatch, userId]);