Skip to content

Commit f256760

Browse files
fix(billing): serialize error as message+stack in 502 log payloads
Winston format.json() does not serialize Error non-enumerable properties, so logging `error: err` produces `{}` in JSON mode. Switch to `error: err?.message ?? String(err), stack: err?.stack` to match the existing convention in billing.webhook.service.js. Update regression test assertions to verify the extracted message string.
1 parent a6b7129 commit f256760

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

modules/billing/controllers/billing.controller.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ const checkout = async (req, res) => {
2727
const status = err.message?.startsWith('Invalid') || err.message?.includes('not found') ? 422 : 502;
2828
if (status === 502) {
2929
logger.error('[billing.checkout] createCheckout failed', {
30-
error: err,
30+
error: err?.message ?? String(err),
31+
stack: err?.stack,
3132
organizationId: req.organization?._id,
3233
priceId: req.body?.priceId,
3334
source: 'web',
@@ -149,7 +150,8 @@ const extrasCheckout = async (req, res) => {
149150
const status = err.message?.startsWith('Invalid') || err.message?.includes('not found') ? 422 : 502;
150151
if (status === 502) {
151152
logger.error('[billing.checkout] createExtrasCheckout failed', {
152-
error: err,
153+
error: err?.message ?? String(err),
154+
stack: err?.stack,
153155
organizationId: req.organization?._id,
154156
packId: req.body?.packId,
155157
source: 'web',

modules/billing/services/billing.service.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ const _ensureStripeCustomer = async (stripe, organization) => {
5858
);
5959
} catch (err) {
6060
logger.error('[billing.service] stripe.customers.create failed', {
61-
error: err,
61+
error: err?.message ?? String(err),
62+
stack: err?.stack,
6263
organizationId: String(organization._id),
6364
});
6465
throw err;
@@ -205,7 +206,8 @@ const createCheckout = async (organization, priceId, successUrl, cancelUrl) => {
205206
session = await stripe.checkout.sessions.create(checkoutParams);
206207
} catch (err) {
207208
logger.error('[billing.service] stripe.checkout.sessions.create failed', {
208-
error: err,
209+
error: err?.message ?? String(err),
210+
stack: err?.stack,
209211
organizationId: String(organization._id),
210212
priceId,
211213
plan: matchedPlan.planId,
@@ -299,7 +301,8 @@ const createExtrasCheckout = async (organization, packId, successUrl, cancelUrl,
299301
);
300302
} catch (err) {
301303
logger.error('[billing.service] stripe.checkout.sessions.create (extras) failed', {
302-
error: err,
304+
error: err?.message ?? String(err),
305+
stack: err?.stack,
303306
organizationId: orgId,
304307
packId,
305308
});

modules/billing/tests/billing.checkout.unit.tests.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,10 @@ describe('Billing service unit tests:', () => {
873873

874874
expect(mockLogger.error).toHaveBeenCalledWith(
875875
'[billing.service] stripe.customers.create failed',
876-
expect.objectContaining({ organizationId: orgId }),
876+
expect.objectContaining({
877+
organizationId: orgId,
878+
error: 'Stripe Test: customer creation blocked',
879+
}),
877880
);
878881
});
879882

@@ -897,7 +900,11 @@ describe('Billing service unit tests:', () => {
897900

898901
expect(mockLogger.error).toHaveBeenCalledWith(
899902
'[billing.service] stripe.checkout.sessions.create failed',
900-
expect.objectContaining({ organizationId: orgId, priceId: 'price_starter_m' }),
903+
expect.objectContaining({
904+
organizationId: orgId,
905+
priceId: 'price_starter_m',
906+
error: 'Stripe Test: session creation blocked',
907+
}),
901908
);
902909
});
903910

@@ -929,7 +936,11 @@ describe('Billing service unit tests:', () => {
929936

930937
expect(mockLogger.error).toHaveBeenCalledWith(
931938
'[billing.service] stripe.checkout.sessions.create (extras) failed',
932-
expect.objectContaining({ organizationId: orgId, packId: 'pack_500k' }),
939+
expect.objectContaining({
940+
organizationId: orgId,
941+
packId: 'pack_500k',
942+
error: 'Stripe Test: extras session blocked',
943+
}),
933944
);
934945
});
935946
});

0 commit comments

Comments
 (0)