Better error handling

This commit is contained in:
Fang-Pen Lin
2025-11-07 09:19:43 -08:00
parent 7edde54281
commit 181faff20b
@@ -77,13 +77,21 @@ export const pkiAcmeChallengeServiceFactory = ({
await acmeChallengeDAL.markAsInvalidCascadeById(challengeId, tx); await acmeChallengeDAL.markAsInvalidCascadeById(challengeId, tx);
// Properly type and inspect the error // Properly type and inspect the error
if (error instanceof TypeError && error.message.includes("fetch failed")) { if (error instanceof TypeError && error.message.includes("fetch failed")) {
const cause = error.cause as AggregateError; const cause = error.cause;
if (cause?.errors?.[0]?.code === "ECONNREFUSED") { if (cause instanceof Error) {
logger.error(error, "Connection refused."); if (cause.message.includes("ECONNREFUSED")) {
return new AcmeConnectionError({ message: "Connection refused." }); return new AcmeConnectionError({ message: "Connection refused" });
} else if (cause?.errors?.[0]?.code === "ENOTFOUND") { } else if (cause.message.includes("ENOTFOUND")) {
logger.error(error, "Hostname could not be resolved (DNS failure)."); return new AcmeDnsFailureError({ message: "Hostname could not be resolved (DNS failure)" });
return new AcmeDnsFailureError({ message: "Hostname could not be resolved (DNS failure)." }); }
} else if (cause instanceof AggregateError) {
// TODO: handle multiple errors
const firstError = cause.errors?.[0];
if (firstError?.code === "ECONNREFUSED") {
return new AcmeConnectionError({ message: "Connection refused" });
} else if (firstError?.code === "ENOTFOUND") {
return new AcmeDnsFailureError({ message: "Hostname could not be resolved (DNS failure)" });
}
} }
} else if (error instanceof Error) { } else if (error instanceof Error) {
logger.error(error, "Error validating ACME challenge response"); logger.error(error, "Error validating ACME challenge response");