show the errors from the api and not a custom error
All checks were successful
Deploy App / docker (ubuntu-latest) (push) Successful in 1m30s

This commit is contained in:
Lee 2024-04-16 23:45:54 +01:00
parent 43e1042c8a
commit 3faf706f82
4 changed files with 68 additions and 30 deletions

@ -14,7 +14,7 @@
"class-variance-authority": "^0.7.0", "class-variance-authority": "^0.7.0",
"clsx": "^2.1.0", "clsx": "^2.1.0",
"lucide-react": "^0.368.0", "lucide-react": "^0.368.0",
"mcutils-library": "^1.1.4", "mcutils-library": "^1.1.5",
"next": "14.2.1", "next": "14.2.1",
"next-themes": "^0.3.0", "next-themes": "^0.3.0",
"react": "^18", "react": "^18",

8
pnpm-lock.yaml generated

@ -21,8 +21,8 @@ dependencies:
specifier: ^0.368.0 specifier: ^0.368.0
version: 0.368.0(react@18.2.0) version: 0.368.0(react@18.2.0)
mcutils-library: mcutils-library:
specifier: ^1.1.4 specifier: ^1.1.5
version: 1.1.4(@babel/core@7.24.4)(@types/node@20.12.7) version: 1.1.5(@babel/core@7.24.4)(@types/node@20.12.7)
next: next:
specifier: 14.2.1 specifier: 14.2.1
version: 14.2.1(@babel/core@7.24.4)(react-dom@18.2.0)(react@18.2.0) 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 tmpl: 1.0.5
dev: false dev: false
/mcutils-library@1.1.4(@babel/core@7.24.4)(@types/node@20.12.7): /mcutils-library@1.1.5(@babel/core@7.24.4)(@types/node@20.12.7):
resolution: {integrity: sha512-+L2aTlQMiXXz5SUoe23YHMSjOq2EesS6c012mlhwdovIrICcTZ3fx2cCx4wpa8tXyW5boYHYWtIgJgQ720ebbw==} resolution: {integrity: sha512-aQCn4YLvmkoqc+yClLiiiNkPJ+02iSOxzZ5X+Tr0SPPNhNzHn6IAP71ZS3SGQlLeRkfIqXynjbhrEplmW+CBzw==}
dependencies: dependencies:
axios: 1.6.8 axios: 1.6.8
jest: 29.7.0(@types/node@20.12.7)(ts-node@10.9.2) jest: 29.7.0(@types/node@20.12.7)(ts-node@10.9.2)

@ -4,6 +4,8 @@ import { NotFound } from "@/app/components/not-found";
import { LookupPlayer } from "@/app/components/player/lookup-player"; import { LookupPlayer } from "@/app/components/player/lookup-player";
import { generateEmbed } from "@/common/embed"; import { generateEmbed } from "@/common/embed";
import { getPlayer } from "mcutils-library"; 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 { Player, SkinPart } from "mcutils-library/dist/types/player/player";
import { Metadata } from "next"; import { Metadata } from "next";
import Image from "next/image"; import Image from "next/image";
@ -16,12 +18,19 @@ type Params = {
}; };
export async function generateMetadata({ params: { id } }: Params): Promise<Metadata> { export async function generateMetadata({ params: { id } }: Params): Promise<Metadata> {
const player = await getData(id); let error: string | undefined = undefined;
if (player == null) { 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" }); 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 headPartUrl = skin.parts.head;
const description = ` const description = `
@ -35,17 +44,25 @@ export async function generateMetadata({ params: { id } }: Params): Promise<Meta
}); });
} }
async function getData(id: string): Promise<Player | null> { /**
try { * Gets the player's data from the uuid or username
const cachedPlayer = await getPlayer(id); *
return cachedPlayer.player; * @param id the player's uuid or username
} catch (error) { * @returns the player's data or an error message
return null; // Player not found */
} async function getData(id: string): Promise<CachedPlayer | null> {
return await getPlayer(id);
} }
export default async function Page({ params }: Params): Promise<JSX.Element> { export default async function Page({ params }: Params): Promise<JSX.Element> {
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 ( return (
<div className="h-full flex flex-col items-center"> <div className="h-full flex flex-col items-center">
@ -57,7 +74,7 @@ export default async function Page({ params }: Params): Promise<JSX.Element> {
</div> </div>
<Card className="w-max xs:w-fit"> <Card className="w-max xs:w-fit">
{player == null && <NotFound message="Invalid UUID / Username" />} {error && <NotFound message={error} />}
{player != null && ( {player != null && (
<div className="flex gap-4 flex-col xs:flex-row"> <div className="flex gap-4 flex-col xs:flex-row">
<div className="flex justify-center xs:justify-start"> <div className="flex justify-center xs:justify-start">

@ -5,9 +5,10 @@ import { generateEmbed } from "@/common/embed";
import { formatNumber } from "@/common/number-utils"; import { formatNumber } from "@/common/number-utils";
import { capitalizeFirstLetter } from "@/common/string-utils"; import { capitalizeFirstLetter } from "@/common/string-utils";
import { getServer } from "mcutils-library"; 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 JavaMinecraftServer from "mcutils-library/dist/types/server/javaServer";
import { ServerPlatform } from "mcutils-library/dist/types/server/platform"; import { ServerPlatform } from "mcutils-library/dist/types/server/platform";
import { MinecraftServer } from "mcutils-library/dist/types/server/server";
import { Metadata } from "next"; import { Metadata } from "next";
import Image from "next/image"; import Image from "next/image";
@ -19,12 +20,20 @@ type Params = {
}; };
export async function generateMetadata({ params: { platform, hostname } }: Params): Promise<Metadata> { export async function generateMetadata({ params: { platform, hostname } }: Params): Promise<Metadata> {
const server = await getData(platform, hostname); let error: string | undefined = undefined;
if (server == null) { 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" }); 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 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<MinecraftServer | null> { /**
try { * Gets the server's data from the hostname
const cachedServer = await getServer(platform, id); *
return cachedServer.server; * @param platform the server's platform
} catch (error) { * @param hostnamt the server's hostname
return null; // Server not found * @returns the server's data or an error message
} */
async function getData(
platform: ServerPlatform,
hostname: string
): Promise<JavaMinecraftServer | BedrockMinecraftServer | null> {
return (await getServer(platform, hostname)).server;
} }
export default async function Page({ params: { platform, hostname } }: Params): Promise<JSX.Element> { export default async function Page({ params: { platform, hostname } }: Params): Promise<JSX.Element> {
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 let favicon = null; // Server favicon
@ -76,7 +97,7 @@ export default async function Page({ params: { platform, hostname } }: Params):
</div> </div>
<Card className="w-max xs:w-fit"> <Card className="w-max xs:w-fit">
{server == null && <NotFound message="Server not responding" />} {error && <NotFound message={error} />}
{server != null && ( {server != null && (
<div className="flex gap-4 flex-col"> <div className="flex gap-4 flex-col">
<div className="flex gap-4 flex-col xs:flex-row"> <div className="flex gap-4 flex-col xs:flex-row">