Skip to content

Commit 1625a85

Browse files
author
kali
committed
fix(api): classify stats route GitHub errors
1 parent fef6257 commit 1625a85

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

app/api/stats/route.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,28 @@ describe('GET /api/stats', () => {
158158
expect(body.error).toBe('GitHub API error');
159159
});
160160

161+
it('returns 404 when GitHub reports that the user does not exist', async () => {
162+
vi.mocked(fetchGitHubContributions).mockRejectedValue(
163+
new Error('GitHub user "missing-user" not found')
164+
);
165+
166+
const response = await GET(makeRequest({ user: 'missing-user' }));
167+
168+
expect(response.status).toBe(404);
169+
const body = await response.json();
170+
expect(body.error).toBe('User not found');
171+
});
172+
173+
it('returns 403 when GitHub rate limiting bubbles up from the client', async () => {
174+
vi.mocked(fetchGitHubContributions).mockRejectedValue(new Error('API Rate Limit Exceeded'));
175+
176+
const response = await GET(makeRequest({ user: 'testuser' }));
177+
178+
expect(response.status).toBe(403);
179+
const body = await response.json();
180+
expect(body.error).toBe('GitHub API rate limit reached. Please configure GITHUB_TOKEN.');
181+
});
182+
161183
it('returns 500 with a generic message for non-Error throws', async () => {
162184
vi.mocked(fetchGitHubContributions).mockRejectedValue('something went wrong');
163185
const response = await GET(makeRequest({ user: 'testuser' }));

app/api/stats/route.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ export async function GET(request: Request) {
7171
);
7272
} catch (error: unknown) {
7373
const message = error instanceof Error ? error.message : 'Unknown error';
74+
75+
if (
76+
message.toLowerCase().includes('not found') ||
77+
message.toLowerCase().includes('could not resolve')
78+
) {
79+
return NextResponse.json({ error: 'User not found' }, { status: 404 });
80+
}
81+
82+
if (
83+
message.toLowerCase().includes('rate limit') ||
84+
message.includes('API limit reached') ||
85+
message.includes('status 403')
86+
) {
87+
return NextResponse.json(
88+
{ error: 'GitHub API rate limit reached. Please configure GITHUB_TOKEN.' },
89+
{ status: 403 }
90+
);
91+
}
92+
7493
return NextResponse.json({ error: message }, { status: 500 });
7594
}
7695
}

0 commit comments

Comments
 (0)