Skip to content

Commit bc8ded8

Browse files
committed
feat: enhance GitHub client token handling and error logging
1 parent 26a3f18 commit bc8ded8

2 files changed

Lines changed: 52 additions & 16 deletions

File tree

src/lib/remote/data.remote.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ const CONTRIBUTION_QUERY = gql`
130130
* Creates a GraphQL client with GitHub token
131131
*/
132132
function createGitHubClient(): GraphQLClient {
133-
const token = process.env.VITE_GITHUB_TOKEN;
133+
let token = process.env.VITE_GITHUB_TOKEN || '';
134+
token = token.trim();
134135

135136
console.log(`[GitHub API] Checking for GitHub token...`);
136137
if (!token) {
@@ -141,7 +142,9 @@ function createGitHubClient(): GraphQLClient {
141142

142143
return new GraphQLClient('https://api.github.com/graphql', {
143144
headers: {
144-
Authorization: `bearer ${token}`
145+
Authorization: `Bearer ${token}`,
146+
'User-Agent': 'Svelte-MiniApps-GitHub-Tracker',
147+
'X-GitHub-Api-Version': '2022-11-28'
145148
}
146149
});
147150
}
@@ -313,16 +316,20 @@ export const getContributionData = query(contributionQuerySchema, async (input)
313316
});
314317

315318
// Handle specific error types
316-
if (err && typeof err === 'object' && 'status' in err) {
317-
const statusCode = (err as any).status;
318-
console.error(`[GitHub API] HTTP status code: ${statusCode}`);
319+
if (err && typeof err === 'object') {
320+
// graphql-request ClientError structure
321+
const response = (err as any).response;
322+
const statusCode = response?.status || (err as any).status;
323+
324+
console.error(`[GitHub API] Detected status code: ${statusCode}`);
325+
319326
if (statusCode === 404) {
320327
console.error(`[GitHub API] Throwing 404 - User not found`);
321328
throw error(404, `GitHub user "${username}" not found`);
322329
}
323330
if (statusCode === 401 || statusCode === 403) {
324-
console.error(`[GitHub API] Throwing 500 - Authentication failed`);
325-
throw error(500, 'GitHub API authentication failed');
331+
console.error(`[GitHub API] Throwing 500 - Authentication failed (Status: ${statusCode})`);
332+
throw error(500, 'GitHub API authentication failed. Please check your token configuration.');
326333
}
327334
}
328335

@@ -370,6 +377,16 @@ export const getContributionYears = query(
370377
return data.user.contributionsCollection.contributionYears || [];
371378
} catch (err) {
372379
console.error('Error fetching contribution years:', err);
380+
381+
if (err && typeof err === 'object') {
382+
const response = (err as any).response;
383+
const statusCode = response?.status || (err as any).status;
384+
385+
if (statusCode === 401 || statusCode === 403) {
386+
throw error(500, 'GitHub API authentication failed. Please check your token configuration.');
387+
}
388+
}
389+
373390
throw error(500, 'Failed to fetch contribution years');
374391
}
375392
}

src/routes/apps/(app)/github-contribution-tracker/[user]/[year]/+page.server.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,37 @@ const CONTRIBUTION_QUERY = gql`
126126

127127
// ========== Helper Functions ==========
128128

129+
/**
130+
* Creates a GraphQL client with GitHub token
131+
*/
132+
// Import dynamic private environment variables
133+
import { env } from '$env/dynamic/private';
134+
129135
/**
130136
* Creates a GraphQL client with GitHub token
131137
*/
132138
function createGitHubClient(): GraphQLClient {
133-
const token = process.env.VITE_GITHUB_TOKEN;
139+
// Try standard GITHUB_TOKEN first, then fall back to VITE_ prefixed version
140+
// Using $env/dynamic/private ensures we get the runtime value in all adapters
141+
let token = env.GITHUB_TOKEN || env.VITE_GITHUB_TOKEN || process.env.VITE_GITHUB_TOKEN || '';
142+
token = token.trim();
134143

135144
console.log(`[GitHub API] Checking for GitHub token...`);
136145
if (!token) {
137-
console.error(`[GitHub API] No VITE_GITHUB_TOKEN found in environment`);
146+
console.error(`[GitHub API] No GITHUB_TOKEN or VITE_GITHUB_TOKEN found in environment`);
138147
throw error(500, 'GitHub token not configured');
139148
}
140-
console.log(`[GitHub API] Token found (length: ${token.length})`);
149+
150+
// Log token details for debugging (safe mask)
151+
const mask =
152+
token.length > 8 ? `${token.substring(0, 4)}...${token.substring(token.length - 4)}` : '***';
153+
console.log(`[GitHub API] Token found (length: ${token.length}, prefix: ${mask})`);
141154

142155
return new GraphQLClient('https://api.github.com/graphql', {
143156
headers: {
144-
Authorization: `bearer ${token}`
157+
Authorization: `Bearer ${token}`,
158+
'User-Agent': 'Svelte-MiniApps-GitHub-Tracker',
159+
'X-GitHub-Api-Version': '2022-11-28'
145160
}
146161
});
147162
}
@@ -352,16 +367,20 @@ export const load: PageServerLoad = async ({ params }) => {
352367
}
353368

354369
// Handle specific error types
355-
if (err && typeof err === 'object' && 'status' in err) {
356-
const statusCode = (err as any).status;
357-
console.error(`[GitHub API] HTTP status code: ${statusCode}`);
370+
if (err && typeof err === 'object') {
371+
// graphql-request ClientError structure
372+
const response = (err as any).response;
373+
const statusCode = response?.status || (err as any).status;
374+
375+
console.error(`[GitHub API] Detected status code: ${statusCode}`);
376+
358377
if (statusCode === 404) {
359378
console.error(`[GitHub API] Throwing 404 - User not found`);
360379
throw error(404, `GitHub user "${username}" not found`);
361380
}
362381
if (statusCode === 401 || statusCode === 403) {
363-
console.error(`[GitHub API] Throwing 500 - Authentication failed`);
364-
throw error(500, 'GitHub API authentication failed');
382+
console.error(`[GitHub API] Throwing 500 - Authentication failed (Status: ${statusCode})`);
383+
throw error(500, 'GitHub API authentication failed. Please check your token configuration.');
365384
}
366385
}
367386

0 commit comments

Comments
 (0)