mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-02-14 19:32:45 +00:00
feat: improve error message
This commit is contained in:
@@ -6,6 +6,7 @@ import { OAuthApp } from "@octokit/oauth-app";
|
||||
import Repository from "../Repository";
|
||||
import * as stream from "stream";
|
||||
import UserModel from "../database/users/users.model";
|
||||
import AnonymousError from "../AnonymousError";
|
||||
|
||||
export default abstract class GitHubBase {
|
||||
type: "GitHubDownload" | "GitHubStream" | "Zip";
|
||||
@@ -37,17 +38,18 @@ export default abstract class GitHubBase {
|
||||
}
|
||||
|
||||
async getFileContent(file: AnonymizedFile): Promise<stream.Readable> {
|
||||
throw new Error("Method not implemented.");
|
||||
throw new AnonymousError("Method not implemented.");
|
||||
}
|
||||
|
||||
getFiles(): Promise<Tree> {
|
||||
throw new Error("Method not implemented.");
|
||||
throw new AnonymousError("Method not implemented.");
|
||||
}
|
||||
|
||||
async getToken(owner?: string) {
|
||||
if (owner) {
|
||||
const user = await UserModel.findOne({ username: owner });
|
||||
if (user && user.accessToken) {
|
||||
return user.accessToken as string;
|
||||
if (user && user.accessTokens.github) {
|
||||
return user.accessTokens.github as string;
|
||||
}
|
||||
}
|
||||
if (this.accessToken) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import { SourceBase } from "../types";
|
||||
import got from "got";
|
||||
import * as stream from "stream";
|
||||
import { OctokitResponse } from "@octokit/types";
|
||||
import AnonymousError from "../AnonymousError";
|
||||
|
||||
export default class GitHubDownload extends GitHubBase implements SourceBase {
|
||||
constructor(
|
||||
@@ -39,7 +40,7 @@ export default class GitHubDownload extends GitHubBase implements SourceBase {
|
||||
|
||||
async download() {
|
||||
if (this.repository.status == "download")
|
||||
throw new Error("repo_in_download");
|
||||
throw new AnonymousError("repo_in_download", this.repository);
|
||||
let response: OctokitResponse<unknown, number>;
|
||||
try {
|
||||
response = await this._getZipUrl(await this.getToken());
|
||||
@@ -48,10 +49,10 @@ export default class GitHubDownload extends GitHubBase implements SourceBase {
|
||||
try {
|
||||
response = await this._getZipUrl(config.GITHUB_TOKEN);
|
||||
} catch (error) {
|
||||
throw new Error("repo_not_accessible");
|
||||
throw new AnonymousError("repo_not_accessible", this.repository);
|
||||
}
|
||||
} else {
|
||||
throw new Error("repo_not_accessible");
|
||||
throw new AnonymousError("repo_not_accessible", this.repository);
|
||||
}
|
||||
}
|
||||
await this.repository.updateStatus("download");
|
||||
|
||||
@@ -3,6 +3,7 @@ import * as gh from "parse-github-url";
|
||||
import { IRepositoryDocument } from "../database/repositories/repositories.types";
|
||||
import { Octokit } from "@octokit/rest";
|
||||
import RepositoryModel from "../database/repositories/repositories.model";
|
||||
import AnonymousError from "../AnonymousError";
|
||||
|
||||
export class GitHubRepository {
|
||||
private _data: Partial<
|
||||
@@ -116,7 +117,7 @@ export class GitHubRepository {
|
||||
selected.readme = readme;
|
||||
await model.save();
|
||||
} catch (error) {
|
||||
throw new Error("readme_not_available");
|
||||
throw new AnonymousError("readme_not_available", this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,7 +127,7 @@ export class GitHubRepository {
|
||||
public get owner(): string {
|
||||
const repo = gh(this.fullName);
|
||||
if (!repo) {
|
||||
throw "invalid_repo";
|
||||
throw new AnonymousError("invalid_repo", this);
|
||||
}
|
||||
return repo.owner || this.fullName;
|
||||
}
|
||||
@@ -134,7 +135,7 @@ export class GitHubRepository {
|
||||
public get repo(): string {
|
||||
const repo = gh(this.fullName);
|
||||
if (!repo) {
|
||||
throw "invalid_repo";
|
||||
throw new AnonymousError("invalid_repo", this);
|
||||
}
|
||||
return repo.name || this.fullName;
|
||||
}
|
||||
@@ -152,7 +153,7 @@ export async function getRepositoryFromGitHub(opt: {
|
||||
repo: opt.repo,
|
||||
})
|
||||
).data;
|
||||
if (!r) throw new Error("repo_not_found");
|
||||
if (!r) throw new AnonymousError("repo_not_found", this);
|
||||
let model = await RepositoryModel.findOne({ externalId: "gh_" + r.id });
|
||||
if (!model) {
|
||||
model = new RepositoryModel({ externalId: "gh_" + r.id });
|
||||
|
||||
@@ -7,6 +7,7 @@ import { SourceBase, Tree } from "../types";
|
||||
import * as path from "path";
|
||||
|
||||
import * as stream from "stream";
|
||||
import AnonymousError from "../AnonymousError";
|
||||
|
||||
export default class GitHubStream extends GitHubBase implements SourceBase {
|
||||
constructor(
|
||||
@@ -24,7 +25,7 @@ export default class GitHubStream extends GitHubBase implements SourceBase {
|
||||
}
|
||||
|
||||
async getFileContent(file: AnonymizedFile): Promise<stream.Readable> {
|
||||
if (!file.sha) throw new Error("file_sha_not_provided");
|
||||
if (!file.sha) throw new AnonymousError("file_sha_not_provided", file);
|
||||
const octokit = new Octokit({
|
||||
auth: await this.getToken(),
|
||||
});
|
||||
@@ -36,7 +37,7 @@ export default class GitHubStream extends GitHubBase implements SourceBase {
|
||||
file_sha: file.sha,
|
||||
});
|
||||
if (!ghRes.data.content && ghRes.data.size != 0) {
|
||||
throw new Error("file_not_accessible");
|
||||
throw new AnonymousError("file_not_accessible", file);
|
||||
}
|
||||
// empty file
|
||||
let content: Buffer;
|
||||
@@ -52,11 +53,11 @@ export default class GitHubStream extends GitHubBase implements SourceBase {
|
||||
return stream.Readable.from(content.toString());
|
||||
} catch (error) {
|
||||
if (error.status == 403) {
|
||||
throw new Error("file_too_big");
|
||||
throw new AnonymousError("file_too_big", file);
|
||||
}
|
||||
console.error(error);
|
||||
}
|
||||
throw new Error("file_not_accessible");
|
||||
throw new AnonymousError("file_not_accessible");
|
||||
}
|
||||
|
||||
async getFiles() {
|
||||
|
||||
Reference in New Issue
Block a user