Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
The most significant changes involve the introduction of a new error …
Browse files Browse the repository at this point in the history
…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`.
  • Loading branch information
Aloento committed Jan 17, 2024
1 parent 029f5ee commit b5fb5bb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/Helpers/Exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
4 changes: 2 additions & 2 deletions src/ShopNet/SignalR.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<T & { QueryExp: number }>(index, {
Expand Down
25 changes: 13 additions & 12 deletions src/ShopNet/User/Get.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<IUserGetMe>(0, "UserGetMe")
.catch(e => {
if (onNew || e instanceof NotLoginError)
const res = useLiveQuery(async (): Promise<IUserGetMe | void> => {
try {
this.EnsureLogin();
return await this.GetVersionCache<IUserGetMe>(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;
}
Expand Down

0 comments on commit b5fb5bb

Please sign in to comment.