Skip to content

Commit b94ee70

Browse files
Merge pull request #18869 from mozilla/FXA-11639
feat(payments-next): Improve payments-next logging
2 parents 8962ca2 + 0e090e1 commit b94ee70

37 files changed

Lines changed: 492 additions & 97 deletions

apps/payments/next/instrumentation.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ export async function register() {
66
if (process.env.NEXT_RUNTIME === 'nodejs') {
77
await import('./sentry.server.config');
88
const { getApp } = await import('@fxa/payments/ui/server');
9+
const { monkeyPatchServerLogging } = await import('@fxa/shared/log');
910

1011
await getApp().initialize();
12+
13+
monkeyPatchServerLogging();
1114
}
1215
}

libs/payments/cart/src/lib/cart.error.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,32 @@ export class CartError extends BaseError {
2222
cause,
2323
info,
2424
});
25+
this.name = 'CartError';
26+
Object.setPrototypeOf(this, CartError.prototype);
2527
}
2628
}
2729

2830
export class CartNotCreatedError extends CartError {
2931
constructor(data: SetupCart, cause: Error) {
3032
super('Cart not created', data, cause);
33+
this.name = 'CartNotCreatedError';
34+
Object.setPrototypeOf(this, CartNotCreatedError.prototype);
3135
}
3236
}
3337

3438
export class CartNotFoundError extends CartError {
3539
constructor(cartId: string, cause: Error) {
3640
super('Cart not found', { cartId }, cause);
41+
this.name = 'CartNotFoundError';
42+
Object.setPrototypeOf(this, CartNotFoundError.prototype);
3743
}
3844
}
3945

4046
export class CartVersionMismatchError extends CartError {
4147
constructor(cartId: string) {
4248
super('Cart version mismatch', { cartId });
49+
this.name = 'CartVersionMismatchError';
50+
Object.setPrototypeOf(this, CartVersionMismatchError.prototype);
4351
}
4452
}
4553

@@ -57,30 +65,40 @@ export class CartNotUpdatedError extends CartError {
5765
},
5866
cause
5967
);
68+
this.name = 'CartNotUpdatedError';
69+
Object.setPrototypeOf(this, CartNotUpdatedError.prototype);
6070
}
6171
}
6272

6373
export class CartStateProcessingError extends CartError {
6474
constructor(cartId: string, cause: Error) {
6575
super('Cart state not changed to processing', { cartId }, cause);
76+
this.name = 'CartStateProcessingError';
77+
Object.setPrototypeOf(this, CartStateProcessingError.prototype);
6678
}
6779
}
6880

6981
export class CartStateFinishedError extends CartError {
7082
constructor() {
7183
super('Cart state is already finished', {});
84+
this.name = 'CartStateFinishedError';
85+
Object.setPrototypeOf(this, CartStateFinishedError.prototype);
7286
}
7387
}
7488

7589
export class CartNotDeletedError extends CartError {
7690
constructor(cartId: string, cause?: Error) {
7791
super('Cart not deleted', { cartId }, cause);
92+
this.name = 'CartNotDeletedError';
93+
Object.setPrototypeOf(this, CartNotDeletedError.prototype);
7894
}
7995
}
8096

8197
export class CartNotRestartedError extends CartError {
8298
constructor(previousCartId: string, cause: Error) {
8399
super('Cart not created', { previousCartId }, cause);
100+
this.name = 'CartNotRestartedError';
101+
Object.setPrototypeOf(this, CartNotRestartedError.prototype);
84102
}
85103
}
86104

@@ -91,12 +109,16 @@ export class CartInvalidStateForActionError extends CartError {
91109
state,
92110
action,
93111
});
112+
this.name = 'CartInvalidStateForActionError';
113+
Object.setPrototypeOf(this, CartInvalidStateForActionError.prototype);
94114
}
95115
}
96116

97117
export class CartTotalMismatchError extends CartError {
98118
constructor(cartId: string, cartAmount: number, invoiceAmount: number) {
99119
super('Cart total mismatch', { cartId, cartAmount, invoiceAmount });
120+
this.name = 'CartTotalMismatchError';
121+
Object.setPrototypeOf(this, CartTotalMismatchError.prototype);
100122
}
101123
}
102124

@@ -111,6 +133,8 @@ export class CartEligibilityMismatchError extends CartError {
111133
cartEligibility,
112134
incomingEligibility,
113135
});
136+
this.name = 'CartEligibilityMismatchError';
137+
Object.setPrototypeOf(this, CartEligibilityMismatchError.prototype);
114138
}
115139
}
116140

