Skip to content

Commit

Permalink
Added custom errors for github client
Browse files Browse the repository at this point in the history
  • Loading branch information
this-Aditya committed Aug 22, 2024
1 parent 1ae9911 commit ace41bb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
32 changes: 21 additions & 11 deletions services/github-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { GITHUB_AUTH_CONFIG, GITHUB_CONFIG } from "../config/github-config";
import {GithubApiError} from "../utils/errors/GithubApiError";
import {ContentLengthError} from "../utils/errors/ContentLengthError";

/**
* A client for interacting with the GitHub API.
Expand Down Expand Up @@ -30,18 +32,26 @@ class GithubClient {
method: 'GET',
});

if (response.status === 401) {
throw new Error("Unauthorized: Please check your GitHub token.");
}
if (response.status === 401) {
throw new GithubApiError("Unauthorized: Please check your GitHub token.", 401);
}

if (response.status === 403) {
throw new GithubApiError(`Forbidden: You have exceeded the rate limit or do not have permission to access this resource.`, 403);
}

if (response.status === 403) {
throw new Error(`Forbidden: ${await response.text()}`);
}
if (response.status === 404) {
throw new GithubApiError(`Not Found: The requested resource could not be found on GitHub.`, 404);
}

if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to fetch content from GitHub: ${errorText}`);
}
if (response.status === 500) {
throw new GithubApiError(`Internal Server Error: GitHub is experiencing issues. Please try again later.`, 500);
}

if (!response.ok) {
const errorText = await response.text();
throw new GithubApiError(`Failed to fetch content from GitHub: ${errorText || 'Unknown error occurred'}`, response.status);
}

this.checkContentLength(parseInt(response.headers.get('Content-Length') || '0', 10));

Expand All @@ -56,7 +66,7 @@ class GithubClient {
*/
private checkContentLength(contentLength: number) {
if (contentLength >= this.maxContentLength) {
throw new Error('Data received is too large to process');
throw new ContentLengthError('Data received from github is too large to process');
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions utils/errors/ContentLengthError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class ContentLengthError extends Error {

constructor(message: string) {
super(message);
this.name = "ContentLengthError";

if (Error.captureStackTrace) {
Error.captureStackTrace(this, ContentLengthError);
}
}
}
13 changes: 13 additions & 0 deletions utils/errors/GithubApiError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class GithubApiError extends Error {
statusCode: number;

constructor(message: string, statusCode: number) {
super(message);
this.name = "GithubApiError";
this.statusCode = statusCode;

if (Error.captureStackTrace) {
Error.captureStackTrace(this, GithubApiError);
}
}
}

0 comments on commit ace41bb

Please sign in to comment.