Add error handling

This commit is contained in:
delivan 2022-12-15 00:39:51 +09:00
parent 10e129baad
commit d0ba550eaf
4 changed files with 107 additions and 48 deletions

View File

@ -0,0 +1,40 @@
import Link from "next/link";
import React, { Component, ErrorInfo, ReactNode } from "react";
interface Props {
children?: ReactNode;
}
interface State {
hasError: boolean;
}
class ErrorBoundary extends Component<Props, State> {
public state: State = {
hasError: false,
};
public static getDerivedStateFromError(): State {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error("Uncaught error:", error, errorInfo);
}
public render() {
if (this.state.hasError) {
return (
<h1>
Oops.. there is something wrong.
<br /> Please try again. <Link href="/">Go to home</Link>
</h1>
);
}
return this.props.children;
}
}
export default ErrorBoundary;

View File

@ -1,5 +1,6 @@
import type { AppProps } from "next/app"; import type { AppProps } from "next/app";
import React from "react"; import React from "react";
import ErrorBoundary from "../components/error-boundary";
import { GlobalStyle } from "../styles/global"; import { GlobalStyle } from "../styles/global";
@ -7,7 +8,9 @@ export default function App({ Component, pageProps }: AppProps) {
return ( return (
<React.Fragment> <React.Fragment>
<GlobalStyle /> <GlobalStyle />
<Component {...pageProps} /> <ErrorBoundary>
<Component {...pageProps} />
</ErrorBoundary>
</React.Fragment> </React.Fragment>
); );
} }

View File

@ -24,6 +24,7 @@ import { useRouter } from "next/router";
import { MainChainId } from "../../constants/wallet"; import { MainChainId } from "../../constants/wallet";
import { getKeplrFromWindow, KeplrWallet } from "../../wallets"; import { getKeplrFromWindow, KeplrWallet } from "../../wallets";
import { ChainIdHelper } from "@keplr-wallet/cosmos"; import { ChainIdHelper } from "@keplr-wallet/cosmos";
import ErrorBoundary from "../../components/error-boundary";
export default function VerificationPage() { export default function VerificationPage() {
const router = useRouter(); const router = useRouter();
@ -107,26 +108,30 @@ export default function VerificationPage() {
}; };
const verifyTwitterAccount = async () => { const verifyTwitterAccount = async () => {
const keplr = await getKeplrFromWindow(); try {
const keplr = await getKeplrFromWindow();
if (twitterAuthInfo && keplr) { if (twitterAuthInfo && keplr) {
const wallet = new KeplrWallet(keplr); const wallet = new KeplrWallet(keplr);
const key = await wallet.getKey(MainChainId); const key = await wallet.getKey(MainChainId);
const icnsVerificationList = ( const icnsVerificationList = (
await request<IcnsVerificationResponse>("/api/icns-verification", { await request<IcnsVerificationResponse>("/api/icns-verification", {
method: "post", method: "post",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
claimer: key.bech32Address, claimer: key.bech32Address,
authToken: twitterAuthInfo.accessToken, authToken: twitterAuthInfo.accessToken,
}), }),
}) })
).verificationList; ).verificationList;
console.log(icnsVerificationList); console.log(icnsVerificationList);
}
} catch (error) {
console.log(error);
} }
}; };
@ -135,39 +140,41 @@ export default function VerificationPage() {
}; };
return ( return (
<Container> <ErrorBoundary>
<Logo /> <Container>
<Logo />
<MainContainer> <MainContainer>
{isLoading ? ( {isLoading ? (
<SkeletonChainList /> <SkeletonChainList />
) : ( ) : (
<ContentContainer> <ContentContainer>
<TwitterProfile twitterProfileInformation={twitterAuthInfo} /> <TwitterProfile twitterProfileInformation={twitterAuthInfo} />
<ChainListTitleContainer> <ChainListTitleContainer>
<ChainListTitle>Chain List</ChainListTitle> <ChainListTitle>Chain List</ChainListTitle>
<SearchContainer>Search</SearchContainer> <SearchContainer>Search</SearchContainer>
</ChainListTitleContainer> </ChainListTitleContainer>
<ChainList <ChainList
chainList={chainList} chainList={chainList}
checkedItems={checkedItems} checkedItems={checkedItems}
setCheckedItems={setCheckedItems} setCheckedItems={setCheckedItems}
/> />
<ButtonContainer> <ButtonContainer>
<PrimaryButton <PrimaryButton
disabled={checkedItems.size < 1} disabled={checkedItems.size < 1}
onClick={onClickRegistration} onClick={onClickRegistration}
> >
Register Register
</PrimaryButton> </PrimaryButton>
</ButtonContainer> </ButtonContainer>
</ContentContainer> </ContentContainer>
)} )}
</MainContainer> </MainContainer>
</Container> </Container>
</ErrorBoundary>
); );
} }

View File

@ -3,7 +3,16 @@ export function request<TResponse>(
config: RequestInit = {}, config: RequestInit = {},
): Promise<TResponse> { ): Promise<TResponse> {
return fetch(url, config) return fetch(url, config)
.then((response) => response.json()) .then((response) => {
if (!response.ok) {
console.error(
new Error(
`This is an HTTP error: The status is ${response.status} ${response.statusText}`,
),
);
}
return response.json();
})
.then((data) => data as TResponse); .then((data) => data as TResponse);
} }