chore: improve error messages

This commit is contained in:
tdurieux
2021-09-09 11:53:49 +02:00
parent efec854b46
commit a9d45a150c
16 changed files with 425 additions and 119 deletions

View File

@@ -38,11 +38,17 @@ export default abstract class GitHubBase {
}
async getFileContent(file: AnonymizedFile): Promise<stream.Readable> {
throw new AnonymousError("Method not implemented.");
throw new AnonymousError("method_not_implemented", {
httpStatus: 501,
object: this,
});
}
getFiles(): Promise<Tree> {
throw new AnonymousError("Method not implemented.");
throw new AnonymousError("method_not_implemented", {
httpStatus: 501,
object: this,
});
}
async getToken(owner?: string) {

View File

@@ -45,7 +45,10 @@ export default class GitHubDownload extends GitHubBase implements SourceBase {
this.repository.status == "download" &&
this.repository.model.statusDate > fiveMinuteAgo
)
throw new AnonymousError("repo_in_download", this.repository);
throw new AnonymousError("repo_in_download", {
httpStatus: 404,
object: this.repository,
});
let response: OctokitResponse<unknown, number>;
try {
response = await this._getZipUrl(await this.getToken());
@@ -55,11 +58,19 @@ export default class GitHubDownload extends GitHubBase implements SourceBase {
response = await this._getZipUrl(config.GITHUB_TOKEN);
} catch (error) {
await this.repository.resetSate("error");
throw new AnonymousError("repo_not_accessible", this.repository);
throw new AnonymousError("repo_not_accessible", {
httpStatus: 404,
cause: error,
object: this.repository,
});
}
} else {
await this.repository.resetSate("error");
throw new AnonymousError("repo_not_accessible", this.repository);
throw new AnonymousError("repo_not_accessible", {
httpStatus: 404,
object: this.repository,
cause: error,
});
}
}
await this.repository.updateStatus("download");
@@ -89,7 +100,11 @@ export default class GitHubDownload extends GitHubBase implements SourceBase {
await storage.extractTar(originalPath, downloadStream);
} catch (error) {
await this.repository.updateStatus("error", "unable_to_download");
throw new AnonymousError("unable_to_download", error);
throw new AnonymousError("unable_to_download", {
httpStatus: 500,
cause: error,
object: this.repository,
});
} finally {
inDownload = false;
clearTimeout(progressTimeout);

View File

@@ -117,7 +117,11 @@ export class GitHubRepository {
selected.readme = readme;
await model.save();
} catch (error) {
throw new AnonymousError("readme_not_available", this);
throw new AnonymousError("readme_not_available", {
httpStatus: 404,
cause: error,
object: this,
});
}
}
@@ -127,7 +131,10 @@ export class GitHubRepository {
public get owner(): string {
const repo = gh(this.fullName);
if (!repo) {
throw new AnonymousError("invalid_repo", this);
throw new AnonymousError("invalid_repo", {
httpStatus: 400,
object: this,
});
}
return repo.owner || this.fullName;
}
@@ -135,7 +142,10 @@ export class GitHubRepository {
public get repo(): string {
const repo = gh(this.fullName);
if (!repo) {
throw new AnonymousError("invalid_repo", this);
throw new AnonymousError("invalid_repo", {
httpStatus: 400,
object: this,
});
}
return repo.name || this.fullName;
}
@@ -159,18 +169,22 @@ export async function getRepositoryFromGitHub(opt: {
})
).data;
} catch (error) {
if (error.status == 404) {
throw new AnonymousError("repo_not_found", {
throw new AnonymousError("repo_not_found", {
httpStatus: error.status,
object: {
owner: opt.owner,
repo: opt.repo,
});
}
throw error;
},
cause: error,
});
}
if (!r)
throw new AnonymousError("repo_not_found", {
owner: opt.owner,
repo: opt.repo,
httpStatus: 404,
object: {
owner: opt.owner,
repo: opt.repo,
},
});
let model = await RepositoryModel.findOne({ externalId: "gh_" + r.id });
if (!model) {

View File

@@ -25,7 +25,11 @@ export default class GitHubStream extends GitHubBase implements SourceBase {
}
async getFileContent(file: AnonymizedFile): Promise<stream.Readable> {
if (!file.sha) throw new AnonymousError("file_sha_not_provided", file);
if (!file.sha)
throw new AnonymousError("file_sha_not_provided", {
httpStatus: 400,
object: file,
});
const octokit = new Octokit({
auth: await this.getToken(),
});
@@ -37,7 +41,10 @@ export default class GitHubStream extends GitHubBase implements SourceBase {
file_sha: file.sha,
});
if (!ghRes.data.content && ghRes.data.size != 0) {
throw new AnonymousError("file_not_accessible", file);
throw new AnonymousError("file_not_accessible", {
httpStatus: 404,
object: file,
});
}
// empty file
let content: Buffer;
@@ -54,12 +61,12 @@ export default class GitHubStream extends GitHubBase implements SourceBase {
await storage.write(file.originalCachePath, content);
return stream.Readable.from(content);
} catch (error) {
if (error.status == 403) {
throw new AnonymousError("file_too_big", file);
}
console.error(error);
throw new AnonymousError("file_too_big", {
httpStatus: error.status,
cause: error,
object: file,
});
}
throw new AnonymousError("file_not_accessible", file);
}
async getFiles() {
@@ -85,8 +92,16 @@ export default class GitHubStream extends GitHubBase implements SourceBase {
recursive: "1",
});
} catch (error) {
await this.repository.resetSate("error");
throw new AnonymousError("repo_not_accessible", this.repository);
await this.repository.resetSate("error", "repo_not_accessible");
throw new AnonymousError("repo_not_accessible", {
httpStatus: error.status,
cause: error,
object: {
owner: this.githubRepository.owner,
repo: this.githubRepository.repo,
tree_sha: sha,
},
});
}
const tree = this.tree2Tree(ghRes.data.tree, truncatedTree, parentPath);