do not throw 500 on mojang server error

This commit is contained in:
2023-11-30 00:40:14 +01:00
parent b75620c892
commit 1f150bae06
2 changed files with 21 additions and 11 deletions

View File

@@ -1,17 +1,22 @@
import { createHash } from 'node:crypto';
export class UserNotFoundError extends Error {
readonly username: string;
constructor(username: string) {
super(`Ein Spieler mit dem Namen '${username}' konnte nicht gefunden werden`);
super(`couldn't find a player with the username '${username}'`);
this.username = username;
}
}
export class MojangError extends Error {}
export async function getJavaUuid(username: string): Promise<string> {
const response = await fetch(`https://api.mojang.com/users/profiles/minecraft/${username}`);
if (!response.ok) {
throw response.status < 500
? new UserNotFoundError(username)
: new Error(`mojang server error (${response.status}): ${await response.text()}`);
: new MojangError(`mojang server error (${response.status}): ${await response.text()}`);
}
const json = await response.json();
const id: string = json['id'];