Skip to content

Commit

Permalink
refactor errors & redesign search results header
Browse files Browse the repository at this point in the history
  • Loading branch information
uiii committed Oct 31, 2023
1 parent f0c7e05 commit a44ea0c
Show file tree
Hide file tree
Showing 18 changed files with 284 additions and 102 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"deepmerge": "^4.3.1",
"dexie": "^3.2.4",
"echarts": "^5.4.2",
"http-status-codes": "^2.3.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.17.0",
Expand Down
33 changes: 28 additions & 5 deletions src/components/ErrorMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/** @jsxImportSource @emotion/react */
import { ReactNode } from "react";
import { ReactNode, useEffect, useMemo } from "react";
import { Alert, AlertProps, AlertTitle } from "@mui/material";
import { css } from "@emotion/react";
import { useRollbar } from "@rollbar/react";

const alertStyle = css`
padding: 16px 22px;
Expand All @@ -25,12 +26,34 @@ const detailsStyle = css`

export type ErrorMessageProps = AlertProps & {
message: ReactNode;
details?: ReactNode;
details?: unknown|unknown[];
report?: boolean;
showReported?: boolean;
}

export const ErrorMessage = (props: ErrorMessageProps) => {
const {message, details, showReported, ...alertProps} = props;
const {message, details, report, showReported = report, ...alertProps} = props;

const rollbar = useRollbar();

const errors = useMemo<unknown[]>(
() => Array.isArray(details) ? details : details ? [details] : [],
[details]
);

const detailsContent = useMemo(() => {
return errors
.map(it => it instanceof Error ? it.message : "Unknown error")
.join("\n\n");
}, [errors]);

console.log("details", detailsContent);

useEffect(() => {
if (report) {
errors.forEach(it => rollbar.error(it as any));
}
}, [errors, report, rollbar]);

return (
<Alert severity="error" css={alertStyle} data-test="error" {...alertProps}>
Expand All @@ -40,10 +63,10 @@ export const ErrorMessage = (props: ErrorMessageProps) => {
This error is logged. No need to report it.
</div>
}
{details &&
{detailsContent &&
<details css={detailsStyle}>
<summary>Details</summary>
<pre>{details}</pre>
<pre>{detailsContent}</pre>
</details>
}
</Alert>
Expand Down
6 changes: 3 additions & 3 deletions src/components/InfoTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export type InfoTableProps<T extends object, A extends any[] = []> = TableContai
additionalData?: A;
loading?: boolean;
notFound?: boolean;
notFoundMessage?: string;
notFoundMessage?: ReactNode;
error?: any;
errorMessage?: string;
children: ReactElement<InfoTableAttributeProps<T, A>>|(ReactElement<InfoTableAttributeProps<T, A>>|false|undefined|null)[];
Expand Down Expand Up @@ -167,8 +167,8 @@ export const InfoTable = <T extends object, A extends any[] = []>(props: InfoTab
return (
<ErrorMessage
message={errorMessage}
details={error.message}
showReported
details={error}
report
/>
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/ItemsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ export const ItemsTable = <T extends ItemsTableItem, S = any, A extends any[] =
return (
<ErrorMessage
message={errorMessage}
details={error.message}
showReported
details={error}
report
/>
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/components/account/AccountBalancesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,10 @@ export const AccountBalancesTable = (props: AccountBalancesTableProps) => {
}
{balance.error &&
<ErrorMessage
message="Unexpected error occured while fetching data"
details={balance.error.message}
data-test={`${balance.network.name}-balance-error`}
message="Unexpected error occured while fetching data"
details={balance.error}
report
/>
}
</>
Expand Down
4 changes: 2 additions & 2 deletions src/components/account/AccountPortfolio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ export const AccountPortfolio = (props: AccountPortfolioProps) => {
return (
<ErrorMessage
message="Unexpected error occured while fetching data"
details={balances.error.message}
showReported
details={balances.error}
report
/>
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/network/NetworkStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export const NetworkStats = (props: NetworkInfoTableProps) => {
return (
<ErrorMessage
message="Unexpected error occured while fetching data"
details={stats.error.message}
showReported
details={stats.error}
report
/>
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/network/NetworkTokenDistribution.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export const NetworkTokenDistribution = (props: NetworkTokenDistributionProps) =
return (
<ErrorMessage
message="Unexpected error occured while fetching data"
details={stats.error.message}
showReported
details={stats.error}
report
/>
);
}
Expand Down
13 changes: 2 additions & 11 deletions src/hooks/useResource.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useEffect, useMemo } from "react";
import useSwr, { SWRConfiguration } from "swr";
import { useRollbar } from "@rollbar/react";

import { Resource } from "../model/resource";
import { DataError } from "../utils/error";
import { NonFatalError } from "../utils/error";

export interface UseResourceOptions extends SWRConfiguration {
/**
Expand All @@ -25,8 +24,6 @@ export function useResource<T = any, F extends any[] = any[]>(
args: F,
options: UseResourceOptions = {}
) {
const rollbar = useRollbar();

const {skip, refresh, refreshInterval = 3000, ...swrOptions} = options;

const swrKey = !skip
Expand All @@ -39,13 +36,7 @@ export function useResource<T = any, F extends any[] = any[]>(
});

useEffect(() => {
if (!error) {
return;
}

if (error && error instanceof DataError) {
rollbar.error(error);
} else {
if (error && !(error instanceof NonFatalError)) {
throw error;
}
}, [error]);
Expand Down
3 changes: 3 additions & 0 deletions src/model/searchResult.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NetworkError } from "../utils/error";

import { ItemsResponse } from "./itemsResponse";
import { SearchResultItem } from "./searchResultItem";

Expand All @@ -11,5 +13,6 @@ export type SearchResult = {
blocks: ItemsResponse<SearchResultItem<Block>, true>
extrinsics: ItemsResponse<SearchResultItem<Extrinsic>, true>
events: ItemsResponse<SearchResultItem<Event>, true>
errors?: NetworkError[];
totalCount: number;
}
24 changes: 23 additions & 1 deletion src/rollbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,26 @@ export const rollbarConfig: Configuration = {
enabled: config.rollbar.enabled
};

export const rollbar = new Rollbar(rollbarConfig);
const rollbar = new Rollbar(rollbarConfig);

if (!config.rollbar.enabled && window.location.hostname === "localhost") {
// if the rollbar is disabled on localhost, print
// the message about reporting to the console
// so it is obvious when it would happen

const notifier = (rollbar as any).client.notifier;
const log = notifier.log;

notifier.log = function (item: any, callback: any) {
console.warn(`The following ${item.level} item would be reported to Rollbar`);

if (!item._isUncaught) {
const consoleAction = ["error", "critical"].includes(item.level) ? "error" : "warn";
console[consoleAction](...item._originalArgs);
}

log.apply(this, [item, callback]);
};
}

export { rollbar };
1 change: 1 addition & 0 deletions src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const routes: RouteObject[] = [
{
path: "search/:tab?",
element: <SearchPage />,
errorElement: <ErrorPage />,
},
{
id: "network",
Expand Down
3 changes: 2 additions & 1 deletion src/screens/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const ErrorPage = () => {
<CardHeader>Error</CardHeader>
<ErrorMessage
message="Unexpected error occured while loading page"
details={("message" in (error as any)) ? (error as any).message : undefined}
details={error}
showReported
/>
</Card>
</>
Expand Down
Loading

0 comments on commit a44ea0c

Please sign in to comment.