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

View File

@ -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",

8
pnpm-lock.yaml generated
View File

@ -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)

View File

@ -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<Metadata> {
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<Meta
});
}
async function getData(id: string): Promise<Player | null> {
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<CachedPlayer | null> {
return await getPlayer(id);
}
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 (
<div className="h-full flex flex-col items-center">
@ -57,7 +74,7 @@ export default async function Page({ params }: Params): Promise<JSX.Element> {
</div>
<Card className="w-max xs:w-fit">
{player == null && <NotFound message="Invalid UUID / Username" />}
{error && <NotFound message={error} />}
{player != null && (
<div className="flex gap-4 flex-col xs:flex-row">
<div className="flex justify-center xs:justify-start">

View File

@ -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<Metadata> {
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<MinecraftServer | null> {
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<JavaMinecraftServer | BedrockMinecraftServer | null> {
return (await getServer(platform, hostname)).server;
}
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
@ -76,7 +97,7 @@ export default async function Page({ params: { platform, hostname } }: Params):
</div>
<Card className="w-max xs:w-fit">
{server == null && <NotFound message="Server not responding" />}
{error && <NotFound message={error} />}
{server != null && (
<div className="flex gap-4 flex-col">
<div className="flex gap-4 flex-col xs:flex-row">