From 3faf706f827052f5189e7c585c220179bfafda48 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 16 Apr 2024 23:45:54 +0100 Subject: [PATCH] show the errors from the api and not a custom error --- package.json | 2 +- pnpm-lock.yaml | 8 ++-- src/app/(pages)/player/[id]/page.tsx | 41 +++++++++++----- .../server/[platform]/[hostname]/page.tsx | 47 ++++++++++++++----- 4 files changed, 68 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 50f0d69..1f3307d 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "class-variance-authority": "^0.7.0", "clsx": "^2.1.0", "lucide-react": "^0.368.0", - "mcutils-library": "^1.1.4", + "mcutils-library": "^1.1.5", "next": "14.2.1", "next-themes": "^0.3.0", "react": "^18", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 68ba2a2..21cda8a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -21,8 +21,8 @@ dependencies: specifier: ^0.368.0 version: 0.368.0(react@18.2.0) mcutils-library: - specifier: ^1.1.4 - version: 1.1.4(@babel/core@7.24.4)(@types/node@20.12.7) + specifier: ^1.1.5 + version: 1.1.5(@babel/core@7.24.4)(@types/node@20.12.7) next: specifier: 14.2.1 version: 14.2.1(@babel/core@7.24.4)(react-dom@18.2.0)(react@18.2.0) @@ -3516,8 +3516,8 @@ packages: tmpl: 1.0.5 dev: false - /mcutils-library@1.1.4(@babel/core@7.24.4)(@types/node@20.12.7): - resolution: {integrity: sha512-+L2aTlQMiXXz5SUoe23YHMSjOq2EesS6c012mlhwdovIrICcTZ3fx2cCx4wpa8tXyW5boYHYWtIgJgQ720ebbw==} + /mcutils-library@1.1.5(@babel/core@7.24.4)(@types/node@20.12.7): + resolution: {integrity: sha512-aQCn4YLvmkoqc+yClLiiiNkPJ+02iSOxzZ5X+Tr0SPPNhNzHn6IAP71ZS3SGQlLeRkfIqXynjbhrEplmW+CBzw==} dependencies: axios: 1.6.8 jest: 29.7.0(@types/node@20.12.7)(ts-node@10.9.2) diff --git a/src/app/(pages)/player/[id]/page.tsx b/src/app/(pages)/player/[id]/page.tsx index 227823b..1d5074c 100644 --- a/src/app/(pages)/player/[id]/page.tsx +++ b/src/app/(pages)/player/[id]/page.tsx @@ -4,6 +4,8 @@ import { NotFound } from "@/app/components/not-found"; import { LookupPlayer } from "@/app/components/player/lookup-player"; import { generateEmbed } from "@/common/embed"; import { getPlayer } from "mcutils-library"; +import { CachedPlayer } from "mcutils-library/dist/types/cache/cachedPlayer"; +import { McUtilsAPIError } from "mcutils-library/dist/types/error"; import { Player, SkinPart } from "mcutils-library/dist/types/player/player"; import { Metadata } from "next"; import Image from "next/image"; @@ -16,12 +18,19 @@ type Params = { }; export async function generateMetadata({ params: { id } }: Params): Promise { - const player = await getData(id); - if (player == null) { + let error: string | undefined = undefined; + let player: Player | undefined = undefined; + + try { + player = (await getData(id))?.player; + } catch (err) { + error = (err as McUtilsAPIError).message; + } + if (error && player == undefined) { return generateEmbed({ title: "Unknown Player", description: "Invalid UUID / Username" }); } - const { username, uniqueId, skin } = player; + const { username, uniqueId, skin } = player as Player; const headPartUrl = skin.parts.head; const description = ` @@ -35,17 +44,25 @@ export async function generateMetadata({ params: { id } }: Params): Promise { - try { - const cachedPlayer = await getPlayer(id); - return cachedPlayer.player; - } catch (error) { - return null; // Player not found - } +/** + * Gets the player's data from the uuid or username + * + * @param id the player's uuid or username + * @returns the player's data or an error message + */ +async function getData(id: string): Promise { + return await getPlayer(id); } export default async function Page({ params }: Params): Promise { - const player = await getData(params.id); + let error: string | undefined = undefined; + let player: Player | undefined = undefined; + + try { + player = (await getData(params.id))?.player; + } catch (err) { + error = (err as McUtilsAPIError).message; + } return (
@@ -57,7 +74,7 @@ export default async function Page({ params }: Params): Promise {
- {player == null && } + {error && } {player != null && (
diff --git a/src/app/(pages)/server/[platform]/[hostname]/page.tsx b/src/app/(pages)/server/[platform]/[hostname]/page.tsx index d4e6320..56671f9 100644 --- a/src/app/(pages)/server/[platform]/[hostname]/page.tsx +++ b/src/app/(pages)/server/[platform]/[hostname]/page.tsx @@ -5,9 +5,10 @@ import { generateEmbed } from "@/common/embed"; import { formatNumber } from "@/common/number-utils"; import { capitalizeFirstLetter } from "@/common/string-utils"; import { getServer } from "mcutils-library"; +import { McUtilsAPIError } from "mcutils-library/dist/types/error"; +import BedrockMinecraftServer from "mcutils-library/dist/types/server/bedrockServer"; import JavaMinecraftServer from "mcutils-library/dist/types/server/javaServer"; import { ServerPlatform } from "mcutils-library/dist/types/server/platform"; -import { MinecraftServer } from "mcutils-library/dist/types/server/server"; import { Metadata } from "next"; import Image from "next/image"; @@ -19,12 +20,20 @@ type Params = { }; export async function generateMetadata({ params: { platform, hostname } }: Params): Promise { - const server = await getData(platform, hostname); - if (server == null) { + let error: string | undefined = undefined; + let server: JavaMinecraftServer | BedrockMinecraftServer | undefined = undefined; + + try { + server = (await getServer(platform, hostname))?.server; + } catch (err) { + error = (err as McUtilsAPIError).message; + } + + if (error && server == undefined) { return generateEmbed({ title: "Unknown Server", description: "Server not responding" }); } - const { hostname: serverHostname, players } = server; + const { hostname: serverHostname, players } = server as JavaMinecraftServer | BedrockMinecraftServer; let favicon = null; // Server favicon @@ -46,17 +55,29 @@ export async function generateMetadata({ params: { platform, hostname } }: Param }); } -async function getData(platform: ServerPlatform, id: string): Promise { - try { - const cachedServer = await getServer(platform, id); - return cachedServer.server; - } catch (error) { - return null; // Server not found - } +/** + * Gets the server's data from the hostname + * + * @param platform the server's platform + * @param hostnamt the server's hostname + * @returns the server's data or an error message + */ +async function getData( + platform: ServerPlatform, + hostname: string +): Promise { + return (await getServer(platform, hostname)).server; } export default async function Page({ params: { platform, hostname } }: Params): Promise { - const server = await getData(platform, hostname); + let error: string | undefined = undefined; + let server: JavaMinecraftServer | BedrockMinecraftServer | undefined = undefined; + + try { + server = (await getServer(platform, hostname))?.server; + } catch (err) { + error = (err as McUtilsAPIError).message; + } let favicon = null; // Server favicon @@ -76,7 +97,7 @@ export default async function Page({ params: { platform, hostname } }: Params):
- {server == null && } + {error && } {server != null && (