From 466fa9823e0edf2a42d89e4830e1992a243ea779 Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Fri, 27 Mar 2026 15:38:17 +0100 Subject: [PATCH] feat: rename to FetchApiError for generic error --- src/decorators/github.ts | 6 +++--- src/errors.ts | 2 +- src/server.ts | 6 +++--- test/decorators/github.test.ts | 8 ++++---- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/decorators/github.ts b/src/decorators/github.ts index 4f6126b..132be3a 100644 --- a/src/decorators/github.ts +++ b/src/decorators/github.ts @@ -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({ @@ -44,7 +44,7 @@ export class GitHubDecorator { }); if (!accessTokenResponse.ok) { - throw new GitHubApiError( + throw new FetchApiError( accessTokenResponse.status, `GitHub access token error: ${accessTokenResponse.status}` ); @@ -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(); diff --git a/src/errors.ts b/src/errors.ts index ca834d8..eaeb57b 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -1,4 +1,4 @@ -export class GitHubApiError extends Error { +export class FetchApiError extends Error { constructor( readonly statusCode: number, readonly message: string diff --git a/src/server.ts b/src/server.ts index 2506c9c..3a4948d 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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, @@ -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); diff --git a/test/decorators/github.test.ts b/test/decorators/github.test.ts index 66e4bee..e2c4e6f 100644 --- a/test/decorators/github.test.ts +++ b/test/decorators/github.test.ts @@ -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; @@ -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 ); }); @@ -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 ); }); @@ -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); });