Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(staking): add error logging to useData #2022

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions apps/staking/src/hooks/use-data.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { useCallback } from "react";
import useSWR, { type KeyedMutator } from "swr";

import { useLogger } from "./use-logger";

export const useData = <T>(...args: Parameters<typeof useSWR<T>>) => {
const { data, isLoading, mutate, ...rest } = useSWR(...args);

const error = rest.error as unknown;
const logger = useLogger();

const reset = useCallback(() => {
mutate(undefined).catch(() => {
Expand All @@ -13,7 +16,8 @@ export const useData = <T>(...args: Parameters<typeof useSWR<T>>) => {
}, [mutate]);

if (error) {
return State.ErrorState(new LoadDashboardDataError(error), reset);
logger.error(error);
return State.ErrorState(new UseDataError(error), reset);
} else if (isLoading) {
return State.Loading();
} else if (data) {
Expand All @@ -38,17 +42,17 @@ const State = {
mutate,
data,
}),
ErrorState: (error: LoadDashboardDataError, reset: () => void) => ({
ErrorState: (error: UseDataError, reset: () => void) => ({
type: StateType.Error as const,
error,
reset,
}),
};

class LoadDashboardDataError extends Error {
class UseDataError extends Error {
constructor(cause: unknown) {
super(cause instanceof Error ? cause.message : "");
this.name = "LoadDashboardDataError";
this.name = "UseDataError";
this.cause = cause;
}
}
Loading