Skip to content

Commit

Permalink
fix: fixed "Request took longer than Xms" issue and dummy-proofing is…
Browse files Browse the repository at this point in the history
…ErrorConstructorName logic
  • Loading branch information
titanism committed Dec 18, 2024
1 parent b222a84 commit 02f3170
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 40 deletions.
9 changes: 5 additions & 4 deletions helpers/get-bounce-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ function getBounceInfo(err) {
) {
if (REGEX_VIRUS.test(response)) bounceInfo.category = 'virus';
else if (REGEX_SPAM.test(response)) bounceInfo.category = 'spam';
else if (isRetryableError(err)) {
bounceInfo.category = 'network';
bounceInfo.action = 'defer';
}
}

if (isRetryableError(err)) {
bounceInfo.category = 'network';
bounceInfo.action = 'defer';
}

// <https://github.com/zone-eu/zone-mta/issues/434>
Expand Down
31 changes: 11 additions & 20 deletions helpers/is-error-constructor-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,24 @@
// <https://github.com/ladjs/koa-better-error-handler/blob/261701158b5151f7df4020834e47249b09952aa8/index.js#L57-L85>
//
function isErrorConstructorName(err, name) {
const names = [];
const names = new Set();

let e = err;
while (e) {
if (!e || !e.name || names.includes(e.name)) break;
names.push(e.name);
if (
!err.constructor ||
!Object.getPrototypeOf(err.constructor).name ||
names.includes(Object.getPrototypeOf(err.constructor).name)
)
if (!e || !e.name) break;
names.add(e.name);
if (e.constructor && e.constructor.name) names.add(e.constructor.name);
if (!e.constructor || !Object.getPrototypeOf(e.constructor).name) break;
names.add(Object.getPrototypeOf(e.constructor).name);
if (!Object.getPrototypeOf(Object.getPrototypeOf(e.constructor)).name)
break;
names.push(Object.getPrototypeOf(err.constructor).name);
if (
!Object.getPrototypeOf(Object.getPrototypeOf(err.constructor)).name ||
names.includes(
Object.getPrototypeOf(Object.getPrototypeOf(err.constructor)).name
)
)
break;
names.push(
Object.getPrototypeOf(Object.getPrototypeOf(err.constructor)).name
);
names.add(Object.getPrototypeOf(Object.getPrototypeOf(e.constructor)).name);
e = Object.getPrototypeOf(e.constructor);
}

return names.includes(name);
names.delete('Object');

return names.has(name);
}

module.exports = isErrorConstructorName;
1 change: 1 addition & 0 deletions helpers/is-timeout-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function isTimeoutError(err) {
if (typeof err[key] !== 'string') continue;
const str = err[key].toLowerCase();
if (
str.includes('request took longer than') ||
str.includes('request aborted') ||
str.includes('timeout') ||
str.includes('request time-out') ||
Expand Down
28 changes: 13 additions & 15 deletions helpers/on-data-mx.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ const scanner = new SpamScanner({
// TODO: if `err.truthSource` is `outlook.com` then link to this article
//
// TODO: we need to be careful here because of false positives for DMARC failures
// TODO: disabled until we use MongoDB for this
/*
async function sendSysAdminEmail(template, err, session, headers) {
// safeguard in case we add more of these kinds of alerts
if (template !== 'dmarc-issue') throw new TypeError('Invalid template');
Expand All @@ -128,13 +130,6 @@ async function sendSysAdminEmail(template, err, session, headers) {
session.resolvedRootClientHostname !==
session.originalFromAddressRootDomain)
) {
const _err = new TypeError(
`Preventing sysadmin ${template} email to ${session.originalFromAddressRootDomain}`
);
_err.isCodeBug = true;
_err.session = session;
_err.original_error = parseErr(err);
logger.fatal(_err);
return;
}
Expand Down Expand Up @@ -169,6 +164,7 @@ async function sendSysAdminEmail(template, err, session, headers) {
}
});
}
*/

async function sendBounce(bounce, headers, session, sealedMessage) {
try {
Expand Down Expand Up @@ -1082,11 +1078,12 @@ async function forward(recipient, headers, session, raw, body) {
// (the below logic is duplicated from elsewhere in this file)
session.rewriteFriendlyFrom = true;

// TODO: disabled until we use MongoDB for this
// notify common system administrator usernames of this issue
sendSysAdminEmail
.call(this, 'dmarc-issue', err, session, headers)
.then()
.catch((err) => logger.fatal(err));
// sendSysAdminEmail
// .call(this, 'dmarc-issue', err, session, headers)
// .then()
// .catch((err) => logger.fatal(err));

headers.update(
'From',
Expand Down Expand Up @@ -1343,11 +1340,12 @@ async function updateMXHeaders(session, headers, body) {
);
err.truthSource = env.WEB_HOST;

// TODO: disabled until we use MongoDB for this
// notify common system administrator usernames of this issue
sendSysAdminEmail
.call(this, 'dmarc-issue', err, session, headers)
.then()
.catch((err) => logger.fatal(err));
// sendSysAdminEmail
// .call(this, 'dmarc-issue', err, session, headers)
// .then()
// .catch((err) => logger.fatal(err));

headers.update(
'From',
Expand Down
2 changes: 1 addition & 1 deletion helpers/retry-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class RetryClient extends undici.Client {
const t = setTimeout(() => {
if (!abortController?.signal?.aborted)
abortController.abort(
new TimeoutError(`Request took longer than ${timeout}ms}`)
new TimeoutError(`Request took longer than ${timeout}ms`)
);
}, timeout);

Expand Down

0 comments on commit 02f3170

Please sign in to comment.