@@ -119,6 +143,8 @@ export class CartAccountNotFoundError extends CartError {
119143
super('Cart account not found for uid', {
120144
cartId,
121145
});
146+
this.name = 'CartAccountNotFoundError';
147+
Object.setPrototypeOf(this, CartAccountNotFoundError.prototype);
122148
}
123149
}
124150

@@ -127,6 +153,8 @@ export class CartUidNotFoundError extends CartError {
127153
super('Cart uid not found', {
128154
cartId,
129155
});
156+
this.name = 'CartUidNotFoundError';
157+
Object.setPrototypeOf(this, CartUidNotFoundError.prototype);
130158
}
131159
}
132160

@@ -141,6 +169,8 @@ export class CartInvalidPromoCodeError extends CartError {
141169
undefined,
142170
'CartInvalidPromoCodeError'
143171
);
172+
this.name = 'CartInvalidPromoCodeError';
173+
Object.setPrototypeOf(this, CartInvalidPromoCodeError.prototype);
144174
}
145175
}
146176

@@ -155,6 +185,8 @@ export class CartCurrencyNotFoundError extends CartError {
155185
currency,
156186
country,
157187
});
188+
this.name = 'CartCurrencyNotFoundError';
189+
Object.setPrototypeOf(this, CartCurrencyNotFoundError.prototype);
158190
}
159191
}
160192

@@ -163,6 +195,8 @@ export class CartNoTaxAddressError extends CartError {
163195
super('Cart tax address not found', {
164196
cartId,
165197
});
198+
this.name = 'CartNoTaxAddressError';
199+
Object.setPrototypeOf(this, CartNoTaxAddressError.prototype);
166200
}
167201
}
168202

@@ -171,5 +205,7 @@ export class CartSubscriptionNotFoundError extends CartError {
171205
super('Cart subscription not found', {
172206
cartId,
173207
});
208+
this.name = 'CartSubscriptionNotFoundError';
209+
Object.setPrototypeOf(this, CartSubscriptionNotFoundError.prototype);
174210
}
175211
}

libs/payments/cart/src/lib/cart.manager.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
import { NotFoundError } from 'objection';
55
import { v4 as uuidv4 } from 'uuid';
6-
6+
import type { LoggerService } from '@nestjs/common';
77
import { AccountDbProvider, CartState } from '@fxa/shared/db/mysql/account';
8-
import { Inject, Injectable } from '@nestjs/common';
8+
import { Inject, Injectable, Logger } from '@nestjs/common';
99

