Skip to content

Commit 26c0b8d

Browse files
feat: rename to FetchApiError for generic error (#10)
1 parent ab046ff commit 26c0b8d

4 files changed

Lines changed: 11 additions & 11 deletions

File tree

src/decorators/github.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from 'zod';
2-
import { GitHubApiError } from '../errors';
2+
import { FetchApiError } from '../errors';
33
import { assertNonNullish } from '../utils/assert';
44

55
const GitHubUserSchema = z.object({
@@ -44,7 +44,7 @@ export class GitHubDecorator {
4444
});
4545

4646
if (!accessTokenResponse.ok) {
47-
throw new GitHubApiError(
47+
throw new FetchApiError(
4848
accessTokenResponse.status,
4949
`GitHub access token error: ${accessTokenResponse.status}`
5050
);
@@ -64,7 +64,7 @@ export class GitHubDecorator {
6464
});
6565

6666
if (!userResponse.ok) {
67-
throw new GitHubApiError(userResponse.status, `GitHub API error: ${userResponse.status}`);
67+
throw new FetchApiError(userResponse.status, `GitHub API error: ${userResponse.status}`);
6868
}
6969

7070
const data = await userResponse.json();

src/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export class GitHubApiError extends Error {
1+
export class FetchApiError extends Error {
22
constructor(
33
readonly statusCode: number,
44
readonly message: string

src/server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Elysia } from 'elysia';
44
import packageJson from '../package.json';
55
import { GitHubDecorator } from './decorators/github';
66
import { JwtDecorator } from './decorators/jwt';
7-
import { GitHubApiError, GitHubAuthUnauthorizedError, NullishError } from './errors';
7+
import { FetchApiError, GitHubAuthUnauthorizedError, NullishError } from './errors';
88
import {
99
GitHubAuthFinalizeSchema,
1010
GitHubAuthInitSchema,
@@ -17,13 +17,13 @@ const { version: appVersion, name: appName, description: appDescription } = pack
1717

1818
export const app = new Elysia()
1919
.error({
20-
GitHubApiError,
20+
FetchApiError,
2121
NullishError,
2222
GitHubAuthNotInitializedError: GitHubAuthUnauthorizedError
2323
})
2424
.onError(({ code, error, status }) => {
2525
switch (code) {
26-
case 'GitHubApiError':
26+
case 'FetchApiError':
2727
return status(error.statusCode, error.message);
2828
case 'GitHubAuthNotInitializedError':
2929
return status(401, error.message);

test/decorators/github.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
22
import { GitHubDecorator } from '../../src/decorators/github';
3-
import { GitHubApiError } from '../../src/errors';
3+
import { FetchApiError } from '../../src/errors';
44

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

6363
expect(github.exchangeGitHubCodeForAccessToken({ code: 'bad-code' })).rejects.toThrow(
64-
GitHubApiError
64+
FetchApiError
6565
);
6666
});
6767

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

144144
expect(github.fetchGitHubUser({ accessToken: 'invalid-token' })).rejects.toThrow(
145-
GitHubApiError
145+
FetchApiError
146146
);
147147
});
148148

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

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

154-
expect(error).toBeInstanceOf(GitHubApiError);
154+
expect(error).toBeInstanceOf(FetchApiError);
155155
expect(error.statusCode).toBe(403);
156156
});
157157

0 commit comments

Comments
 (0)