From b5fb5bbba26868f9d68d94ef2a8c8afb1972fcc7 Mon Sep 17 00:00:00 2001 From: Aloento <11802769+Aloento@users.noreply.github.com> Date: Wed, 17 Jan 2024 11:50:13 +0100 Subject: [PATCH] The most significant changes involve the introduction of a new error class `EmptyResponseError` and the modification of error handling in `SignalR.ts` and `Get.tsx`. The `EmptyResponseError` class is used to handle cases where the server returns an empty response. In `SignalR.ts`, this new error class replaces the previously used `TypeError`. In `Get.tsx`, the error handling has been restructured with a `try-catch` block that handles different types of errors differently. The version number in the `UserGet` class in `Get.tsx` was also updated. Changes: 1. A new error class `EmptyResponseError` was added to `Exceptions.ts`. This class is used to throw an error when the server returns an empty response. 2. The `EmptyResponseError` class was imported into `SignalR.ts` and `Get.tsx` files. 3. In `SignalR.ts`, the `TypeError` that was previously thrown when the server returned an empty response was replaced with the new `EmptyResponseError`. 4. In `Get.tsx`, the `useBoolean` and `useMount` hooks were removed, and the `OnNewUserSubject` subscription was also removed. Instead, a `try-catch` block was added to handle errors when getting user information. 5. The version number in the `UserGet` class in `Get.tsx` was updated from `0.4.1` to `0.4.2`. --- src/Helpers/Exceptions.ts | 11 +++++++++++ src/ShopNet/SignalR.ts | 4 ++-- src/ShopNet/User/Get.tsx | 25 +++++++++++++------------ 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/Helpers/Exceptions.ts b/src/Helpers/Exceptions.ts index ff3631f0..145be60f 100644 --- a/src/Helpers/Exceptions.ts +++ b/src/Helpers/Exceptions.ts @@ -19,3 +19,14 @@ export class NotTrueError extends Error { super("Server Returned False"); } } + +/** + * @author Aloento + * @since 1.3.0 + * @version 0.1.0 + */ +export class EmptyResponseError extends Error { + public constructor() { + super("Server Returned Empty Response"); + } +} diff --git a/src/ShopNet/SignalR.ts b/src/ShopNet/SignalR.ts index aaac9114..7a87ed9f 100644 --- a/src/ShopNet/SignalR.ts +++ b/src/ShopNet/SignalR.ts @@ -1,7 +1,7 @@ import { HubConnectionState } from "@microsoft/signalr"; import dayjs, { Dayjs } from "dayjs"; import { Subject } from "rxjs"; -import { NotLoginError, NotTrueError } from "~/Helpers/Exceptions"; +import { EmptyResponseError, NotLoginError, NotTrueError } from "~/Helpers/Exceptions"; import type { Logger } from "~/Helpers/Logger"; import type { AdminNet } from "./Admin/AdminNet"; import { MSAL, Shared, type IConcurrency } from "./Database"; @@ -135,7 +135,7 @@ export abstract class SignalR { if (!res) { Shared.Sto.delete(index); - throw new TypeError("Empty Response"); + throw new EmptyResponseError(); } await Shared.Set(index, { diff --git a/src/ShopNet/User/Get.tsx b/src/ShopNet/User/Get.tsx index 5b7631c3..ae75cc25 100644 --- a/src/ShopNet/User/Get.tsx +++ b/src/ShopNet/User/Get.tsx @@ -1,9 +1,7 @@ import { useConst } from "@fluentui/react-hooks"; -import { useBoolean, useMount } from "ahooks"; import { useLiveQuery } from "dexie-react-hooks"; -import { OnNewUserSubject } from "~/Components/NewUser"; import { IPersona } from "~/Components/ShopCart/Persona"; -import { NotLoginError } from "~/Helpers/Exceptions"; +import { EmptyResponseError, NotLoginError } from "~/Helpers/Exceptions"; import type { Logger } from "~/Helpers/Logger"; import { useErrorToast } from "~/Helpers/useToast"; import { IConcurrency } from "../Database"; @@ -27,26 +25,29 @@ export abstract class UserGet extends ShopNet { /** * @author Aloento * @since 1.0.0 - * @version 0.4.1 + * @version 0.4.2 */ public static useMe(pLog: Logger): IUserGetMe | void { const log = useConst(() => pLog.With("|", "Hub", "User", "Get", "Me")); const { dispatch } = useErrorToast(log); - const [onNew, { set }] = useBoolean(); - useMount(() => OnNewUserSubject.subscribe(set)); - - const res = useLiveQuery(() => this.GetVersionCache(0, "UserGetMe") - .catch(e => { - if (onNew || e instanceof NotLoginError) + const res = useLiveQuery(async (): Promise => { + try { + this.EnsureLogin(); + return await this.GetVersionCache(0, "UserGetMe"); + } catch (e) { + if (e instanceof EmptyResponseError) + return; + else if (e instanceof NotLoginError) log.info(e); else dispatch({ Message: "Failed to Get Your Info", - Error: e, + Error: e as Error, Request: "" }); - })); + } + }); return res; }