Skip to content

Commit

Permalink
Improve error handling for GraphQL errors in GraphQLRequest (#68)
Browse files Browse the repository at this point in the history
* feat: write custom paginator

also updated packages

* feat: show gql error in yuukoerror
  • Loading branch information
crackheadakira authored Feb 15, 2025
1 parent 57a674b commit 34e96a5
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 7 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/api/controllers/public.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export const publicController = new Elysia({
const decryptedToken = await rsaEncryption(encryptedToken, false);
const { Viewer: data } = (await graphQLRequest("Viewer", {}, decryptedToken)).data;
if (!data) return { message: "Invalid token" };
console.log(data);
const existingUser = (await db.select().from(anilistUser).where(eq(anilistUser.discordId, discordId)).limit(1))[0];
if (existingUser) return { message: "User already registered" };
await db.insert(anilistUser).values({ discordId, anilistToken: encryptedToken, anilistId: data.id });

console.log(`Registered ${discordId} as ${data.name}`);
set.status = 201;
return { message: `Registered as ${data.name}!` };
},
Expand Down
2 changes: 1 addition & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { api } from '#api/controllers/*'
import { api } from '#api/controllers/global.controller'
import { cors } from '@elysiajs/cors'
import { Elysia } from 'elysia'

Expand Down
3 changes: 0 additions & 3 deletions src/database/sqlite/README.md

This file was deleted.

8 changes: 6 additions & 2 deletions src/utils/graphQLRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,13 @@ export async function graphQLRequest<QueryKey extends Query>(queryKey: QueryKey,

try {
const res = await fetch(url, reqOptions)
if (!res.ok) throw new YuukoError(res.statusText, vars);
const resJson = await res.json() as GraphQLResponse;
if (!res.ok) {
const errorMessage = resJson.errors[0].message;
throw new YuukoError(`${res.status} ${errorMessage.length > 0 ? errorMessage : ""} ${res.statusText}`, vars);
}

const data = await res.json() as GraphQLResponse<QueryVariables[QueryKey][0]>
const data = resJson as GraphQLResponse<QueryVariables[QueryKey][0]>
return data;
} catch (e: any) {
console.error(e)
Expand Down
1 change: 1 addition & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface Headers {

export interface GraphQLResponse<TData = any> {
data: TData
errors: any[]
headers: any
}

Expand Down

0 comments on commit 34e96a5

Please sign in to comment.