Skip to content

Commit

Permalink
fix: fixed logger/stringify usage
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Dec 19, 2024
1 parent 665b8a1 commit 6a2e834
Show file tree
Hide file tree
Showing 27 changed files with 93 additions and 65 deletions.
5 changes: 3 additions & 2 deletions app/controllers/api/v1/paypal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const _ = require('lodash');
const isSANB = require('is-string-and-not-blank');
const ms = require('ms');
const parseErr = require('parse-err');
const safeStringify = require('fast-safe-stringify');

const config = require('#config');
const emailHelper = require('#helpers/email');
Expand Down Expand Up @@ -416,7 +417,7 @@ async function processEvent(ctx) {
subject: `Error while capturing PayPal order payment for ${user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down Expand Up @@ -522,7 +523,7 @@ async function webhook(ctx) {
subject: `Error with PayPal Webhook (Event ID ${ctx.request.body.id})`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/api/v1/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const ms = require('ms');
const parseErr = require('parse-err');
const pMapSeries = require('p-map-series');
const titleize = require('titleize');
const safeStringify = require('fast-safe-stringify');

const { Users, Domains } = require('#models');
const config = require('#config');
Expand Down Expand Up @@ -597,7 +598,7 @@ async function webhook(ctx) {
subject: `Error with Stripe Webhook (Event ID ${event.id})`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down
33 changes: 17 additions & 16 deletions app/controllers/web/my-account/retrieve-domain-billing.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const isSANB = require('is-string-and-not-blank');
const ms = require('ms');
const pMapSeries = require('p-map-series');
const parseErr = require('parse-err');
const safeStringify = require('fast-safe-stringify');
const striptags = require('striptags');

const config = require('#config');
Expand Down Expand Up @@ -674,7 +675,7 @@ async function retrieveDomainBilling(ctx) {
: `Stripe Payment Intent/Method Error for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down Expand Up @@ -723,7 +724,7 @@ async function retrieveDomainBilling(ctx) {
} for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down Expand Up @@ -810,7 +811,7 @@ async function retrieveDomainBilling(ctx) {
subject: `Error retrieving/creating stripe payment for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand All @@ -835,7 +836,7 @@ async function retrieveDomainBilling(ctx) {
subject: `Error saving user for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down Expand Up @@ -906,7 +907,7 @@ async function retrieveDomainBilling(ctx) {
} for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down Expand Up @@ -1009,7 +1010,7 @@ async function retrieveDomainBilling(ctx) {
subject: `Error while capturing PayPal order payment for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down Expand Up @@ -1073,7 +1074,7 @@ async function retrieveDomainBilling(ctx) {
subject: `Error retrieving/creating paypal payment for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand All @@ -1097,7 +1098,7 @@ async function retrieveDomainBilling(ctx) {
subject: `Error saving user for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down Expand Up @@ -1154,7 +1155,7 @@ async function retrieveDomainBilling(ctx) {
} for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down Expand Up @@ -1302,7 +1303,7 @@ async function retrieveDomainBilling(ctx) {
subject: `Error retrieving/creating paypal payment for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand All @@ -1327,7 +1328,7 @@ async function retrieveDomainBilling(ctx) {
subject: `Error saving user for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down Expand Up @@ -1392,7 +1393,7 @@ async function retrieveDomainBilling(ctx) {
} for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down Expand Up @@ -1483,7 +1484,7 @@ async function retrieveDomainBilling(ctx) {
} for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down Expand Up @@ -1517,7 +1518,7 @@ async function retrieveDomainBilling(ctx) {
} for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down Expand Up @@ -1604,7 +1605,7 @@ async function retrieveDomainBilling(ctx) {
subject: `A refund error occurred for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down Expand Up @@ -1671,7 +1672,7 @@ async function retrieveDomainBilling(ctx) {
subject: `A refund error occurred for ${ctx.state.user.email}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down
2 changes: 1 addition & 1 deletion app/models/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ Logs.postCreate(async (doc, next) => {
subject: `Code Bug: ${doc?.err?.name} - ${doc?.err?.message} (${doc.id})`
},
locals: {
message: `<pre><code>${JSON.stringify(doc, null, 2)}</code></pre>`
message: `<pre><code>${safeStringify(doc, null, 2)}</code></pre>`
}
});
next();
Expand Down
5 changes: 3 additions & 2 deletions helpers/get-database.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const mongoose = require('mongoose');
const ms = require('ms');
const pRetry = require('p-retry');
const parseErr = require('parse-err');
const safeStringify = require('fast-safe-stringify');
const { Builder } = require('json-sql');
const { boolean } = require('boolean');

Expand Down Expand Up @@ -1058,11 +1059,11 @@ function retryGetDatabase(...args) {
locals: {
message: `<p>${
error.dbFilePath
}</p><hr /><pre><code>${JSON.stringify(
}</p><hr /><pre><code>${safeStringify(
error.stats,
null,
2
)}</code></pre><pre><code>${JSON.stringify(
)}</code></pre><pre><code>${safeStringify(
parseErr(error),
null,
2
Expand Down
2 changes: 1 addition & 1 deletion helpers/imap/on-append.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ async function onAppend(path, flags, date, raw, session, fn) {
},
locals: {
message: `<strong>${session.user.username}</strong> &ndash; ${err.message}`,
// message: `<pre><code>${JSON.stringify(
// message: `<pre><code>${safeStringify(
// parseErr(err),
// null,
// 2
Expand Down
27 changes: 15 additions & 12 deletions helpers/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,7 @@ for (const level of logger.config.levels) {
// <https://nodejs.org/api/globals.html#structuredclonevalue-options>
// <https://github.com/ungap/structured-clone>
try {
message = global.structuredClone(message, {
// avoid throwing
lossy: true,
// avoid throwing *and* looks for toJSON
json: true
});
message = global.structuredClone(message);
} catch (err) {
console.error({ message, err });
message = JSON.parse(safeStringify(message));
Expand All @@ -300,12 +295,20 @@ for (const level of logger.config.levels) {
// <https://nodejs.org/api/globals.html#structuredclonevalue-options>
// <https://github.com/ungap/structured-clone>
try {
meta = global.structuredClone(meta, {
// avoid throwing
lossy: true,
// avoid throwing *and* looks for toJSON
json: true
});
//
// NOTE: we need to take into account that most instances of logger
// for IMAP/POP3 have `logger.fatal(err, { session })`
// and this causes unnecessary cloning of `session.getQueryResponse`
// and would cause `DOMException [DataCloneError]` from `structuredClone`
// and the alternative would be `logger.fatal(err, { session: _.omit(session, 'getQueryResponse') })`
// which is a lot to type everywhere and to remember so this is a safeguard
//
if (
typeof meta.session === 'object' &&
typeof meta.session.getQueryResponse === 'function'
)
meta.session = _.omit(meta.session, ['getQueryResponse']);
meta = global.structuredClone(meta);
} catch (err) {
console.error({ meta, err });
message = JSON.parse(safeStringify(message));
Expand Down
4 changes: 3 additions & 1 deletion helpers/on-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ async function onAuth(auth, session, fn) {
}
}

// NOTE: this is only required for WildDuck servers (IMAP/POP3)
// override session.getQueryResponse (safeguard)
session.getQueryResponse = getQueryResponse;
if (this.server instanceof IMAPServer || this.server instanceof POP3Server)
session.getQueryResponse = getQueryResponse;

// username must be a valid email address
if (
Expand Down
3 changes: 2 additions & 1 deletion helpers/process-email.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const pMap = require('p-map');
const pMapSeries = require('p-map-series');
const parseErr = require('parse-err');
const prettyMilliseconds = require('pretty-ms');
const safeStringify = require('fast-safe-stringify');
const { SRS } = require('sender-rewriting-scheme');
const { Splitter, Joiner } = require('mailsplit');
const { authenticate } = require('mailauth');
Expand Down Expand Up @@ -1236,7 +1237,7 @@ async function processEmail({ email, port = 25, resolver, client }) {
domain.name,
domain.bounce_webhook
) +
`<pre><code>${JSON.stringify(
`<pre><code>${safeStringify(
_.omit(parseErr(err), 'stack'),
null,
2
Expand Down
7 changes: 4 additions & 3 deletions helpers/sync-paypal-subscription-payments-by-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const isSANB = require('is-string-and-not-blank');
const ms = require('ms');
const pMapSeries = require('p-map-series');
const parseErr = require('parse-err');
const safeStringify = require('fast-safe-stringify');

const emailHelper = require('./email');
const logger = require('./logger');
Expand Down Expand Up @@ -244,7 +245,7 @@ async function syncPayPalSubscriptionPaymentsByUser(errorEmails, customer) {
subject: `${customer.email} had an issue syncing a transaction from paypal subscription ${subscriptionId} and transaction ${transaction.id}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down Expand Up @@ -303,7 +304,7 @@ async function syncPayPalSubscriptionPaymentsByUser(errorEmails, customer) {
subject: `${customer.email} has an issue syncing all payments from paypal subscription ${subscriptionId} that were not synced by the sync-payment-histories job`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand All @@ -330,7 +331,7 @@ async function syncPayPalSubscriptionPaymentsByUser(errorEmails, customer) {
subject: `Sync PayPal payment histories hit ${config.paypalErrorThreshold} errors during the script`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down
3 changes: 2 additions & 1 deletion helpers/sync-stripe-payment-intent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const dedent = require('dedent');
const isSANB = require('is-string-and-not-blank');
const ms = require('ms');
const parseErr = require('parse-err');
const safeStringify = require('fast-safe-stringify');

const logger = require('./logger');
const ThresholdError = require('./threshold-error');
Expand Down Expand Up @@ -439,7 +440,7 @@ function syncStripePaymentIntent(user) {
subject: `Problem syncing billing history for ${user.email} - payment_intent ${paymentIntent.id}`
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down
3 changes: 2 additions & 1 deletion jobs/bounce-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Graceful = require('@ladjs/graceful');
const dayjs = require('dayjs-with-plugins');
const mongoose = require('mongoose');
const parseErr = require('parse-err');
const safeStringify = require('fast-safe-stringify');

const config = require('#config');
const emailHelper = require('#helpers/email');
Expand Down Expand Up @@ -88,7 +89,7 @@ graceful.listen();
subject: 'Email Deliverability Report Issue'
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down
3 changes: 2 additions & 1 deletion jobs/check-pm2.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const parseErr = require('parse-err');
const pm2 = require('pm2');
const prettyMilliseconds = require('pretty-ms');
const ms = require('ms');
const safeStringify = require('fast-safe-stringify');

const config = require('#config');
const emailHelper = require('#helpers/email');
Expand Down Expand Up @@ -151,7 +152,7 @@ graceful.listen();
subject: 'Check PM2 had an error'
},
locals: {
message: `<pre><code>${JSON.stringify(
message: `<pre><code>${safeStringify(
parseErr(err),
null,
2
Expand Down
Loading

0 comments on commit 6a2e834

Please sign in to comment.