1010
import {
1111
CartInvalidStateForActionError,
@@ -35,7 +35,11 @@ import type {
3535
CartErrorReasonId,
3636
} from '@fxa/shared/db/mysql/account';
3737
import assert from 'assert';
38-
import { CaptureTimingWithStatsD, StatsDService, type StatsD } from '@fxa/shared/metrics/statsd';
38+
import {
39+
CaptureTimingWithStatsD,
40+
StatsDService,
41+
type StatsD,
42+
} from '@fxa/shared/metrics/statsd';
3943
// For an action to be executed, the cart state needs to be in one of
4044
// valid states listed in the array of CartStates below
4145
const ACTIONS_VALID_STATE = {
@@ -60,7 +64,8 @@ const isAction = (action: string): action is keyof typeof ACTIONS_VALID_STATE =>
6064
export class CartManager {
6165
constructor(
6266
@Inject(AccountDbProvider) private db: AccountDatabase,
63-
@Inject(StatsDService) public statsd: StatsD
67+
@Inject(StatsDService) public statsd: StatsD,
68+
@Inject(Logger) private log: LoggerService
6469
) {}
6570

6671
/**
@@ -123,7 +128,7 @@ export class CartManager {
123128
currency: cart.currency,
124129
};
125130
} catch (error) {
126-
console.log(error);
131+
this.log.error(error);
127132
throw new CartNotCreatedError(input, error);
128133
}
129134
}
@@ -159,7 +164,7 @@ export class CartManager {
159164
currency: cart.currency,
160165
};
161166
} catch (error) {
162-
console.log(error);
167+
this.log.error(error);
163168
throw new CartNotCreatedError(input, error);
164169
}
165170
}

libs/payments/cart/src/lib/cart.service.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import { Inject, Injectable } from '@nestjs/common';
5+
import { Inject, Injectable, Logger } from '@nestjs/common';
66
import type { LoggerService } from '@nestjs/common';
77
import * as Sentry from '@sentry/node';
88
import assert from 'assert';
@@ -51,7 +51,6 @@ import {
5151
CartState,
5252
} from '@fxa/shared/db/mysql/account';
5353
import { SanitizeExceptions } from '@fxa/shared/error';
54-
import { LOGGER_PROVIDER } from '@fxa/shared/log';
5554
import { StatsDService } from '@fxa/shared/metrics/statsd';
5655

5756
import {
@@ -99,7 +98,7 @@ export class CartService {
9998
private customerSessionManager: CustomerSessionManager,
10099
private eligibilityService: EligibilityService,
101100
private invoiceManager: InvoiceManager,
102-
@Inject(LOGGER_PROVIDER) private log: LoggerService,
101+
@Inject(Logger) private log: LoggerService,
103102
private paymentMethodManager: PaymentMethodManager,
104103
private paymentIntentManager: PaymentIntentManager,
105104
private priceManager: PriceManager,
@@ -220,11 +219,14 @@ export class CartService {
220219
'checkout_failure_subscription_not_cancelled'
221220
);
222221

223-
this.log.log('checkout failed, subscription not canceled', {
224-
eligibility_status: cart.eligibilityStatus,
225-
offering_id: cart.offeringConfigId,
226-
interval: cart.interval,
227-
});
222+
this.log.log(
223+
'cartService.wrapWithCartCatch.subscriptionNotCancelled',
224+
{
225+
eligibilityStatus: cart.eligibilityStatus,
226+
offeringId: cart.offeringConfigId,
227+
interval: cart.interval,
228+
}
229+
);
228230
}
229231
}
230232
} catch (e) {
@@ -358,7 +360,6 @@ export class CartService {
358360
CartErrorReasonId.IAP_BLOCKED_CONTACT_SUPPORT
359361
);
360362
}
361-
362363
return this.cartManager.createCart(createCartParams);
363364
}
364365

libs/payments/cart/src/lib/checkout.error.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,31 @@ import { BaseError } from '@fxa/shared/error';
77
export class CheckoutError extends BaseError {
88
constructor(...args: ConstructorParameters<typeof BaseError>) {
99
super(...args);
10+
this.name = 'CheckoutError';
11+
Object.setPrototypeOf(this, CheckoutError.prototype);
1012
}
1113
}
1214

1315
export class CheckoutPaymentError extends BaseError {
1416
constructor(...args: ConstructorParameters<typeof BaseError>) {
1517
super(...args);
18+
this.name = 'CheckoutPaymentError';
19+
Object.setPrototypeOf(this, CheckoutPaymentError.prototype);
1620
}
1721
}
1822

1923
export class CheckoutFailedError extends CheckoutError {
2024
constructor(...args: ConstructorParameters<typeof BaseError>) {
2125
super(...args);
26+
this.name = 'CheckoutFailedError';
27+
Object.setPrototypeOf(this, CheckoutFailedError.prototype);
2228
}
2329
}
2430

2531
export class CheckoutUpgradeError extends BaseError {
2632
constructor(...args: ConstructorParameters<typeof BaseError>) {
2733
super(...args);
34+
this.name = 'CheckoutUpgradeError';
35+
Object.setPrototypeOf(this, CheckoutUpgradeError.prototype);
2836
}
2937
}

libs/payments/currency/src/lib/currency.error.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { BaseError } from '@fxa/shared/error';
77
export class CurrencyError extends BaseError {
88
constructor(...args: ConstructorParameters<typeof BaseError>) {
99
super(...args);
10+
this.name = 'CurrencyError';
11+
Object.setPrototypeOf(this, CurrencyError.prototype);
1012
}
1113
}
1214

@@ -18,6 +20,8 @@ export class CurrencyCodeInvalidError extends CurrencyError {
1820
},
1921
cause,
2022
});
23+
this.name = 'CurrencyCodeInvalidError';
24+
Object.setPrototypeOf(this, CurrencyCodeInvalidError.prototype);
2125
}
2226
}
2327

@@ -29,6 +33,8 @@ export class CountryCodeInvalidError extends CurrencyError {
2933
},
3034
cause,
3135
});
36+
this.name = 'CountryCodeInvalidError';
37+
Object.setPrototypeOf(this, CountryCodeInvalidError.prototype);
3238
}
3339
}
3440

@@ -41,5 +47,7 @@ export class CurrencyCountryMismatchError extends CurrencyError {
4147
},
4248
cause,
4349
});
50+
this.name = 'CurrencyCountryMismatchError';
51+
Object.setPrototypeOf(this, CurrencyCountryMismatchError.prototype);
4452
}
4553
}

0 commit comments

Comments
 (0)