Skip to content

Commit

Permalink
Add error handling for PrismaClientKnownRequestError in getMinecraftI…
Browse files Browse the repository at this point in the history
…nfo function (#66)

This pull request adds error handling for PrismaClientKnownRequestError in the getMinecraftInfo function to prevent any potential issues.
  • Loading branch information
DarthGigi authored Jan 27, 2024
2 parents d85a94e + 3cac549 commit e76d54a
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/routes/api/oauth/minecraft/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { auth } from "$lib/server/lucia";
import { OAuthRequestError, providerUserAuth, validateOAuth2AuthorizationCode } from "@lucia-auth/oauth";
import { error, redirect } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";

async function getMinecraftInfo(access_token: string): Promise<MCAuthProfile> {
const res = await fetch("https://mc-auth.com/api/v2/profile ", {
Expand Down Expand Up @@ -180,12 +181,18 @@ export const load = (async ({ cookies, url, locals }) => {

if (!user) throw new Error("Failed to get user");

const key = await auth.createKey({
providerId: "username",
providerUserId: user.username.toLocaleLowerCase(),
password: null,
userId: user.userId
});
try {
const key = await auth.createKey({
providerId: "username",
providerUserId: minecraftUser.name.toLocaleLowerCase(),
password: null,
userId: user.userId
});
} catch (e: any) {
if (!(e instanceof PrismaClientKnownRequestError && e.code === "P2002") && !(e instanceof TypeError)) {
console.error(e);
}
}

const session = await auth.createSession({
userId: user.userId,
Expand Down

0 comments on commit e76d54a

Please sign in to comment.