Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/decorators/github.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { GitHubApiError } from '../errors';
import { FetchApiError } from '../errors';
import { assertNonNullish } from '../utils/assert';

const GitHubUserSchema = z.object({
Expand Down Expand Up @@ -44,7 +44,7 @@ export class GitHubDecorator {
});

if (!accessTokenResponse.ok) {
throw new GitHubApiError(
throw new FetchApiError(
accessTokenResponse.status,
`GitHub access token error: ${accessTokenResponse.status}`
);
Expand All @@ -64,7 +64,7 @@ export class GitHubDecorator {
});

if (!userResponse.ok) {
throw new GitHubApiError(userResponse.status, `GitHub API error: ${userResponse.status}`);
throw new FetchApiError(userResponse.status, `GitHub API error: ${userResponse.status}`);
}

const data = await userResponse.json();
Expand Down
2 changes: 1 addition & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class GitHubApiError extends Error {
export class FetchApiError extends Error {
constructor(
readonly statusCode: number,
readonly message: string
Expand Down
6 changes: 3 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Elysia } from 'elysia';
import packageJson from '../package.json';
import { GitHubDecorator } from './decorators/github';
import { JwtDecorator } from './decorators/jwt';
import { GitHubApiError, GitHubAuthUnauthorizedError, NullishError } from './errors';
import { FetchApiError, GitHubAuthUnauthorizedError, NullishError } from './errors';
import {
GitHubAuthFinalizeSchema,
GitHubAuthInitSchema,
Expand All @@ -17,13 +17,13 @@ const { version: appVersion, name: appName, description: appDescription } = pack

export const app = new Elysia()
.error({
GitHubApiError,
FetchApiError,
NullishError,
GitHubAuthNotInitializedError: GitHubAuthUnauthorizedError
})
.onError(({ code, error, status }) => {
switch (code) {
case 'GitHubApiError':
case 'FetchApiError':
return status(error.statusCode, error.message);
case 'GitHubAuthNotInitializedError':
return status(401, error.message);
Expand Down
8 changes: 4 additions & 4 deletions test/decorators/github.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
import { GitHubDecorator } from '../../src/decorators/github';
import { GitHubApiError } from '../../src/errors';
import { FetchApiError } from '../../src/errors';

describe('decorators > github', () => {
let github: GitHubDecorator;
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('decorators > github', () => {
spyOn(global, 'fetch').mockResolvedValue(new Response('{}', { status: 401 }));

expect(github.exchangeGitHubCodeForAccessToken({ code: 'bad-code' })).rejects.toThrow(
GitHubApiError
FetchApiError
);
});

Expand Down Expand Up @@ -142,7 +142,7 @@ describe('decorators > github', () => {
spyOn(global, 'fetch').mockResolvedValue(new Response('{}', { status: 401 }));

expect(github.fetchGitHubUser({ accessToken: 'invalid-token' })).rejects.toThrow(
GitHubApiError
FetchApiError
);
});

Expand All @@ -151,7 +151,7 @@ describe('decorators > github', () => {

const error = await github.fetchGitHubUser({ accessToken: 'expired-token' }).catch((e) => e);

expect(error).toBeInstanceOf(GitHubApiError);
expect(error).toBeInstanceOf(FetchApiError);
expect(error.statusCode).toBe(403);
});

Expand Down
Loading