Handle published contract page server side errors (#5098)

## Problem solved

Short description of the bug fixed or feature added

<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on improving error handling and null checks in the `published-contract` related components of the dashboard application. It ensures that appropriate actions are taken when data is missing, enhancing the application's robustness.

### Detailed summary
- Added null checks for `publishedContracts` in multiple files.
- Implemented `notFound()` calls when `publishedContractVersions` or `publishedContracts` are missing.
- Enhanced error handling in `getPublishedContractsWithPublisherMapping.ts` with try-catch.
- Updated `publisherAddress` resolution logic for better clarity.

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`

<!-- end pr-codex -->
This commit is contained in:
MananTank
2024-10-21 20:29:44 +00:00
parent bc5b543269
commit 002c545d0e
9 changed files with 56 additions and 18 deletions
+2
View File
@@ -29,3 +29,5 @@ packages/*/typedoc/*
*storybook.log
storybook-static
.aider*
tsconfig.tsbuildinfo
@@ -3,7 +3,7 @@ import { getActiveAccountCookie, getJWTCookie } from "@/constants/cookie";
import { getThirdwebClient } from "@/constants/thirdweb.server";
import { ContractPublishForm } from "components/contract-components/contract-publish-form";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { notFound, redirect } from "next/navigation";
import { fetchDeployMetadata } from "thirdweb/contract";
import { getPublishedContractsWithPublisherMapping } from "../../../published-contract/[publisher]/[contract_id]/utils/getPublishedContractsWithPublisherMapping";
@@ -47,6 +47,10 @@ export default async function PublishContractPage(
contract_id: publishMetadataFromUri.name,
});
if (!publishedContractVersions) {
notFound();
}
const publishedContract = publishedContractVersions[0];
if (publishedContract) {
@@ -28,6 +28,10 @@ export default async function Image(props: {
fetchPublisherProfile(publisher),
]);
if (!publishedContracts) {
return null;
}
const publishedContract =
publishedContracts.find((p) => p.version === props.params.version) ||
publishedContracts[0];
@@ -29,12 +29,24 @@ export default async function PublishedContractPage(
props: PublishedContractDeployPageProps,
) {
// resolve ENS if required
const publisherAddress = isAddress(props.params.publisher)
? props.params.publisher
: await resolveAddress({
let publisherAddress: string | undefined = undefined;
if (isAddress(props.params.publisher)) {
publisherAddress = props.params.publisher;
} else {
try {
publisherAddress = await resolveAddress({
client: getThirdwebClient(),
name: mapThirdwebPublisher(props.params.publisher),
});
} catch {
// ignore
}
}
if (!publisherAddress) {
notFound();
}
// get all the published versions of the contract
const publishedContractVersions = await fetchPublishedContractVersions(
@@ -1,3 +1,4 @@
import { notFound } from "next/navigation";
import { PublishedContractBreadcrumbs } from "./components/breadcrumbs.client";
import { getPublishedContractsWithPublisherMapping } from "./utils/getPublishedContractsWithPublisherMapping";
@@ -23,6 +24,10 @@ export async function generateMetadata({ params }: { params: Params }) {
contract_id: contract_id,
});
if (!publishedContracts) {
notFound();
}
const publishedContract = publishedContracts[0];
if (!publishedContract) {
@@ -27,6 +27,10 @@ export default async function Image(props: {
fetchPublisherProfile(publisher),
]);
if (!publishedContracts) {
return null;
}
const publishedContract = publishedContracts[0];
if (!publishedContract) {
@@ -22,6 +22,10 @@ export default async function PublishedContractPage(
contract_id: props.params.contract_id,
});
if (!publishedContractVersions) {
notFound();
}
const publishedContract = publishedContractVersions[0];
if (!publishedContract) {
@@ -16,19 +16,23 @@ export async function getPublishedContractsWithPublisherMapping(options: {
}) {
const { publisher, contract_id } = options;
// resolve ENS
const publisherAddress = isAddress(publisher)
? publisher
: await resolveAddress({
client: getThirdwebClient(),
name: mapThirdwebPublisher(publisher),
});
try {
// resolve ENS
const publisherAddress = isAddress(publisher)
? publisher
: await resolveAddress({
client: getThirdwebClient(),
name: mapThirdwebPublisher(publisher),
});
// get all the published versions of the contract
const publishedContractVersions = await fetchPublishedContractVersions(
publisherAddress,
contract_id,
);
// get all the published versions of the contract
const publishedContractVersions = await fetchPublishedContractVersions(
publisherAddress,
contract_id,
);
return publishedContractVersions;
return publishedContractVersions;
} catch {
return undefined;
}
}
File diff suppressed because one or more lines are too long