mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 17:13:33 +01:00
fix(logging): give raw GitHub failures an error code, log missing branches as warnings
Overnight, GitHub refusals reached the logs as bare HTTPError/UnknownError with no code: a ZIP whose archive request was refused became a cut connection and a 502 zip_not_available, a 401 while checking a file became an uncoded 401, and an Octokit 403 on /options answered internal_error. - codeUpstreamError maps got/Octokit replies to repo_not_accessible, github_rate_limit_exceeded, github_unavailable or upstream_error; handleError applies it, the streamer file route uses the file-level mapping, and programming errors pass through unchanged. - A ZIP that fails before any byte answers with the code, so the API relays it; 4xx archive refusals log at warn. - branch_not_found (owner deleted the branch) logs at warn. - Octokit's HttpError is an allowlisted error type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
351ed12b28
commit
3f8812c99e
10 files changed
+195
-11
No files matched your search
@@ -0,0 +1,58 @@
|
||||
const { expect } = require("chai");
|
||||
require("ts-node/register/transpile-only");
|
||||
const http = require("node:http");
|
||||
const { PassThrough } = require("node:stream");
|
||||
const AnonymousError = require("../src/core/AnonymousError").default;
|
||||
const { codeUpstreamError } = require("../src/core/upstream-error");
|
||||
const { handleError } = require("../src/server/routes/route-utils");
|
||||
const { streamAnonymizedZip } = require("../src/core/zipStream");
|
||||
|
||||
function octokitError(status) {
|
||||
return Object.assign(new Error("Resource not accessible"), { name: "HttpError", status });
|
||||
}
|
||||
|
||||
describe("upstream error codes", () => {
|
||||
it("codes raw GitHub replies and leaves bugs alone", () => {
|
||||
const denied = codeUpstreamError(octokitError(403));
|
||||
expect(denied).to.be.instanceOf(AnonymousError);
|
||||
expect(denied.message).to.equal("repo_not_accessible");
|
||||
expect(denied.httpStatus).to.equal(403);
|
||||
const gotError = Object.assign(new Error("Response code 503"), { name: "HTTPError", response: { statusCode: 503 } });
|
||||
expect(codeUpstreamError(gotError).message).to.equal("github_unavailable");
|
||||
expect(codeUpstreamError(octokitError(429)).message).to.equal("github_rate_limit_exceeded");
|
||||
const reset = Object.assign(new Error("socket hang up"), { name: "RequestError", code: "ECONNRESET" });
|
||||
expect(codeUpstreamError(reset).message).to.equal("upstream_error");
|
||||
const bug = new TypeError("x is undefined");
|
||||
expect(codeUpstreamError(bug)).to.equal(bug);
|
||||
});
|
||||
|
||||
it("answers an Octokit failure with its code instead of internal_error", () => {
|
||||
let status, body;
|
||||
const res = { headersSent: false, status(code) { status = code; return this; }, json(value) { body = value; } };
|
||||
handleError(octokitError(403), res);
|
||||
expect(status).to.equal(403);
|
||||
expect(body).to.deep.equal({ error: "repo_not_accessible" });
|
||||
});
|
||||
|
||||
it("replies to a refused archive with a code before any ZIP byte", async () => {
|
||||
const server = http.createServer((req, res) => { res.statusCode = 404; res.end("Not Found"); });
|
||||
await new Promise(resolve => server.listen(0, "127.0.0.1", resolve));
|
||||
const { port } = server.address();
|
||||
const GitHubDownload = require("../src/core/source/GitHubDownload").default;
|
||||
const original = GitHubDownload.prototype.getZipUrl;
|
||||
GitHubDownload.prototype.getZipUrl = async () => ({ url: `http://127.0.0.1:${port}/archive.zip` });
|
||||
const response = new PassThrough();
|
||||
let replied;
|
||||
response.headersSent = false;
|
||||
response.status = code => ({ json: body => { replied = { code, body }; response.end(); } });
|
||||
try {
|
||||
await streamAnonymizedZip({ repoId: "test", organization: "owner", repoName: "private", commit: "c0ffee",
|
||||
getToken: () => "ghs_token", anonymizerOptions: { terms: [] } }, response);
|
||||
await new Promise(resolve => response.once("finish", resolve).resume());
|
||||
} finally {
|
||||
GitHubDownload.prototype.getZipUrl = original;
|
||||
server.close();
|
||||
}
|
||||
expect(replied).to.deep.equal({ code: 404, body: { error: "repo_not_accessible" } });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user