mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-04-21 21:06:01 +02:00
Standardize error responses with consistent format and human-readable messages (#667)
This commit is contained in:
@@ -114,7 +114,7 @@ export default class PullRequest {
|
||||
this.status == "removing" ||
|
||||
this.status == "removed"
|
||||
) {
|
||||
throw new AnonymousError("pullRequest_expired", {
|
||||
throw new AnonymousError("pull_request_expired", {
|
||||
object: this,
|
||||
httpStatus: 410,
|
||||
});
|
||||
@@ -126,8 +126,9 @@ export default class PullRequest {
|
||||
this.status == "preparing" ||
|
||||
(this.status == "download" && this._model.statusDate > fiveMinuteAgo)
|
||||
) {
|
||||
throw new AnonymousError("pullRequest_not_ready", {
|
||||
throw new AnonymousError("pull_request_not_ready", {
|
||||
object: this,
|
||||
httpStatus: 503,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,6 +220,7 @@ export default class Repository {
|
||||
) {
|
||||
throw new AnonymousError("repository_not_ready", {
|
||||
object: this,
|
||||
httpStatus: 503,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -356,6 +357,7 @@ export default class Repository {
|
||||
await this.resetSate();
|
||||
throw new AnonymousError("branch_not_found", {
|
||||
object: this,
|
||||
httpStatus: 404,
|
||||
});
|
||||
}
|
||||
this._model.anonymizeDate = new Date();
|
||||
|
||||
@@ -127,7 +127,7 @@ export default class S3Storage extends StorageBase {
|
||||
}
|
||||
} catch {
|
||||
try {
|
||||
res.status(500);
|
||||
res.status(500).json({ error: "file_not_found" });
|
||||
} catch (err) {
|
||||
console.error(`[ERROR] S3 send ${path}`, err);
|
||||
}
|
||||
|
||||
+1
-1
@@ -27,7 +27,7 @@ function indexResponse(req: express.Request, res: express.Response) {
|
||||
req.path.startsWith("/favicon") ||
|
||||
req.path.startsWith("/api")
|
||||
) {
|
||||
return res.status(404).send("Not found");
|
||||
return res.status(404).json({ error: "not_found" });
|
||||
}
|
||||
if (
|
||||
req.params.repoId &&
|
||||
|
||||
@@ -59,7 +59,7 @@ router.post("/queue/:name/:repo_id", async (req, res) => {
|
||||
}
|
||||
res.send("ok");
|
||||
} catch {
|
||||
res.status(500).send("error_retrying_job");
|
||||
res.status(500).json({ error: "error_retrying_job" });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -102,9 +102,9 @@ export function handleError(
|
||||
req?: express.Request
|
||||
) {
|
||||
printError(error, req);
|
||||
let message = error;
|
||||
let errorCode = error;
|
||||
if (error instanceof Error) {
|
||||
message = error.message;
|
||||
errorCode = error.message;
|
||||
}
|
||||
let status = 500;
|
||||
if (error.httpStatus) {
|
||||
@@ -112,15 +112,16 @@ export function handleError(
|
||||
} else if (error.$metadata?.httpStatusCode) {
|
||||
status = error.$metadata.httpStatusCode;
|
||||
} else if (
|
||||
message &&
|
||||
(message.indexOf("not_found") > -1 || message.indexOf("(Not Found)") > -1)
|
||||
errorCode &&
|
||||
(errorCode.indexOf("not_found") > -1 ||
|
||||
errorCode.indexOf("(Not Found)") > -1)
|
||||
) {
|
||||
status = 404;
|
||||
} else if (message && message.indexOf("not_connected") > -1) {
|
||||
} else if (errorCode && errorCode.indexOf("not_connected") > -1) {
|
||||
status = 401;
|
||||
}
|
||||
if (res && !res.headersSent) {
|
||||
res.status(status).send({ error: message });
|
||||
res.status(status).json({ error: errorCode });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user