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

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