From 8508a01c28ed98f105e9ab12a1fd4b2ab1acb159 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Wed, 19 Oct 2022 10:38:44 +0200 Subject: [PATCH] fix: fix error message when file is not found --- src/AnonymizedFile.ts | 6 ++---- src/routes/route-utils.ts | 2 +- src/server.ts | 2 ++ src/source/GitHubStream.ts | 7 +++++++ 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/AnonymizedFile.ts b/src/AnonymizedFile.ts index 645e27f..8990988 100644 --- a/src/AnonymizedFile.ts +++ b/src/AnonymizedFile.ts @@ -167,9 +167,8 @@ export default class AnonymizedFile { } if (await storage.exists(this.originalCachePath)) { return storage.read(this.originalCachePath); - } else { - return await this.repository.source?.getFileContent(this); } + return await this.repository.source?.getFileContent(this); } async anonymizedContent() { @@ -192,8 +191,7 @@ export default class AnonymizedFile { try { await pipe(await this.anonymizedContent(), res); } catch (error) { - handleError(error); - res.end(); + handleError(error, res); } } } diff --git a/src/routes/route-utils.ts b/src/routes/route-utils.ts index 57cc8e6..2092bc3 100644 --- a/src/routes/route-utils.ts +++ b/src/routes/route-utils.ts @@ -78,7 +78,7 @@ export function handleError( } else if (message && message.indexOf("not_connected") > -1) { status = 401; } - if (res) { + if (res && !res.headersSent) { res.status(status).send({ error: message }); } return; diff --git a/src/server.ts b/src/server.ts index e2a3014..4ab3082 100644 --- a/src/server.ts +++ b/src/server.ts @@ -40,6 +40,8 @@ export default async function start() { app.set("trust proxy", config.TRUST_PROXY); app.set("etag", "strong"); + app.get('/ip', (request, response) => response.send(request.ip)) + // handle session and connection app.use(connection.appSession); app.use(passport.initialize()); diff --git a/src/source/GitHubStream.ts b/src/source/GitHubStream.ts index d01cd08..71f5bc8 100644 --- a/src/source/GitHubStream.ts +++ b/src/source/GitHubStream.ts @@ -61,6 +61,13 @@ export default class GitHubStream extends GitHubBase implements SourceBase { await storage.write(file.originalCachePath, content, file, this); return stream.Readable.from(content); } catch (error) { + if (error.status == 404) { + throw new AnonymousError("file_not_found", { + httpStatus: error.status, + cause: error, + object: file, + }); + } throw new AnonymousError("file_too_big", { httpStatus: error.status, cause: error,